Skip to content

Instantly share code, notes, and snippets.

@bhashkarsharma
Last active August 29, 2015 14:06
Show Gist options
  • Save bhashkarsharma/5d217482e49d97dfd15a to your computer and use it in GitHub Desktop.
Save bhashkarsharma/5d217482e49d97dfd15a to your computer and use it in GitHub Desktop.
Reply to Facebook birthday posts
# This script will go through your facebook wall,
# pick the posts within 3 days of your birthday
# and like all of them, in addition, allowing you
# to respond to the wishes with a comment.
# You will be prompted to comment each time.
# If you just hit 'Enter', it will submit the default comment.
# this needs facepy to be installed
# run 'pip install facepy'
from facepy import GraphAPI
from datetime import datetime
# Get access token from Facebook
access_token = ''
graph = GraphAPI(access_token)
# Change this default comment
default = "Thank You!"
# All posts will also be 'Liked'
like_all = True
bday_posts = []
format = "%Y-%m-%dT%H:%M:%S+0000"
# Get birthday
me = graph.get('me')
dob = me.get('birthday')
date = datetime.strptime(dob, '%m/%d/%Y')
date.replace(year=datetime.now().year)
# Get posts within 3 days of birthday
posts = graph.get('me/feed')
# This list of posts might be incomplete
# In which case, make successive calls
# to the 'next' URL in the response and
# append to bday_posts
daterange = datetime.timedelta(days=3)
for item in posts.data:
if datetime.strptime(item['updated_time'], format) - date > daterange:
bday_posts.append(item)
# TODO: filter out extra posts
# in case people have posted on your wall within this date range
# unrelated to birthday, you might want to eliminate those posts out
count = 0
for i in bday_posts:
print i['from']['name'], ": ", i['message']
id = i['id']
a = raw_input('Comment: ')
if len(a) == 0:
a = default
comment = {"message" : a }
print a
if like_all:
graph.post(path=id+"/likes")
graph.post(path=id+"/comments", **comment)
count += 1
print "Replied to %d posts." % count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment