background

Avengers 4 premiere tickets are already available on eBay for $2500 per ticket. In order to avoid losing tickets, we wrote a script to monitor the release of Avengers 4 and send text messages so that we can grab tickets in the first place, after all, the ticket is the money…

Train of thought

Node writes a grab script to see if the keywords “Avengers” are in any movie for sale, and sends a text message if so

implementation

In order to realize my idea, TWO node libraries, superagent and Cheerio, are used to realize sending request and DOM node analysis respectively.

Set timer

Here, setInterval is used to realize the timing, capturing web data every 60 seconds

function intervalFunc() {
    superagent.get('https://maoyan.com/films?sortId=2').end((err, res) => {
        if (err) {
            console.log(` - ${err}`)}else {
            getFilms(res)
        }
    });
}

var timer = setInterval(intervalFunc, 10000);
Copy the code

Analyzing DOM nodes

We just need to get the div class to be channel-detail

let getFilms = (res) => {
    let $ = cheerio.load(res.text);
    var isOn = false
    $('.channel-detail').each((idx, ele) => {
        if ($(ele).text().indexOf("Revenge") != -1) {
            isOn = true}});if (isOn) {
        // for (const phone of phoneArray) {
          //  sendMessage(phone)
        // }
        sendWx()
        clearInterval(timer)
    } else {
        console.log("Film not released....")}};Copy the code

Here if the monitoring of the movie has been shown remember to turn off the timer ~

Send wechat messages

The wechat service of Server sauce is called here, and you can change the key to your own ~

function sendWx() {
    var http = require('http');
    var key = 'SCU48466Tf3f96d026cbcd4d4ab8f05fa7d04ee2c5cada058e0522'
    var path =  '.send? text='
    var text = 'The Avengers 4 movie is on sale now, so go get your tickets! '

    http.get('http://sc.ftqq.com/' + key + path + text,function(data){
        var str="";
        data.on("data".function(chunk){
            str+=chunk;
        })
        data.on("end".function(){
            console.log("Push sent successfully")})})}Copy the code

Thank you @Rondolo for my advice

Send a text message

function sendMessage(mobile) {
    var http = require('http');
    var username = "";
    var secretkey = "";
    
    var path = "/sms_token? ddtkey=" + username + "&secretkey=" + secretkey
        + "&mobile=" + mobile + "&content=Go"

    var options = {
        host: '112.124.17.46',
        port: 7001,
        path: path,
        method: 'GET',
        headers: {
            'Content-Type': 'application/json'}}; http.get(options,function (res) {
        var resData = "";
        res.on("data".function (data) {
            resData += data;
        });
        res.on("end".function () {
            console.log("The movie is on. Text message is successful.")}); })}Copy the code

conclusion

1. Node packet capture adopts superagent and Cheerio

2. Use clearInterval to disable the timer

Full code address: github.com/ChengRuipen…