Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function makeRequest(url, callback) {
- request({
- url: url,
- json: true
- }, function follow(error, response, username) {
- if (!error && response.statusCode === 200) {
- callback(username);
- }
- });
- }
- var last_showed = "";
- setTimeout(function () {
- connection.query("SELECT * FROM action_log WHERE source='twitch' AND action='follow' ORDER BY id DESC LIMIT 1", function (error, results, fields) {
- last_showed = results[0].username;
- //console.log(last_showed);
- });
- }, 10 * 1000);
- function followers(url) {
- request({
- url: url,
- json: true
- }, function (error, response, followerList) {
- if (!error && response.statusCode === 200) {
- followerList = followerList.follows;
- //Loop followerList and add username to nameList array.
- //Uses negative for loop to reorder followers
- var nameList = [];
- for (var i = followerList.length - 1; i >= 0; i--) {
- nameList.push(followerList[i].user.name);
- };
- //Removes old follower names including last_showed
- var newFollowers = nameList.slice(nameList.indexOf(last_showed) + 1);
- //Loops any remaining names and emits each name
- //Also sets last_showed
- for (var i = 0; i < newFollowers.length; i++) {
- last_showed = newFollowers[i];
- //
- var post_follow = { username: last_showed, action: "follow", source: "twitch", comment: last_showed + " has followed the channel" };
- connection.query('INSERT INTO action_log SET ?', post_follow, function (err, result) { });
- io.emit("new-action", last_showed, "follow", "F");
- //this will emit each name one at a time but all at once.
- //you will need to find a way to delay the showing of each notification.
- //console.log(newFollowers[i]);
- }
- }
- //Repeats every 30 secs
- //No reason to do more then 30 secs cause the api is cached
- setTimeout(followers, 30 * 1000, url);
- });
- }
- //initial call
- setTimeout(function () {
- makeRequest(url, function follow(follow) {
- 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) {
- if (error) {
- console.log(error);
- }
- if (results.length > 0) {
- console.log('fail');
- } else {
- var post = { username: follow.follows[0].user.name, action: "follow", source: "twitch", comment: follow.follows[0].user.name + " has followed the channel" };
- connection.query('INSERT INTO action_log SET ?', post, function (err, result) { });
- }
- });
- });
- }, 2000);
- setTimeout(function () {
- followers(url);
- }, 20 * 1000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement