什么?每天早上准时9点给你发送github项目推荐邮件

前言

  • 你是否也想过每天给你定时推送github的开源项目给你?

  • 你是否也想过没有服务器的我该怎么实现?

  • 你是否也想过学过node,前端er该怎么node实战?

本文即将带领你们来使用nest+vercel来实现每天九点进行邮件通知今天的github项目推荐~

我们可以在 GitHub trending 页面上搜到最近火热的开源项目,那能不能做一个程序,自动爬取这些火热的开源项目呢?

这是一个类似于 GitHub trending 的网站,提供了 API 供我们调用。 GitHub trending 现在的反爬虫机制做的很强,对于我这种爬虫小白来说破解不了。

来到这个 ossinsight 页面,就可以看到这个网站提供的 api ,没有任何限制,直接调用即可。

bash 复制代码
https://api.ossinsight.io/v1/trends/repos?period=past_week&language=${lang}

项目准备

一个邮箱、一个github账号、一个verrcel账号,具体注册流程可以自行百度,这里不过多叙~

搭建nest项目(nest的基础知识)

首先,你需要全局安装 Nest CLI:

css 复制代码
npm i -g @nestjs/cli

然后,创建一个新的项目:

arduino 复制代码
nest new project-name

这将生成一个新的 NestJS 项目,其中包含了基本的文件结构和依赖。

  1. 进入项目目录

创建项目后,进入项目目录:

bash 复制代码
cd project-name

4. 安装依赖

在项目目录中,安装项目依赖:

复制代码
npm install

或者,如果你使用 yarn:

复制代码
yarn install

5. 启动项目

安装完依赖后,你可以启动项目:

arduino 复制代码
npm run start

或者,如果你使用 yarn:

sql 复制代码
yarn start

这将启动开发服务器,NestJS 应用将在 http://localhost:3000 上运行。

6. 生成模块和控制器

NestJS 使用模块化结构,你可以使用 CLI 来生成模块和控制器:

arduino 复制代码
nest g module cats
复制代码
nest g controller cats

这将创建一个 CatsModule 和一个 CatsController。

7. 生成服务

同样,你可以生成服务:

复制代码
nest g service cats

这将创建一个 CatsService。

8. 编写代码

现在,你可以开始编写代码了。NestJS 遵循模块化和分层架构,所以你的代码应该被组织在不同的模块、控制器、服务等中。

9. 运行测试

NestJS 支持测试,你可以使用 CLI 来生成测试文件:

kotlin 复制代码
nest g class cats.service.spec

然后,你可以使用 Jest 或其他测试框架来编写和运行测试。

10. 构建和部署

当你的项目准备好部署时,你可以构建生产版本:

arduino 复制代码
npm run build

然后,使用 node 或 pm2 等进程管理器来启动你的应用。

这些是搭建一个基本 NestJS 项目的步骤。NestJS 还提供了许多其他功能,如中间件、守卫、管道等,你可以根据需要在你的项目中使用这些功能。

本次定时发送邮件的关键代码

kotlin 复制代码
async findOne(lang: string) {
    const config = {
      method: 'get',
      maxBodyLength: Infinity,
      url: 'https://api.ossinsight.io/v1/trends/repos?period=past_week&language='+lang,
      headers: {
        Accept: "application/json",
      },
    };
    const res = await axios(config);
    const data = await this.getRepoExtraInfoByStar(res.data.data.rows);
    return {
      data
    };
  }

定时任务

typescript 复制代码
import { Injectable } from '@nestjs/common';
import { Cron } from '@nestjs/schedule';
import { GithubService } from '../github/github.service';

@Injectable()
export class CronService {
  constructor(private readonly githubService: GithubService) {}

  // 使用 @Cron 装饰器来定义一个定时执行的方法
  @Cron('00 09 * * *')
  handleCronJob() {
    this.githubService.findOne('JavaScript');
  }
}

邮件service

typescript 复制代码
import { Injectable } from '@nestjs/common';
import { MailerService } from '@nestjs-modules/mailer';

@Injectable()
export class EmailService {
  constructor(private readonly mailerService: MailerService) {}
  async sendEmail({to, subject, text,html}) {
    console.log('process.env.NEST_EMAIL', process.env.NEST_EMAIL)
    // Implement your email sending logic here
    return await this.mailerService.sendMail({
      to,
      subject,
      text,
      html
    })
  }
}

邮件module,这里使用环境变量进行

php 复制代码
import { Module } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { MailerModule } from '@nestjs-modules/mailer';
import { EmailService } from './email.service';

@Module({
  imports: [
    MailerModule.forRootAsync({
      useFactory: (configService: ConfigService) => ({
        transport: {
          host: 'smtp.qq.com',
          port: 587,
          secure: false,
          auth: {
            user: configService.get<string>('NEST_EMAIL'),
            pass: configService.get<string>('NEST_EMAIL_PASSWORD'),
          }
        },
        defaults: {
          from: "github 每日推荐" <configService.get<string>('NEST_EMAIL')>
        }
      }),
      inject: [ConfigService], // 注入ConfigService
    }),
  ],
  providers: [EmailService],
  exports: [EmailService],
})
export class EmailModule {}

上面的lang传参仅限以下的字符串

最终得到的效果,有兴趣的可以评论区拿GitHub项目地址~

相关推荐
孟陬1 小时前
Foobar 技术周刊#159:当看不懂代码时,如何高效排障调试。脏页陷阱:一条只读查询,写入数 TB 数据。推测解码:大模型加速利器
前端·人工智能·后端
Zhao-Jichao1 小时前
AI Prompting for Everyone 课程笔记 - P17 图像生成 Imaging Generation
前端
Highcharts.js2 小时前
智能数据分析组件Orbit 正式发布与快速上手体验教程
前端·人工智能·信息可视化·数据分析系统·highcharts·orbit·智能可视化
Dream-Y.ocean2 小时前
鸿蒙PC平台星图遥感适配实战:基于 Electron 壳方案的纯 JavaScript 遥感图像处理引擎开发
javascript·electron·harmonyos
螺丝钉tina2 小时前
[Playwright ][16]Playwright + TypeScript API 自动化测试实战:API测试
前端·javascript·typescript
小天源3 小时前
智叟云集系统 · 架构与职责分层
服务器·前端·数据库
Highcharts.js5 小时前
React 中原生支持使用 JSX 组件配置自定义表格 Highcharts Grid
前端·javascript·react.js·前端框架·jsx·highcharts·自定义表格
Java后端的Ai之路5 小时前
【AIAgent开发】-Agent工具封装成CLI(纯干货!!!)
java·javascript·人工智能·agent·cli
薛定谔的猫-菜鸟程序员5 小时前
Electron开发超详细教程(从入门到放弃)
前端·javascript·electron
最强小杰5 小时前
DeepSeek V4 Flash + Codex CLI 接入教程:原生 Responses API 适配,从配置到跑通收藏这篇就够了(2026)
java·前端·python·ai