Skip to content

Instantly share code, notes, and snippets.

@bbhavsar
Last active December 27, 2015 23:09
Show Gist options
  • Save bbhavsar/7403592 to your computer and use it in GitHub Desktop.
Save bbhavsar/7403592 to your computer and use it in GitHub Desktop.
Python script to automate thank you messages to "Happy Birthday" posts. Get appropriate permissions like read_stream etc. This is Akshit Khurana's script from Quora answer http://qr.ae/pLLZL and includes minor modifications.
import requests
import json
# This is Akshit Khurana's script from Quora answer with minor modifications
# http://qr.ae/pLLZL
# Get your token from https://developers.facebook.com/tools/explorer/
TOKEN = ''
# Get unix epoch time just before your birthday
# http://www.epochconverter.com/
AFTER_TIME = 1383436800
def get_posts():
"""Returns dictionary of id, first names of people who posted on my wall
between start and end time"""
query = ("SELECT post_id, actor_id, message FROM stream WHERE "
"filter_key = 'others' AND source_id = me() "
" AND created_time > 1383436800 LIMIT 200")
print("query %s" % query)
payload = {'q': query, 'access_token': TOKEN}
r = requests.get('https://graph.facebook.com/fql', params=payload)
result = json.loads(r.text)
return result['data']
def commentall(wallposts):
"""Comments thank you on all posts"""
count = 0
for wallpost in wallposts:
print ("%s" % wallpost['message'])
r = requests.get('https://graph.facebook.com/%s' %
wallpost['actor_id'])
user = json.loads(r.text)
fname = user['first_name']
print (fname)
# Add some exceptions if necessary.
if fname == 'Dipak':
fname = 'Papa'
# Some randomness ;)
count = count + 1
if count % 3 == 0:
message = 'Thank you %s! How is it going?' % fname
else:
message = 'Thanks %s!' % fname
print (message)
url = 'https://graph.facebook.com/%s/comments' % wallpost['post_id']
payload = {'access_token': TOKEN, 'message': message}
s = requests.post(url, data=payload)
if __name__ == '__main__':
commentall(get_posts())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment