Skip to content

Instantly share code, notes, and snippets.

@Justasic
Created January 1, 2014 06:01
Show Gist options
  • Save Justasic/8205542 to your computer and use it in GitHub Desktop.
Save Justasic/8205542 to your computer and use it in GitHub Desktop.
This is my (very buggy) facebook automatic birthday wisher
#! /usr/bin/python
#############################################################################
# Facebook Auto-Birthday-Wisher Python V1.5 #
# #
# Written By: Justin Crawford <Justasic@gmail.com> #
# Auto-wishes everyone's birthdays daily and counts them. #
#############################################################################
FACEBOOK_USERNAME = ""
FACEBOOK_PASSWORD = ""
import mechanize, time, os, sys, curses, re
from BeautifulSoup import BeautifulSoup as soup
from urlparse import urlparse, parse_qs
MAX_DELAY = 60*60*24 # Birthdays only change daily, wish them daily, not every 5 seconds
delay = MAX_DELAY
totalPokes = 0
stdscr = None
row = 0
def _print(string):
global row
y, x = stdscr.getmaxyx()
row += 1
if row > (y-2):
row -= 2
stdscr.addstr(row%y-1, 1, string.encode('utf-8'))
stdscr.refresh()
def PrintTotalPokes():
global totalPokes
y, x = stdscr.getmaxyx()
RED_TEXT = 1
stdscr.hline(y-2, 0, '_', x-1)
curses.init_pair(RED_TEXT, curses.COLOR_RED, curses.COLOR_BLACK)
stdscr.addstr(y-1, 0, u"Total wishes: %d" % totalPokes, curses.color_pair(RED_TEXT))
stdscr.refresh()
def FacebookPoker(stdscr):
global delay, totalPokes, MAX_DELAY
browser = mechanize.Browser()
browser.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.61')]
browser.set_handle_robots(False)
browser.open("https://m.facebook.com/browse/birthdays")
browser._factory.is_html = True
browser.select_form(nr=0)
browser.form['email'] = FACEBOOK_USERNAME
browser.form['pass'] = FACEBOOK_PASSWORD
browser.submit()
browser._factory.is_html = True
_print("Hello!")
while True:
try:
tempPokeCount = 0
browser.open("https://m.facebook.com/browse/birthdays")
browser._factory.is_html = True
s = soup(browser.response().get_data())
for f in browser.forms():
if f.method == "POST":
## find the name
# first get the facebook user id
user_id = parse_qs(urlparse(f.attrs['action']).query)['id'][0]
# now get the image link (whose url containes the user id)
name = s.find('img', src=re.compile(user_id))['alt']
# split the name, make the message, publish.
first, last = name.split()
f['message'] = "Happy Birthday, %s!" % first
browser.open(f.click())
browser.back()
_print("Wished %s a happy birthday." % name)
totalPokes += 1
delay = MAX_DELAY
PrintTotalPokes()
except:
_print("There was some sort of error :(")
time.sleep(delay)
if __name__ == "__main__":
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(True)
stdscr.nodelay(True)
row = 0
pad = None
curses.start_color()
yx = stdscr.getmaxyx()
try:
PrintTotalPokes()
FacebookPoker(stdscr)
except curses.error as e:
print "some kind of curses error: ", e
raise
except:
raise
finally:
stdscr.keypad(False)
stdscr.nodelay(False)
curses.nocbreak()
curses.echo()
curses.endwin()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment