掘金免漏签&免费抽奖!三分钟搞一个跑在服务端的自动签到&抽奖脚本~

·  阅读 1932
掘金免漏签&免费抽奖!三分钟搞一个跑在服务端的自动签到&抽奖脚本~

实现

  • 自动签到
  • 自动免费抽奖
  • 签到结果发送邮件

git地址:github.com/zhuyuqian/n…

脚本

index.js

const Cron = require('cron').CronJob;
const {log} = require('./util.js');
const juejin = require('./juejin.js');

let job = new Cron('0 1 * * *', async () => {
    await juejin.checkIn();
    await juejin.draw();
}, null, false)

log('开始运行')

job.start();
复制代码

juejin.js

const axios = require('axios');
const {log, getConfig} = require('./util.js');
const {sendEmailFromQQ} = require('./email.js')

// 获取今天免费抽奖的次数
const getTodayDrawStatus = async () => {
    const {cookie, baseUrl, apiUrl} = getConfig();
    let {data} = await axios({url: baseUrl + apiUrl.getLotteryConfig, method: 'get', headers: {Cookie: cookie}});
    return {error: data.err_no !== 0, isDraw: data?.data?.free_count === 0};
}

// 抽奖
const draw = async () => {
    let {error, isDraw} = await getTodayDrawStatus();
    if (error) return log('查询抽奖次数失败', true);
    if (isDraw) return log('今日已无免费抽奖次数', true);
    const {cookie, baseUrl, apiUrl} = getConfig();
    let {data} = await axios({url: baseUrl + apiUrl.drawLottery, method: 'post', headers: {Cookie: cookie}});
    if (data.err_no) return log('免费抽奖失败', true);
    log(`恭喜抽到:${data.data.lottery_name}`);
}

// 查询今日是否已经签到
const getTodayCheckStatus = async () => {
    const {cookie, baseUrl, apiUrl} = getConfig();
    let {data} = await axios({url: baseUrl + apiUrl.getTodayStatus, method: 'get', headers: {Cookie: cookie}});
    if (data.err_no) {
        await sendEmailFromQQ('今日掘金签到查询:失败', JSON.stringify(data));
    }
    return {error: data.err_no !== 0, isCheck: data.data}
}

// 签到
const checkIn = async () => {
    let {error, isCheck} = await getTodayCheckStatus();
    if (error) return log('查询签到失败', true);
    if (isCheck) return log('今日已参与签到', true);
    const {cookie, baseUrl, apiUrl} = getConfig();
    let {data} = await axios({url: baseUrl + apiUrl.checkIn, method: 'post', headers: {Cookie: cookie}});
    if (data.err_no) {
        log('签到失败', true);
        await sendEmailFromQQ('今日掘金签到:失败', JSON.stringify(data));
    } else {
        log(`签到成功!当前积分:${data.data.sum_point}`);
        await sendEmailFromQQ('今日掘金签到:成功', JSON.stringify(data));
    }

}

module.exports = {
    checkIn,
    draw
}
复制代码

config.json

{
  "baseUrl": "https://api.juejin.cn",
  "apiUrl": {
    "getTodayStatus": "/growth_api/v1/get_today_status",
    "checkIn": "/growth_api/v1/check_in",
    "getLotteryConfig": "/growth_api/v1/lottery_config/get",
    "drawLottery": "/growth_api/v1/lottery/draw"
  },
  "cookie": "",
  "email": {
    "qq": {
      "user": "416556102@qq.com",
      "from": "416556102@qq.com",
      "to": "416556102@qq.com",
      "pass": ""
    }
  }
}
复制代码

email.js

const nodeMailer = require('nodemailer');
const {getConfig, log} = require('./util.js');

/**
 * @description 通过qq邮箱发送
 * @param {string} subject 邮件标题
 * @param {string} html 邮件内容
 * @date: 2021/9/18
 */
const sendEmailFromQQ = async (subject, html) => {
    let cfg = getConfig()?.email?.qq;
    if (!cfg || !cfg.user || !cfg.pass) return;
    const transporter = nodeMailer.createTransport({service: 'qq', auth: {user: cfg.user, pass: cfg.pass}});
    transporter.sendMail({
        from: cfg.from,
        to: cfg.to,
        subject: subject,
        html: html
    }, (err) => {
        if (err) return log(`发送邮件失败:${err}`, true);
        log('发送邮件成功')
    })
}

module.exports = {
    sendEmailFromQQ
}
复制代码

util.js

const dayjs = require('dayjs');
const chalk = require('chalk');
const fs = require('fs');
const configPath = './config.json';

// 打印
const log = (message, isError = false) => {
    if (isError) {
        console.log(chalk.red(dayjs().format('YYYY-MM-DD HH:mm:ss'), message));
    } else {
        console.log(chalk.blue(dayjs().format('YYYY-MM-DD HH:mm:ss'), message));
    }
}

// 获取配置
const getConfig = () => {
    return JSON.parse(fs.readFileSync(configPath));
}

module.exports = {
    log,
    getConfig
}
复制代码

使用

1、修改config.json内cookie参数

如何从浏览器获取cookie

2、修改config.json内email.qq参数

{
  "email": {
    "qq": { // 目前使用QQ邮箱的
      "user": "", // 开启SMTP服务的QQ邮箱号
      "from": "", // 开启SMTP服务的QQ邮箱号
      "to": "", // 接收者的邮箱
      "pass": "" // 授权码(不知道如何获取可自行百度)
    }
  }
}
复制代码

2、安装pm2

npm install -g pm2
复制代码

3、使用pm2启动项目

pm2 start index.js --name ***
复制代码

git地址

github.com/zhuyuqian/n…

分类:
前端
标签: