Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const mysql = require('mysql');
- const pool = mysql.createPool({
- host: 'localhost',
- user: 'user',
- password: 'pass',
- database: 'db_name'
- });
- pool.connect(function (err) {
- if (err) {
- throw err;
- }
- console.log("SQL Connected!");
- });
- const platformFollowersParsers = {
- 'YouTube': function () {
- const serviceId = 5;
- var flag = true;
- while (true) {
- if (flag) {
- getPlatfromChannelsForParsing(serviceId)
- .then(parseYouTubeChannels)
- .then(writeResultToDB)
- .then(shouldContinue => {
- if (!shouldContinue) {
- flag = false;
- getOldestChangedChannel(serviceId,
- function (errorFromDB, oldestChannel) {
- if (errorFromDB) {
- console.log(errorFromDB);
- // if error try in 60minutes
- waitMilliseconds(1000 * 60 * 60, () => flag = true);
- } else {
- // try in 24hours after oldest change
- var dayAfterOldestChange = (oldestChannel['last_change_followers_count'] -
- Date.now()) +
- 1000 * 60 * 60 * 24;
- waitMilliseconds(dayAfterOldestChange, () => flag = true);
- }
- })
- }
- })
- .catch((error) => {
- flag = false;
- console.log(error);
- // if error try in 60minutes
- waitMilliseconds(1000 * 60 * 60, () => flag = true);
- });
- }
- }
- },
- 'Twitch': function () {
- const serviceId = 6;
- }
- };
- function writeResultToDB(channelsWithFollowers) {
- return new Promise(function(resolve, reject) {
- if (channelsWithFollowers.length) {
- // write followers to DB
- resolve(true);
- } else {
- resolve(false)
- }
- })
- }
- function waitMilliseconds(milliseconds, callbackForFlagChange) {
- setTimeout(callbackForFlagChange, milliseconds);
- }
- function parseYouTubeChannels(channels, callbackForFlagChange) {
- return new Promise(function (resolve, reject) {
- var index = 0;
- var iterations = channels.length;
- var channelsWithFollowers = [];
- function next() {
- if (index < iterations) {
- index++;
- getFollowers(channels[index], function (err, result) {
- if (err) {
- result.push({
- 'channel': channels[index],
- 'followers': null
- })
- } else {
- result.push({
- 'channel': channels[index],
- 'followers': result
- })
- }
- next();
- });
- } else {
- resolve(channelsWithFollowers);
- }
- }
- });
- }
- function getOldestChangedChannel(serviceId, callback) {
- //'SELECT * FROM user_stream_settings WHERE service_id = serviceId ORDER BY
- // `last_change_followers_count` ASC LIMIT 1'
- callback(null, {'last_change_followers_count': 123123123123})
- }
- function getPlatfromChannelsForParsing(platformId, callback) {
- return new Promise(function (resolve, reject) {
- pool.getConnection(function (err, connection) {
- if (err) {
- return reject(err);
- }
- var dayAgo = Date.now() - 1000 * 60 * 60 * 24;
- connection.query('SELECT * FROM user_stream_settings WHERE service_id = ? ' +
- 'AND `last_change_followers_count` > ? ASC LIMIT ?', [
- platformId,
- dayAgo,
- channelsLimit
- ], function (err, channelsForParsing) {
- connection.release();
- if (err) {
- reject(err);
- }
- resolve(channelsForParsing);
- });
- });
- });
- }
- for (let platform in platformFollowersParsers) {
- platformFollowersParsers[platform]();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement