Seventy-line script to realize Wechat Notification of Promotion Information

Keywords: node.js Mobile JQuery network github

Yesterday, Doudong's LAMY pen was doing another activity, but when it was seen, it had already risen back to its original price. In a fit of anger, I wrote a JS script to automatically query the promotion information and notify me by micro-mail.

Get ready

data acquisition

In data acquisition, we use Node.js to implement a crawler, and regularly access the page refresh, monitoring data at any time.

  • The dependencies used are as follows:

  1. Cheerio: JS Web page parsing, syntax similar to JQuery

  2. Supagent: Network Request Library

  3. system-sleep: Call system sleep

  4. moment: Format the time for easy logging

Message Notification

Wechat informs us that Server sauce Realize, what is this?

Server sauce

What is it?

Server sauce, English name "Server Chan", is a communication software between "programmer" and "server".

Say something reasonable. It's a tool that pushes alarms and logs from servers to mobile phones.

It only takes one minute to open and use it:

Log in: Log in with GitHub account to get a SCKEY (on the Send Message page)
Binding: Click on "Wechat Push" to scan the code and pay attention to it at the same time to complete the binding.
Message: To http://sc.ftqq.com/SCKEY.send By sending GET requests, you can receive messages in Wechat.

Server sauce is so simple to use. After registration and binding, you will get a URL. We only need to send a message to this URL to receive notification in Wechat.

code implementation

var cheerio = require('cheerio');
var superagent = require('superagent')
var sleep = require('system-sleep');
var moment = require('moment')
//URLs
var base = 'http://http://www.baicaio.com/'
var collection = 'http://www.baicaio.com/index-index-type--tab-isnice-dss-cc'
var wechat = 'http://sc.ftqq.com/ ##Your own url.send'
//Oh, Mosiloy, that's the key word of concern.
var omxly = ['Lamy', 'Headset', 'Pen', 'mouse', 'Monitor', 'Computer', 'Mobile phone', 'television']
//href of notified content to prevent multiple notifications of the same content
var notificated = []

function refresh(callback) {
    console.log('Check preferential information  '+ moment().format('MMMM Do YYYY, h:mm:ss a'))
    var items = []
    superagent
        .get(collection)
        .end(function (err, res) {
            var $ = cheerio.load(res.text)
            $('#C_drc ').find('li').each(function (idx, element) {
                $element = $(element)
                var href = $element.find('h2 > a').attr('href')
                var title = $element.find('h2 > a').attr('title')
                var price = $element.find('h2 > a > em').text()
                items.push({
                    href: href,
                    title: title,
                    price: price
                })
            })
            check(items, callback)
        })
}

function check(items, callback) {
    for (index in items) {
        var item = items[index]
        for (word_index in omxly) {
            var flag = true
            var word = omxly[word_index]
            if (String(item.title).indexOf(omxly[word_index]) !== -1) {
                for (href_index in notificated) {
                    if (notificated[href_index] == item.href) {
                        flag = false
                        callback()
                        break
                    }
                }
                if (flag) {
                    console.log('Discover goods of concern')
                    console.log(item.title, item.price)
                    notificated.push(item.href)
                    wechatNotification(word,item.title, item.price, item.href, callback)
                }
            }
        }
    }
}
function wechatNotification(word,title, desc, url, callback) {
    var text = 'master,Found what you like.:_' +
        '' + word
    desc = '['+ title + ']('+ base + url +')\n\n' + desc
    desc += '\n\n'
    desc += 'time:'+moment().format('MMMM Do YYYY, h:mm:ss a')
    superagent
        .get(wechat)
        .query({text: text})
        .query({desp: desc})
        .end(function (err, sres) {
            callback()
        })
}
while (true) {
    refresh({})
    sleep(5 * 1000)
}

Posted by sgs on Thu, 06 Jun 2019 12:26:15 -0700