From c905724a92adbf3c2b30f879ca9cce01c3c432cd Mon Sep 17 00:00:00 2001 From: Kousik Satish Date: Fri, 11 Oct 2019 00:01:47 +0530 Subject: [PATCH] Add basic function to check followers and follow back --- src/tweet.js | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/tweet.js b/src/tweet.js index 978f3aeb..d0866bee 100644 --- a/src/tweet.js +++ b/src/tweet.js @@ -88,10 +88,56 @@ function tweetNow(status){ }); } +/** + * + * @description it checks the list of followers and follows them if we have not already followed them + */ +function followBack(){ + // Get the list of friends (accounts which we follow) + client.get('friends/list', function(error, friendsResult) { + if (friendsResult) { + const friendIdList = []; + friendsResult.users.forEach((friend) => { + friendIdList.push(friend.screen_name); + }); + + // Get the list of followers + client.get('followers/list', function(error, followerResult) { + if (followerResult) { + followerResult.users.forEach((follower) => { + // If we are not already following the account + if (!friendIdList.includes(follower.id)) { + client.post('friendships/create', { + screen_name: follower.screen_name + }, function(error, createResult) { + if (createResult) { + console.log(`Follow back success for ${follower.id} - ${follower.screen_name}`); + } + if (error) { + console.log(`Follow back failed for ${follower.id} - ${follower.screen_name}`); + } + }); + } + }); + } + if (error) { + console.log(`Error occured while getting followers!`); + } + }); + } + if (error) { + console.log(`Error occured while getting friends!`); + } + }); + +} + /** * @description API to fetch random images */ var imageURL = 'https://source.unsplash.com/featured/?motivation'; // encode the image to base64 -encodeImage(imageURL); \ No newline at end of file +encodeImage(imageURL); + +// followBack();