Skip to content

Instantly share code, notes, and snippets.

@mjrich
Forked from nikhilrajendra/README.md
Created November 6, 2012 04:48
Show Gist options
  • Save mjrich/4022645 to your computer and use it in GitHub Desktop.
Save mjrich/4022645 to your computer and use it in GitHub Desktop.
Thanking friends who wishes for your birthday

Hi,

You can use the below scrip to send thank you notes to your friends who wished for your birthday on your Facebook timeline.

Instructions:

#Get the Facebook access token:

Head to https://developers.facebook.com/tools/explorer and get the api token. Select 'publish_actions', 'publish_stream','read_stream','status_update' and 'user_about_me' options when obtaining the API token

Replace YOUR_FACEBOOK_API_TOKEN by this api token

#Installing facepy:

  1. direct install
$pip install facepy
  1. from git clone
$git clone https://github.com/jgorset/facepy.git $cd facepy $python setup.py install

--If you don't have root access $python setup.py install --user

#! /usr/bin/env python
#Code stolen from amazing people here: http://pradeepnayak.in/technology/2012/08/13/programatically-responding-to-your-bday-wishes-on-facebook/
#Also, I added a bit more randomization. Instead of per line, i have per phrase randomization.
#Also, I dropped the string filtering since it wasn't working and people dont all say "happy" or "birthday.
#You should install facepy API to use this $pip install facepy
#Specify your api key and birthday date
oath_token='YOUR_AP_Key'
my_birth_day = '2012-11-04T00:00:00+0000' #in ISO 8601 format ex: 2012-05-21T00:00:00+0000
#You need not change any code below this line
from random import choice
from facepy import GraphAPI
from dateutil import parser
import json
import random
#Intialize the graph API
graph = GraphAPI(oath_token)
#some variables specific to the app
my_facebook_id = graph.get('me')['id'].encode('utf-8')
my_birth_date = parser.parse(my_birth_day)
fetchMoreFeeds = True
feedLink = "me/feed"
posts = []
bD_strings = set([" "])
def getPost(feeds):
"""
Function to get individual post from the feeds,
It adds the post to an array @posts if
* it is addressed to the user
* after the birthday until now
* user didn't commented already
* contains wishing words like happy, birthday
"""
global fetchMoreFeeds
for feed in feeds['data']:
#skip if this is not a post/message
if not feed.has_key("message"):
continue
#skip if this is my own post
if feed['from']['id'].encode('utf-8') == my_facebook_id:
continue
created_date = parser.parse(feed['created_time'])
#Stop the loop and return if this post is older than my birthday
if created_date.__lt__(my_birth_date):
fetchMoreFeeds = False
return
#Skip if the post is not addressed to me
if feed['to']['data'][0]['id'].encode('utf-8') != my_facebook_id:
continue
message = feed['message'].encode('utf-8').lower()
#if the post already has comments skip it
if feed['comments']['count'] > 0:
continue
#If message has any of the birthday words add the post to map
#if bD_strings.intersection(set(message.split())).__len__() > 0:
post = {}
post['id'] = feed['id']
post['url'] = feed['actions'][0]['link']
post['sender_name'] = feed['from']['name'].encode('utf-8')
post['sender_id'] = feed['from']['id'].encode('utf-8')
posts.append(post)
#Get the next set of posts(pagination)
feedLink = feeds['paging']['next'].replace('https://graph.facebook.com/', '')
return feedLink
# Get the feeds till the birthday
while fetchMoreFeeds:
feeds = graph.get(feedLink)
feedLink = getPost(feeds)
#Now select a random thanking note and comment on the post
greetings = ['hey,', 'aww,', ':), ', 'yay!', 'woohoo!', "yo!"]
thanks = [' thanks.', ' thank you.', ' tenks.', ' thx!', '']
questions = [" hope all is well.", " how are things?", " what's new?", " all the best.", " what's crackin?", " keep it real."]
for post in posts:
post_link = str(post['id']) + '/comments'
thank_note = (choice(greetings) + choice(thanks) + choice(questions))
graph.post(post_link, message=thank_note)
print "Wished %s .." % (post['sender_name'])
@pradeep1288
Copy link

I saw this today :-) Thanks for the credit. Appreciate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment