什么?每天早上准时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项目地址~

相关推荐
星空寻流年2 小时前
css3伸缩盒模型第二章(侧轴相关)
javascript·css·css3
GalenWu4 小时前
对象转换为 JSON 字符串(或反向解析)
前端·javascript·微信小程序·json
GUIQU.4 小时前
【Vue】微前端架构与Vue(qiankun、Micro-App)
前端·vue.js·架构
zwjapple4 小时前
“ES7+ React/Redux/React-Native snippets“常用快捷前缀
javascript·react native·react.js
数据潜水员4 小时前
插槽、生命周期
前端·javascript·vue.js
2401_837088504 小时前
CSS vertical-align
前端·html
优雅永不过时·4 小时前
实现一个漂亮的Three.js 扫光地面 圆形贴图扫光
前端·javascript·智慧城市·three.js·贴图·shader
CodeCraft Studio5 小时前
报表控件stimulsoft教程:使用 JoinType 关系参数创建仪表盘
前端·ui
春天姐姐6 小时前
vue知识点总结 依赖注入 动态组件 异步加载
前端·javascript·vue.js
互联网搬砖老肖7 小时前
Web 架构之数据读写分离
前端·架构·web