Last active
May 4, 2019 20:54
-
-
Save FlyInk13/9ce0336c4282fd3bdff80cfc0a92a327 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var request = require('request-promise'); | |
| var Events = require('events'); | |
| var emitter = new Events(); | |
| var endpoint = 'https://t.me/spbmetro/'; | |
| var last_mid = 2288; | |
| var pending = 0; | |
| function parseMessage(id) { | |
| return request(endpoint + id).then((res) => { | |
| var body = res.toString(); | |
| var start = '<meta property="og:description" content="'; | |
| var startPos = body.indexOf(start) + start.length; | |
| var endPos = body.substr(startPos, 4000).indexOf('">'); | |
| var message = body.substr(startPos, endPos); | |
| if (/View In Channel/.test(body)) { | |
| return { id, message }; | |
| } else { | |
| throw { code: 404, res: body }; | |
| } | |
| }); | |
| } | |
| function findMessage(message_id) { | |
| return parseMessage(message_id).catch((e) => { | |
| if (e && e.code !== 404) { | |
| throw e; | |
| } | |
| return parseMessage(message_id + 1); | |
| }).catch((e) => { | |
| if (e && e.code !== 404) { | |
| throw e; | |
| } | |
| return parseMessage(message_id + 2); | |
| }); | |
| } | |
| setInterval(() => { | |
| if (pending) return; | |
| pending = findMessage(last_mid).then(({ message_id, description }) => { | |
| last_mid = message_id + 1; | |
| emitter.emit('update', { message_id, description }); | |
| pending = false; | |
| }).catch((e) => { | |
| if (e.code !== 404) { | |
| console.error(e); | |
| } | |
| pending = false; | |
| }); | |
| }, 1000); | |
| emitter.on('update', (event) => { | |
| console.log(event); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment