Advertisement
Guest User

Untitled

a guest
Aug 17th, 2015
211
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. function makeRequest(url, callback) {
  3.   request({
  4.     url: url,
  5.     json: true
  6.   }, function follow(error, response, username) {
  7.     if (!error && response.statusCode === 200) {
  8.       callback(username);
  9.     }
  10.   });
  11. }
  12.  
  13. var last_showed = "";
  14. setTimeout(function () {
  15.   connection.query("SELECT * FROM action_log WHERE source='twitch' AND action='follow' ORDER BY id DESC LIMIT 1", function (error, results, fields) {
  16.     last_showed = results[0].username;
  17.     //console.log(last_showed);
  18.   });
  19. }, 10 * 1000);
  20.  
  21. function followers(url) {
  22.   request({
  23.     url: url,
  24.     json: true
  25.   }, function (error, response, followerList) {
  26.     if (!error && response.statusCode === 200) {
  27.       followerList = followerList.follows;
  28.       //Loop followerList and add username to nameList array.
  29.       //Uses negative for loop to reorder followers
  30.       var nameList = [];
  31.       for (var i = followerList.length - 1; i >= 0; i--) {
  32.         nameList.push(followerList[i].user.name);
  33.       };
  34.       //Removes old follower names including last_showed
  35.       var newFollowers = nameList.slice(nameList.indexOf(last_showed) + 1);
  36.  
  37.       //Loops any remaining names and emits each name
  38.       //Also sets last_showed
  39.       for (var i = 0; i < newFollowers.length; i++) {
  40.  
  41.         last_showed = newFollowers[i];
  42.         //
  43.         var post_follow = { username: last_showed, action: "follow", source: "twitch", comment: last_showed + " has followed the channel" };
  44.         connection.query('INSERT INTO action_log SET ?', post_follow, function (err, result) { });
  45.         io.emit("new-action", last_showed, "follow", "F");
  46.         //this will emit each name one at a time but all at once.
  47.         //you will need to find a way to delay the showing of each notification.
  48.        
  49.         //console.log(newFollowers[i]);
  50.        
  51.       }
  52.     }
  53.     //Repeats every 30 secs
  54.     //No reason to do more then 30 secs cause the api is cached
  55.     setTimeout(followers, 30 * 1000, url);
  56.   });
  57. }
  58.  
  59. //initial call
  60. setTimeout(function () {
  61.   makeRequest(url, function follow(follow) {
  62.     connection.query("SELECT 1 FROM action_log WHERE username = '" + follow.follows[0].user.name + "' AND source='twitch' AND action='follow' ORDER BY id LIMIT 1", function (error, results, fields) {
  63.       if (error) {
  64.         console.log(error);
  65.       }
  66.       if (results.length > 0) {
  67.         console.log('fail');
  68.       } else {
  69.         var post = { username: follow.follows[0].user.name, action: "follow", source: "twitch", comment: follow.follows[0].user.name + " has followed the channel" };
  70.         connection.query('INSERT INTO action_log SET ?', post, function (err, result) { });
  71.       }
  72.     });
  73.   });
  74. }, 2000);
  75.  
  76. setTimeout(function () {
  77.   followers(url);
  78. }, 20 * 1000);
Advertisement
RAW Paste Data Copied
Advertisement