Skip to content

Instantly share code, notes, and snippets.

@mihirsoni
Created August 31, 2013 14:54
Show Gist options
  • Save mihirsoni/6398721 to your computer and use it in GitHub Desktop.
Save mihirsoni/6398721 to your computer and use it in GitHub Desktop.
This is python script to thank people on your wall whoever wished you on your birthday. I patched up this script and make it work :-) Thanks to Quora ;)
import requests
import datetime
import time
import json
#Get the access token from https://developers.facebook.com/tools/explorer with publish permission & paste it here :)
TOKEN = ''
BIRTH_DATE = '08/30/1989'
def get_posts():
#Birthdate spliting
bday = datetime.datetime.strptime(BIRTH_DATE, '%m/%d/%Y')
#by default it will start posts from 11:45 PM in case change is required change hour & miniute.
wish = bday.replace(year=datetime.datetime.now().year,
hour=11,
minute=45)-datetime.timedelta(days=1)
# create a unix timestamp from first post's time
wish_timestamp = time.mktime(wish.timetuple())
"""Returns dictionary of id, first names of people who posted on my wall, Change LIMIT if wishes are more than 200 ;)"""
query = ("SELECT post_id, actor_id, message FROM stream WHERE "
"filter_key = 'others' AND source_id = me() AND "
"created_time > %s LIMIT 200" % wish_timestamp)
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"""
for wallpost in wallposts:
r = requests.get('https://graph.facebook.com/%s' %
wallpost['actor_id'])
#Post comment
url = 'https://graph.facebook.com/%s/comments' % wallpost['post_id']
user = json.loads(r.text)
message = 'Thank you %s :)' % user['first_name']
payload = {'access_token': TOKEN, 'message': message}
s = requests.post(url, data=payload)
print "Wall post done for %s" % user['first_name']
#Like the post
url = 'https://graph.facebook.com/%s/likes' % wallpost['post_id']
payload1 = {'access_token': TOKEN}
print "Wall post liked for %s" % user['first_name']
s = requests.post(url, data=payload1)
if __name__ == '__main__':
#commentall(get_posts())
get_posts()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment