So after I created a scheduler, I was thinking what is that I can automate next. I had been following people who follow me on good morning account. Also wanted to unfollow who first follow and then unfollow me. Going through the documentation , I thought this isn’t going to be a big task. However, after various failed attempts I was still not able to follow people using R. Finally decided to check on with some other languages to understand how it is done. Found an interesting article on calling Python from R. So I was all set with all the raw materials, now the only task remaining was to tune this example according to my requirement.
So the first task was to follow the people who follow me. To decide whom to follow I decided to find the difference between the people who follow me (followers) and the people whom I follow (friends). This was simple,
wakeupUser <- getUser("wakeupwithsmile") followpeople <- setdiff(wakeupUser$getFollowerIDs(), wakeupUser$getFriendIDs())
So, I now have the list of people (ID’s) whom I need to follow. Similarly, I want to create a list of people whom I want to unfollow.
unfollowpeople <- setdiff(wakeupUser$getFriendIDs(), wakeupUser$getFollowerIDs())
If the followpeople and unfollowpeople are not empty then we send a request with the arguments as parameters. Command is the command which we want to execute, path2script is the path of the python file which we want to execute. For every id to follow we can call this python script to follow a person with specific id.
output = system2(command, args=allArgs, stdout=TRUE)
Whatever you print in the python code gets stored in output here. So for example, we have id = 125422 and 857444 to follow then the two system calls would be
python D:/create_friendships.py 125422
python D:/create_friendships.py 857444
Similar thing is done for unfollowpeople as well.
Now coming to the python code. Although we have authorized twitter in R, we need to do it again in python as these two are different environments. There is a special twitter library for Python called tweepy which I am using here. The python code is simple basically we just create an object and use the create_friendship function with the specified id to follow which is passed from the console.
Similarly, we do it for unfollowing people as well using the destroy_friendship’ function.