leetcode 每日一题机器人推送

leetcode-question-today

github repo: cloud-org/leetcode-question-today 有用请点 star

leetcode 每日一题推送(目前支持 slack/wecom),写个机器人为了提醒自己写题。

主要原理

  • 获取每日一题链接
package api

import (
	"context"

	"github.com/machinebox/graphql"
)

func GetTodayQuestion(ctx context.Context) (*QuestionTodayResp, error) {
	// create a client (safe to share across requests)
	client := graphql.NewClient("https://leetcode.cn/graphql/")

	// make a request
	req := graphql.NewRequest(QuestionQuery)

	// set header fields
	req.Header.Set("Cache-Control", "no-cache")
	req.Header.Set("content-type", "application/json")
	req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36")
	req.Header.Set("referer", "https://leetcode.cn/problemset/all/")
	req.Header.Set("origin", "https://leetcode.cn")

	// run it and capture the response
	var resp QuestionTodayResp
	if err := client.Run(ctx, req, &resp); err != nil {
		return nil, err
	}

	return &resp, nil
}
  • 整合数据进行相关推送
	// 获取每日一题,如果有则推送即可
	resp, err := api.GetTodayQuestion(context.TODO())
	if err != nil {
		log.Printf("获取每日一题发生错误: %v\n", err)
		return
	}

	if len(resp.TodayRecord) <= 0 {
		log.Printf("todayRecord 长度为 0,请检查\n")
		return
	}

	today := resp.TodayRecord[0]

	msgTemplate := `每日一题(%s)
Title: %s
Tags: %s
Link: %s
LinkCN: %s`
	date := today.Date
	title := fmt.Sprintf("%s(%s)", today.Question.TitleCn, today.Question.Title)
	tags := make([]string, 0)
	for _, tag := range today.Question.TopicTags {
		tags = append(tags, fmt.Sprintf("%s(%s)", tag.NameTranslated, tag.Name))
	}
	tagsValue := strings.Join(tags, "、")
	link := fmt.Sprintf("%s/problems/%s", api.Leetcode, today.Question.TitleSlug)
	linkCn := fmt.Sprintf("%s/problems/%s", api.LeetcodeCn, today.Question.TitleSlug)

	content := fmt.Sprintf(msgTemplate, date, title, tagsValue, link, linkCn)

usage

  • fork 此仓库
  • Secrets Actions 中添加 SLACK_URL 为对应的 token 即可

在这里插入图片描述

  • 可自行拓展其他的通知方式

效果图

在这里插入图片描述

acknowledgement

  • graphql
  • leetcode
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值