I wrote a script hanging in the Node environment: “Gold Nuggets no check-ins & Free raffle! Three minutes to create an automatic check-ins & raffle script running on the server”, xD wants a cloud version in the comments section. I haven’t studied cloud functions before, so take this opportunity to see how this thing works.

The script

Without more words, first on the complete version of the script, if you will create their own cloud function, directly copy the past, cookie and mailbox pass change, open a scheduled task trigger.

  • Note: The final cloud function SRC should have 4 files:
  1. package.json
  2. package-lock.json
  3. node_modules
  4. index.js

package.json

{
  "name": "juejin"."version": "1.0.0"."main": "index.js"."dependencies": {
    "axios": "^ 0.21.4"."nodemailer": "^ 6.6.3." "}}Copy the code

index.js

"use strict"

/ * -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- rely on the -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
const nodeMailer = require('nodemailer');
const axios = require('axios');

/ * -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- configuration -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
const config = {
    "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": "[email protected]"."from": "[email protected]"."to": "[email protected]"."pass": ""}}}/ * -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the Denver nuggets -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /

/ / sign in
const checkIn = async() = > {let {error, isCheck} = await getTodayCheckStatus();
    if (error) return console.log('Query check-in failed');
    if (isCheck) return console.log('Signed in today');
    const {cookie, baseUrl, apiUrl} = config;
    let {data} = await axios({url: baseUrl + apiUrl.checkIn, method: 'post'.headers: {Cookie: cookie}});
    if (data.err_no) {
        console.log('Check-in failed');
        await sendEmailFromQQ('Nuggets of the Day check in: Failed'.JSON.stringify(data));
    } else {
        console.log('Successful sign-in! Current integral:${data.data.sum_point}`);
        await sendEmailFromQQ('Nuggets of the Day Check in: Success'.JSON.stringify(data)); }}// Check whether you have checked in today
const getTodayCheckStatus = async() = > {const {cookie, baseUrl, apiUrl} = config;
    let {data} = await axios({url: baseUrl + apiUrl.getTodayStatus, method: 'get'.headers: {Cookie: cookie}});
    if (data.err_no) {
        await sendEmailFromQQ('Today's nuggets check-in query: failed'.JSON.stringify(data));
    }
    return {error: data.err_no ! = =0.isCheck: data.data}
}

/ / draw
const draw = async() = > {let {error, isDraw} = await getTodayDrawStatus();
    if (error) return console.log('Failed to query lucky draw count');
    if (isDraw) return console.log('No more free lucky draw today');
    const {cookie, baseUrl, apiUrl} = config;
    let {data} = await axios({url: baseUrl + apiUrl.drawLottery, method: 'post'.headers: {Cookie: cookie}});
    if (data.err_no) return console.log('Free draw failed');
    console.log('Congratulations on drawing:${data.data.lottery_name}`);
}

// Get the number of times today's free draw
const getTodayDrawStatus = async() = > {const {cookie, baseUrl, apiUrl} = config;
    let {data} = await axios({url: baseUrl + apiUrl.getLotteryConfig, method: 'get'.headers: {Cookie: cookie}});
    if (data.err_no) {
        return {error: true.isDraw: false}}else {
        return {error: false.isDraw: data.data.free_count === 0}}}/ * -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- email -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /

// Send by qq mailbox
const sendEmailFromQQ = async (subject, html) => {
    let cfg = config.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 console.log('Failed to send mail:${err}`.true);
        console.log('Email sent successfully')})}exports.juejin = async (event, context) => {
    console.log('开始');
    await checkIn();
    await draw();
    console.log('the end');
};
Copy the code

steps

Step 1: Get cookies

Nuggets official website -> Developer tools -> Document. Cookie, cookie out. Then copy it into the cookie of the script.

Step 2: Get the QQ email pass

Open QQ mail – > Settings – > account – > POP3 / IMAP/SMTP/Exchange/CardDAV/CalDAV service – > generate the authorization code; Then copy it to the email.qq.pass property and change the user, from, and to to your own.

Step 3: Open Tencent cloud function console

console.cloud.tencent.com/scf

1. Create a function

2. Basic configuration

3. Trigger configuration

0 0 1 * * * * stands for one o ‘clock every day. The rules can be found here in the Cron documentation

4. Save and install dependencies

Function Management -> Function code -> Code Editing -> Terminals -> Install dependencies

  • This should be the most problematic, because I didn’t bring package.json😂 when I first posted this article
  1. Cut to the directorysrcThe following
cd ./src
Copy the code
  1. Install dependencies (if package.json is already in the directory)
npm install
Copy the code

5. Test to see if the script works

6, is done ~ ~

Point a praise