Node script realizes automatic sign-in and lottery function

Node script realizes automatic sign-in and lottery function

1. Introduction

Since launching the sign-in activity, Nuggets has been continuously improving this function. Now, ores can be used for raffles and exchanged for items (it’s so cool! ✧*。٩(ˊᗜˋ*)و✧*。). The author has been using Nuggets for a long time (before the Nuggets sign-in function came out), but he is lazy and his sign-ins are sporadic, so he can only watch others exchange prizes (envy, jealousy٩(๑`^´๑)۶). Now the ore has not passed w (mainly relying on luck ( • ̀ω•́ )✧), and it happened that some Nuggets bloggers posted articles about automatic sign-in not long ago, and I think this is a good idea, so I took advantage of the New Year’s Day to make a good use of it, because I bought a cheap Tencent cloud server not long ago and I am a front-end novice, so I decided to use the cloud server plus node timing script to realize the automatic sign-in and lottery function. After deciding on the direction, I started looking for articles, so I directly searched for the keyword自動簽到, started reading articles one by one, and then started to implement it myself. Without further ado, let's see how to implement automatic sign-in and lottery.

2. Preparation

server

I have read a lot of articles, and basically they all use a request package and a scheduled task trigger package to implement it. I have also seen some that use Treasure Monkey scripts, cloud functions, and node scripts, which basically cover everything. But no matter what is used, the idea and the calling interface are the same, so this article is based on the cloud server, which requires a cloud server or a computer connected to the Internet and not shut down 24 hours a day.

Node environment

I won't say much about this. The node environment installation is available online, but I believe everyone must have installed it. After tidying up the environment, we started to build the project.

3. Script Project Construction

Create a folder and write the project name (give it a random name ( ̄▽ ̄)/);

Type cmd

Then open the dos window (cmd) under the folder, or use VsCode to open the folder;

Open DOS

Type npm init in the window and press Enter to generate the package.json file;

Initialize the project

Then prepare two packages here, one is axios and the other is node-schedule , install them as follows:

 npm i axios
npm i node-schedule

After the installation is completed, the following figure is shown;

Installing Packages

Then create index.js and config.js files in the root directory for code and parameter writing;

File Addition

At this point, the entire node script project file has been built, and the next step is code writing.

4. Code Writing & Running

First extract the parameters used into a file ( config.js )

 //config.js
//Query whether the sign-in is successful today. API: https://api.juejin.cn/growth_api/v1/get_today_status
module.exports = {
  // Nuggets related parameters nuggets: {
    signInUrl: `https://api.juejin.cn/growth_api/v1/check_in`, //Sign-in interface freeCheckUrl: `https://api.juejin.cn/growth_api/v1/lottery_config/get`, //Free lottery number query drawUrl: `https://api.juejin.cn/growth_api/v1/lottery/draw`, //Lottery interface headers: {
      Referer: "https://juejin.cn/",
      "Upgrade-Insecure-Requests": 1,
      "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36",
      Cookie: `Fill in your own cookie here, log in to the Nuggets web version, open the console's network, find a request at random, check the cookie in the request header and copy it`, //Use your own}, //Related request headers},
  //Message push related parameters Follow the pushplus WeChat public account to get the one-to-one push call parameters, not to promote pushPlus: {
    url: `http://www.pushplus.plus/send`, //WeChat push URL
    token: `This is the token obtained from PushPlus. You can get the token by following the official account and then opening the official website to find the one-to-one push.`, //No ads, this is free}
}

The contents that need to be modified in this file are mainly cookie and token , which are used for Nuggets interface requests and wx message push respectively. If you do not need to use wx message push, just add a line of return; to the first line of the pushMsg function.

Here cookie log in to the PC version of the Nuggets directly, press F12 to open the console, go to network, and then click on some interactions on the page to capture the request, then find cookie in it and copy it, as shown in the figure below.

Get cookies

To obtain token for wx message push, just search for the pushplus official account on wx, follow it and activate the message push, then go to the official website to get the token and add it to the file (this is not an advertisement! I learned this from a blog post by a big shot, it’s quite useful─━ _ ─━✧).

Finally, write the code ( index.js )

I will briefly pick out a part of it here. The whole code is too much to put up here, so I will mainly put the part about sign-in and scheduled tasks. I will upload the whole project to Github and Gitee, and everyone is welcome to download and study (if possible, give a star (・ω<)☆).

 //Package file needed const axios = require("axios");
const schedule = require("node-schedule");
//Related parameters are saved in the file const { nuggets, pushPlus } = require("./config");

/**
 * Get the formatted time of the current time * @param {String} key Call js date function string * @returns The formatted string of the current time */
const getNowTime = (key) => {
  let nowTime = ``;
  try {
    nowTime = new Date()[key]();
  } catch (e) {
    nowTime = `Error in getting time function! `;
    console.error(`Please pass in the date function——${e}`);
  }
  return nowTime;
}

/**
 * Nuggets automatic sign-in request method */
const hacpaiSignRequest = async () => {
  console.log(`\n\n------${getNowTime(`toLocaleDateString`)} - Start signing in------\n`);
  const { headers, signInUrl } = nuggets; //Sign-in related parameters const res = await axios({
    url: signInUrl,
    method: `post`,
    headers,
  });
  if (res && res.data) {
    let jsonMsg = JSON.stringify(res.data);
    console.log(`\n ${jsonMsg} \n \n ------ ${getNowTime(`toLocaleTimeString`)} Sign in successfully------\n`);
    pushMsg(`Nuggets sign-in result`, res.data); //Push message after successful sign-in //After successful sign-in, check the number of free draws within 30 seconds setTimeout(() => {
      freeCheck();
    }, Math.random() * 30 * 1000)
  } else {
    console.log(res);
    console.log(`\n ------ ${getNowTime(`toLocaleTimeString`)} Sign-in failed------ \n`);
    pushMsg(`Nuggets sign-in result`, { 'Sign-in failed': res.data }); //Push message after successful sign-in}
}

//Timed trigger task const signTask = () => {
  //Randomly sign in between 6:00-6:10 every day schedule.scheduleJob("0 0 6 * * *", () => {
    setTimeout(() => {
      hacpaiSignRequest(); //Sign-in function}, Math.random() * 10 * 60 * 1000)
  })
}

//Start executing the task console.log(`Start executing the task - ${getNowTime('toLocaleString')}`);
signTask();

The code here cannot be used directly because some functions are not put in. The logic written here is mainly sign-in -> query the number of free lottery draws -> lottery draw. This process is randomly triggered at any time between 6:00-6:10 every day (I am afraid that it will be hard-coded and be regarded as a robot!!!∑(゚Д゚ノ)ノ). The code is quite easy to write. It mainly involves sorting out the demand logic and then finding the relevant interfaces. Finally, no matter what request package is used, as long as it is an http request, these functions can be completed.

Finally, enter node index.js in the cmd window to execute the code, and then just run it in a stable environment (I directly throw it on the server٩(๑❛ᴗ❛๑)۶).

This article mainly uses wx message push. If you have any needs for email message push, you can tell me in the comment area. I can write another article about using node for email message push later. Thank you for your support! (Just write what you want to write! And say it out loud! ヾ(•ω•`。))

5. Summary and what I want to say

At first I thought this might be a little difficult to do, but after actually trying it I found that it is not that difficult to write. It mainly requires overcoming my own psychological barriers, laziness and unwillingness to do it. I hope to get back the feeling I had in college. No matter whether I can do it or not, I will just bite the bullet and do it. There will always be a way to solve it. It will just take more time. I believe that only in this way can I make myself grow. Keep it up! Little D! And to those of you who are reading this blog, please also work hard together! ( ̄▽ ̄)/

6. Related source code and reference blogs

source code

gitee AutomaticCheck-inJueJin

github: AutomaticCheck-inJueJin

Blogs of the masters of the articles I have borrowed (in no particular order (*❦ω❦)):

Nuggets avoid missing out on signings! Automatic sign-in & automatic free draw

One hundred lines of code to achieve it! Cloud Function Version of Nuggets Automatic Sign-in & Lucky Draw & Email Reminder~

🎉 A few lines of code to achieve automatic sign-in + WeChat push, no longer worry about missing sign-in

Cloud function regularly triggers the gold mining community: automatic sign-in, automatic free lottery🔥🔥

Nuggets always forget to sign in? Node automatically signs in to help you

This is the end of this article about how to use node script to realize automatic sign-in and lottery functions. For more information about node automatic sign-in and lottery, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of nodejs WeChat public account development——4. Automatically reply to various messages
  • Detailed explanation of how nodejs express automatically generates project framework
  • How to configure webstorm to support nodejs and automatically complete
  • Node automated deployment method
  • Node.js automatic FTP upload script sharing
  • How to build docker+jenkins+node.js automated deployment environment from scratch
  • Tool for automatically converting JSON files to Excel using nodejs (recommended)

<<:  A brief analysis of the count tracking of a request in nginx

>>:  How to display and format json data on html page

Recommend

Several implementation methods of the tab bar (recommended)

Tabs: Category + Description Tag bar: Category =&...

Issues with using Azure Container Registry to store images

Azure Container Registry is a managed, dedicated ...

SMS verification code login function based on antd pro (process analysis)

Table of contents summary Overall process front e...

JavaScript style object and CurrentStyle object case study

1. Style object The style object represents a sin...

Pure CSS to achieve three-dimensional picture placement effect example code

1. Percentage basis for element width/height/padd...

Analysis of common usage examples of MySQL process functions

This article uses examples to illustrate the comm...

Basic HTML directory problem (difference between relative path and absolute path)

Relative path - a directory path established based...

Quickly master how to get started with Vuex state management in Vue3.0

Vuex is a state management pattern developed spec...

Docker creates MySQL explanation

1. Download MySQL Image Command: docker pull mysq...

Optimization of MySQL thread_stack connection thread

MySQL can be connected not only through the netwo...

Detailed installation and use of RocketMQ in Docker

To search for RocketMQ images, you can search on ...

How to set static IP for Ubuntu 18.04 Server

1. Background Netplan is a new command-line netwo...