Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- ####################
- README v 2014-12-21
- ####################
- Automatically posts birthday messages to friends on facebook.
- URL to source-code:
- http://pastebin.com/uBysEpEC
- Usage:
- General:
- python ./facebook.py <email> <password> <birthday message>
- Example:
- python ./facebook.py james1029384756@hotmail.com ********** "Happy Birthday\!"
- """
- ###########
- # Imports #
- ###########
- from splinter import Browser
- from splinter import exceptions
- import pdb
- import sys
- #############
- # Functions #
- #############
- class Facebook(object):
- def __init__(self, link_address, email, password):
- self.exists = None
- self.link_address = link_address
- self.signed_in = False
- self.browser = None
- self.connections = None
- self.email = email
- self.password = password
- def does_url_exist(self):
- """ Checks if url exists, stores the Browser() session in self.browser for continuity.
- Returns:
- url_exists: True or False
- Args:
- url: url of the site to test
- Raises:
- None
- Usage:
- url_exists = does_url_exist(url)
- """
- browser = Browser()
- try:
- browser.visit(self.link_address)
- self.exists=True
- self.browser = browser
- except:
- self.exists=False
- browser.quit()
- raise Exception("the URL does not exist: "+self.link_address)
- def sign_in(self):
- """ signs into Facebook, stores Browser() session into self.browser for continuity.
- Returns:
- None
- Args:
- None
- Raises:
- None
- Usage:
- self.sign_in()
- """
- if self.exists:
- browser = self.browser
- # Are you already signed in?
- # YES:
- try:
- #browser.find_link_by_partial_href("https://www.facebook.com/")[1].text
- browser.find_by_name("firstname")
- self.signed_in = False
- # NO:
- except exceptions.ElementDoesNotExist:
- print "ah already signed in, skipping..."
- self.signed_in = True
- if not self.signed_in:
- print "signing in to facebook..."
- browser.fill("email",self.email) # e.g.:self.email:"james1029394756@hotmail.com"
- browser.fill("pass",self.password) # e.g.:self.password:"*********"
- browser.driver.find_element_by_id("u_0_l").click() # clicks the "login" button
- self.signed_in = True
- self.browser = browser
- else:
- pass
- else:
- print "checking if URL exists..."
- self.does_url_exist()
- self.sign_in()
- def say_happy_birthday(self,birthday_message):
- """ iteratively post "Happy Birthday!" to all friends whose birthday it is
- Returns:
- None
- Args:
- birthday_message: str: birthday message to post on friend's walls, e.g. "Happy Bithday!"
- Raises:
- None
- Usage:
- self.say_happy_birthday()
- """
- if self.signed_in:
- browser = self.browser
- print "celebrating birthdays..."
- # Click "events"
- browser.find_link_by_partial_text("Events").click()
- # Expand birthdays
- browser.find_link_by_partial_href("/events/birthdays/?acontext")[0].click()
- # Say happy birthday to everyone until there are no more birthdays
- birthday_count = 0
- while True:
- try:
- browser.fill("message_text",birthday_message+"\r")
- birthday_count = birthday_count + 1
- print "birthdays celebrated: "+str(birthday_count)
- except exceptions.ElementDoesNotExist:
- print "\tno more birthdays to celebrate!"
- break
- self.browser = browser
- else:
- self.sign_in()
- self.say_happy_birthday()
- def quit(self):
- """ closes the browser
- Args:
- None
- Returns:
- None
- Raises:
- None
- E.g.:Usage:
- self.quit()
- """
- browser = self.browser
- browser.quit()
- ########
- # Main #
- ########
- platform = "facebook" # @changeme
- help_message = "Usage: python ./"+platform+" <"+platform+" email address> <password> <'birthday message'>"
- url = "https://www.facebook.com" # @changeme
- # Is the script run from within python?
- # YES:
- if len(sys.argv)==1:
- email = raw_input("type your "+platform+" email (e.g. some@email.com)...")
- password= raw_input("type your "+platform+" password (e.g. *********)...")
- else:
- # NO:
- try:
- email = sys.argv[1] # e.g.:james1029384756@hotmail.com
- except IndexError:
- raise Exception(help_message)
- try:
- password = sys.argv[2] # e.g.:*********
- except IndexError:
- raise Exception(help_message)
- try:
- birthday_message = sys.argv[3]
- except IndexError:
- raise Exception(help_message)
- session = Facebook(url,email,password)
- session.sign_in()
- try:
- session.say_happy_birthday(birthday_message)
- except:
- session.say_happy_birthday("Happy Birthday!")
- session.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement