Advertisement
Guest User

Untitled

a guest
Jun 20th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const mysql = require('mysql');
  2.  
  3. const pool = mysql.createPool({
  4.     host: 'localhost',
  5.     user: 'user',
  6.     password: 'pass',
  7.     database: 'db_name'
  8. });
  9.  
  10. pool.connect(function (err) {
  11.     if (err) {
  12.         throw err;
  13.     }
  14.     console.log("SQL Connected!");
  15. });
  16.  
  17. const platformFollowersParsers = {
  18.     'YouTube': function () {
  19.         const serviceId = 5;
  20.         var flag = true;
  21.  
  22.         if (flag) {
  23.             flag = false;
  24.             getPlatfromChannelsForParsing(serviceId)
  25.                 .then(parseYouTubeChannels)
  26.                 .then(writeResultToDB)
  27.                 .then(shouldContinue => {
  28.                     if (!shouldContinue) {
  29.                         flag = false;
  30.                         getOldestChangedChannel(serviceId, function (errorFromDB, oldestChannel) {
  31.                             if (errorFromDB) {
  32.                                 console.log(errorFromDB);
  33.                                 // if error try in 60minutes
  34.                                 waitMilliseconds(1000 * 60 * 60, () => flag = true);
  35.                             } else {
  36.                                 // try in 24hours after oldest change
  37.                                 var dayAfterOldestChange = (oldestChannel['last_change_followers_count'] -
  38.                                                             Date.now()) + 1000 * 60 * 60 * 24;
  39.                                 waitMilliseconds(dayAfterOldestChange, () => flag = true);
  40.                             }
  41.                         })
  42.                     }
  43.                 })
  44.                 .catch((error) => {
  45.                     flag = false;
  46.                     console.log(error);
  47.                     // if error try in 60minutes
  48.                     waitMilliseconds(1000 * 60 * 60, () => flag = true);
  49.                 });
  50.         }
  51.  
  52.     },
  53.  
  54.     'Twitch': function () {
  55.         const serviceId = 6;
  56.     }
  57. };
  58.  
  59. function writeResultToDB(channelsWithFollowers) {
  60.  
  61.     return new Promise(function (resolve, reject) {
  62.  
  63.         if (channelsWithFollowers.length) {
  64.             // write followers to DB
  65.             resolve(true);
  66.         } else {
  67.             resolve(false)
  68.         }
  69.     })
  70. }
  71.  
  72. function waitMilliseconds(milliseconds, callbackForFlagChange) {
  73.     setTimeout(callbackForFlagChange, milliseconds);
  74. }
  75.  
  76. function parseYouTubeChannels(channels, callbackForFlagChange) {
  77.  
  78.     return new Promise(function (resolve, reject) {
  79.  
  80.         var index = 0;
  81.         var iterations = channels.length;
  82.         var channelsWithFollowers = [];
  83.  
  84.         function next() {
  85.             if (index < iterations) {
  86.                 index++;
  87.                 getFollowers(channels[index], function (err, result) {
  88.                     if (err) {
  89.                         result.push({
  90.                             'channel': channels[index],
  91.                             'followers': null
  92.                         })
  93.                     } else {
  94.                         result.push({
  95.                             'channel': channels[index],
  96.                             'followers': result
  97.                         })
  98.                     }
  99.                     next();
  100.                 });
  101.             } else {
  102.                 resolve(channelsWithFollowers);
  103.             }
  104.         }
  105.     });
  106. }
  107.  
  108. function getOldestChangedChannel(serviceId, callback) {
  109.     //'SELECT * FROM user_stream_settings WHERE service_id = serviceId ORDER BY
  110.     // `last_change_followers_count` ASC LIMIT 1'
  111.     callback(null, {'last_change_followers_count': 123123123123})
  112. }
  113.  
  114. function getPlatfromChannelsForParsing(platformId, callback) {
  115.  
  116.     return new Promise(function (resolve, reject) {
  117.  
  118.         pool.getConnection(function (err, connection) {
  119.             if (err) {
  120.                 return reject(err);
  121.             }
  122.             var dayAgo = Date.now() - 1000 * 60 * 60 * 24;
  123.             connection.query('SELECT * FROM user_stream_settings WHERE service_id = ? ' +
  124.                              'AND `last_change_followers_count` > ? ASC LIMIT ?', [
  125.                 platformId,
  126.                 dayAgo,
  127.                 channelsLimit
  128.             ], function (err, channelsForParsing) {
  129.                 connection.release();
  130.                 if (err) {
  131.                     reject(err);
  132.                 }
  133.                 resolve(channelsForParsing);
  134.             });
  135.         });
  136.     });
  137. }
  138.  
  139. setInterval(() => {
  140.     for (let platform in platformFollowersParsers) {
  141.         platformFollowersParsers[platform]();
  142.     }
  143. }, 1000 * 60);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement