Automate facebook comments and likes
Well, it was my birthday recently, and my facebook wall was flooded by people wishing me. I don’t really use facebook, but wanted to reply to them all. Manually? Never. 😛
So, lets get to search something that can be used. Came across, FB graph API, and that is exactly what was needed. 🙂
A challenge in place, how to not make people realize what I did? Hmm… All posts shouldn’t be identical. I didn’t have much of ideas so generated my posts using this-
messages = ["Thank you %s", "Thanks a lot %s", "Thanks %s"]; # %s The user's first name goes after each string
A nice little trick, isn’t it 😛
For accessing and posting to facebook, first you will need to generate an access token here. You will need to accept some permissions. Basic permissions allow you to only read info while you need to select permissions if you want to post using your account.
Let’s start by defining timestamps for starting and ending to read posts. Using the time library in python
AFTER = time.mktime(time.strptime( '01/01/2014',"%d/%m/%Y") ) BEFORE = time.mktime(time.strptime( '02/01/2014',"%d/%m/%Y") )
As you would have guessed, 01/01/2014 isn’t my birthday 😛
We need to convert the timestamps to such that we can pass it to the graph API. Basically, we need it in integer format(no decimal places)
FB_START = str( int( AFTER ) ) FB_END = str( int( BEFORE ) ) # Converting to strings as we need to concatenate this in the query
Here is the snippet that reads all posts. I am using Python requests for this purpose
query = ( "SELECT post_id, actor_id, message FROM stream WHERE " "filter_key = 'others' AND source_id = me() AND " "created_time>" + FB_START + " AND created_time<" + FB_END + " LIMIT 400" ); # The query gives out PostId and ActorId for all posts on my wall(other than by me) # I filtered out the time using AFTER and before timestamps payload = {'q': query, 'access_token': TOKEN} response = requests.get('https://graph.facebook.com/fql', params=payload) # Encode the parameters and get the response
The response is in JSON format, we just convert it into text in our case using
result = json.loads(response.text)
The result is an array of all these JSON objects(converted to strings). We will process over each post one by one and post a message and like each of them.
Commenting on posts
The way it is done is(specified in documentation)
Url: 'https://graph.facebook.com/{POST_ID}/comments; Payload = { 'access_token': TOKEN, 'message': MESSAGE };
Liking posts
Url: 'https://graph.facebook.com/{POST_ID}/likes; Payload = { 'access_token': TOKEN, };
Having described this, let’s have a look at the code snippet which finally uses all this:
for post in result: # Load info about user response = requests.get( 'https://graph.facebook.com/%s' % post['actor_id'] ) # Convert it to string user = json.loads(response.text); # Generate a message for each user from the list of messages # and add the poster's name into it message = random.choice( messages ) % user['first_name']; # Comment on each post url = 'https://graph.facebook.com/%s/comments' % post['post_id']; payload = { 'access_token': TOKEN, 'message': message }; s = requests.post( url, data = payload ); # 'print s' should be Response 200 in case of success, error response otherwise # Similarly, Like each post url = 'https://graph.facebook.com/%s/likes' % post['post_id']; payload = { 'access_token': TOKEN } s = requests.post(url, data=payload);
Done!! Now see the magic happen!
This isn’t it, a lot more can be done using Facebook graph API. Here is the documentation https://developers.facebook.com/docs/graph-api/using-graph-api/v2.0
Wonderful
Learned a new technique. Graet work