Advertisement
Guest User

Untitled

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