node脚本练习一之掘金社区自动签到

思路:

1、首先查看掘金签到的接口,获取其curl和cookie(curl+exec搭配就可以发送请求)

2、代码思路

js 复制代码
const autoCheckIn = () => {
   try{
   // 发送请求
     const res = await fetchCheckIn()
     if(success){
       // 成功信息发送邮箱
     }else{
     // 失败也发送邮箱
     }
   }catch(err){
   // 失败发送邮箱
   }
}
// 定时发送信息-立即执行函数
(function scheduleLogic() {
  schedule.scheduleJob({ second: 0, minute: 0, hour: 7 }, () => {
    logic();
  });
})();

代码:

js 复制代码
const { execSync } = require("child_process"); // 引入shell
const schedule = require("node-schedule"); // 定时任务
const nodemailer = require("nodemailer"); // 发送邮件

// Create a transporter using SMTP
const transporter = nodemailer.createTransport({
  service: "qq",  // qq邮箱固定
  port: 465, // qq邮箱固定
  secureConnection: true,
  auth: {
    user: "xxxxxxxxxx@qq.com",  // 写你自己的发送邮件号
    pass: "xxxxxxxxxxxxxxxx", // 写你自己的IMAP授权码(在qq邮箱官网可看)
  },
});

/** 写死cookie*/
const myCookie = "xxxxxxxxx"; // 根据curl中的-b提取出来你自己的cookie

/**
 * 进行签到
 * @returns
 */
const checkIn = () => {
  return `curl -s 'https://api.juejin.cn/growth_api/v1/check_in?aid=2608&uuid=752451xxxxxx6&spider=0&a_bogus=dXBmDcxxxxxxxxwy' \
  -H 'accept: */*' \
  -H 'accept-language: en,zh-CN;q=0.9,zh;q=0.8' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -b '${myCookie}' \
  -H 'origin: https://juejin.cn' \
  -H 'pragma: no-cache' \
  -H 'priority: u=1, i' \
  -H 'referer: https://juejin.cn/' \
  -H 'sec-ch-ua: "Not:A-Brand";v="99", "Google Chrome";v="145", "Chromium";v="145"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "macOS"' \
  -H 'sec-fetch-dest: empty' \
  -H 'sec-fetch-mode: cors' \
  -H 'sec-fetch-site: same-site' \
  -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36' \
  -H 'x-secsdk-csrf-token: DOWNGRADE' \
  --data '{}'`;
};

/** 签到函数 */
const logic = async () => {
  console.log("开始签到...");
  try {
    const res = JSON.parse(execSync(checkIn(), { silent: true }));
    console.log("。。。", res);
    if (res.err_no === 0) {
      console.log("签到成功...", res);
      // 签到成功
      await sendEmail({ subject: "掘金签到成功", text: "恭喜你,签到成功!" });
    } else {
      console.log("签到失败", res?.err_msg);
      await sendEmail({ subject: "掘金签到失败", text: res?.err_msg });
    }
  } catch (err) {
    console.log("签到失败");
    // 签到失败
    await sendEmail({ subject: "掘金签到失败", text: `签到失败!${err}` });
  }
};

/** 发送邮件*/
const sendEmail = async (text) => {
  // 先进行验证~
  await transporter.verify();
  console.log("Server is ready to take our messages");
  try {
    const info = await transporter.sendMail({
      from: "xxxxxxxxx@qq.com", // sender address
      to: "xxxxxxxxx@126.com", // list of recipients
      subject: text?.subject, // subject line
      text: text?.text, // plain text body
    });
    console.log("邮件发送成功", info.messageId);
  } catch (err) {
    console.error("邮件发送失败", err);
  }
};

// 定时执行签到-立即执行函数
(function scheduleLogic() {
  schedule.scheduleJob({ second: 0, minute: 0, hour: 7 }, () => {
    logic();
  });
})();
// 测试用*/
// logic();

示例:

相关推荐
木头左11 小时前
【浏览器自动化】用 CDP 接管本机 Chrome:从一个宿主迁到另一个宿主的设计与踩坑
node.js·浏览器自动化·cdp·技能化·踩坑实践
cpolar技术支持1 天前
把 agent-autoloop 装在 Mac 上:用本机 Node 实跑一次「接活—提问—拍板—接着干」,再用 cpolar 把它的状态页开给不在工位的自己
node.js·内网穿透·cpolar·ai agent·agent-autoloop
SEO_juper1 天前
Node.js 全站 SEO 体检报告生成器:一次查完标题、描述、canonical、H1、图片
开发语言·前端·node.js·html·bash
木头左2 天前
【Node.js】Markdown 转带图片 Word 文档实战:docx 库转换器设计与 5 个踩坑
node.js·word·markdown·文档自动化
思特奇时空智能3 天前
大规模点要素地图渲染性能优化实践
vue.js·arcgis·性能优化·node.js·html·gis·webgl
w3296362715 天前
飞书接单到月度结算:我用一个 DSH 插件跑通鞋类采购 WMS,附 22 项验收
pdf·node.js·飞书·后端开发·ai agent
濮水大叔5 天前
Cabloy/Vona 为什么不需要 NestJS 的 Dynamic Module
typescript·node.js·nestjs
匠云创新5 天前
读完一万段要两分钟:我们把标书读取从 WPS 对象模型搬到了 docx 解析
性能优化·node.js
某哈压力大6 天前
SSH 突然 Connection reset?不是密钥坏了,是 OpenSSL 把 sshd 干趴了
前端·node.js·命令行
回眸&啤酒鸭6 天前
【回眸】Node.js 新手从零到一实战指南
node.js