1+ import sys
2+ from urllib import urlencode
3+ import requests
4+ from urlparse import urlparse , parse_qs
5+ from random import choice
6+ import re
7+ from datetime import datetime , date , time
8+ import calendar
9+
10+ #settings
11+
12+ #your birthday: datetime(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])
13+ bday = datetime (2014 , 8 , 25 , 12 , 30 , 0 )
14+
15+ #access_token: Generate one at https://developers.facebook.com/tools/explorer
16+ access_token = "CAACEdEose0cBAO2yzpGTncORU3gUGZBcwAqj0ZAI5OSF2ZC2IBOh3LJa23Xso62tih2ZB74vOq4UF6YyDf3ktpoxQRWbrKZAd16OZCBaZAHMpexeklDhBSM4lOdSU9e6syLZA3uZBtbb5ZCQ6pIQx4kZBMZACxrDHKcZBxIIZBuGlnXuQ6SSbZCRw74zZCmbS8CmB9reaUrb4XMr7IZAc7lD2FZC6s6lE3dZBQbZBYmbPz8ZD"
17+
18+ #set true to like posts on your wall
19+ like = False ;
20+
21+ #set true to comment thank you
22+ comment = True ;
23+
24+ #the list of messages from which you want a random message to be selected
25+ message_set = ['Thank you very much' , 'Thanks a lot' , 'Thank you!' ]
26+
27+ #if false, repies to every message. Make it false if you are sure every wish posted on your wall is a birthday message
28+ use_filter = True
29+
30+ #keywords to respond to. Comment only on posts containing at lease one of these words
31+ bdaywords = ["happy" , "bday" , "b\' day" , "birthday" ,"hbd" , "wish" , "returns" , u"cumpleaños" .encode ("utf-8" ),"anniversaire" ,"compleanno" ,"Geburtstag" ]
32+
33+ #proxy settings
34+ http_proxy = "https://proxy61.iitd.ernet.in:3128"
35+ http_proxy = "https://proxy61.iitd.ernet.in:3128"
36+ ftp_proxy = "proxy61.iitd.ernet.in:3128"
37+
38+ #do not change anything beyond this line
39+
40+ #calculate utc timestamp
41+ epoch = datetime (1970 ,1 ,1 )
42+ td = bday - epoch
43+ utc_bday = int ((td .microseconds + (td .seconds + td .days * 24 * 3600 ) * 10 ** 6 ) / 1e6 )
44+
45+ #create proxy dictionary
46+ proxy_dict = {
47+ "http" : http_proxy ,
48+ "https" : http_proxy ,
49+ "ftp" : ftp_proxy
50+ }
51+
52+ #get birthday wishes
53+ def get_posts (url , wishes = None ):
54+ #check if we are done
55+ if wishes is None :
56+ wishes = []
57+ stop = False
58+ else :
59+ until = parse_qs (urlparse (url ).query ).get ('until' )
60+ stop = int (until [0 ]) < utc_bday
61+
62+ if stop :
63+ return wishes
64+ else :
65+ print url
66+ req = requests .get (url , proxies = proxy_dict )
67+ if req .status_code == 200 :
68+
69+ content = req .json ()
70+
71+ #keep only relevant fields from post data
72+ feed = []
73+ for post in content ['data' ]:
74+ feed .append ({'id' : post ['id' ],'from' : post ['from' ]['name' ],'message' : post .get ('message' , '' ),'type' : post ['type' ]})
75+
76+ #keep only posts relevant to birthday. Make sure you reply your friends who post happy birthday pictures on your timeline or posts in local language
77+ for post in feed :
78+ if post ['type' ]== 'status' and is_birthday (post ['message' ], use_filter ) :
79+ wishes .append (post )
80+
81+ next_url = content ['paging' ]['next' ]
82+
83+ return get_posts (next_url , wishes )
84+ else :
85+ print "Unable to connect. Check if session is still valid"
86+
87+ def confirm (prompt = None , resp = False ):
88+ if prompt is None :
89+ prompt = 'Confirm'
90+ if resp :
91+ prompt = '%s [%s]|%s: ' % (prompt , 'y' , 'n' )
92+ else :
93+ prompt = '%s [%s]|%s: ' % (prompt , 'n' , 'y' )
94+ while True :
95+ ans = raw_input (prompt )
96+ if not ans :
97+ return resp
98+ if ans not in ['y' , 'Y' , 'n' , 'N' ]:
99+ print 'please enter y or n.'
100+ continue
101+ if ans == 'y' or ans == 'Y' :
102+ return True
103+ if ans == 'n' or ans == 'N' :
104+ return False
105+
106+ def is_birthday (message , filter ):
107+ if filter == False :
108+ return True
109+ for keyword in bdaywords :
110+ if keyword in message :
111+ return True
112+ return False
113+
114+ if __name__ == '__main__' :
115+
116+ #get bithday wishes
117+ base_url = 'https://graph.facebook.com/v2.1/me/feed'
118+ params = {'since' : utc_bday , 'access_token' : access_token }
119+ url = '%s?%s' % (base_url , urlencode (params ))
120+ posts = get_posts (url )
121+
122+ #confirm before posting
123+ usersignal = confirm ('Found %s birthday wishes. Ready to thank them?' % (len (posts )))
124+
125+ #post if user said yes
126+ if usersignal is True :
127+ for post in posts :
128+
129+ #thank the user
130+ if comment :
131+ reply = choice (message_set )
132+ print 'Replying %s to %s' % (reply , wish ['from' ])
133+ url = 'https://graph.facebook.com/%s/comments?access_token=%s' % (wish ['id' ], access_token )
134+ requests .post (url , data = {'message' : reply }, proxies = proxy_dict )
135+
136+ if like :
137+ url = 'https://graph.facebook.com/%s/likes?access_token=%s' % (wish ['id' ], access_token )
138+ requests .post (url , data = "" , proxies = proxy_dict )
0 commit comments