什么?每天早上准时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

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

这些是搭建一个基本 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项目地址~

相关推荐
张3蜂1 天前
Python 四大 Web 框架对比解析:FastAPI、Django、Flask 与 Tornado
前端·python·fastapi
南风知我意9571 天前
【前端面试5】手写Function原型方法
前端·面试·职场和发展
qq_12498707531 天前
基于Java Web的城市花园小区维修管理系统的设计与实现(源码+论文+部署+安装)
java·开发语言·前端·spring boot·spring·毕业设计·计算机毕业设计
摘星编程1 天前
用React Native开发OpenHarmony应用:Image网络图片加载
javascript·react native·react.js
摘星编程1 天前
OpenHarmony环境下React Native:ImageBase64图片显示
javascript·react native·react.js
阿蒙Amon1 天前
TypeScript学习-第13章:实战与最佳实践
javascript·学习·typescript
小安驾到1 天前
【前端的坑】vxe-grid表格tooltip提示框不显示bug
前端·vue.js
去码头整点薯条981 天前
python第五次作业
linux·前端·python
沐墨染1 天前
Vue实战:自动化研判报告组件的设计与实现
前端·javascript·信息可视化·数据分析·自动化·vue
奔跑的呱呱牛1 天前
viewer-utils 图片预览工具库
javascript·vue·react