Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###########
- # Imports #
- ###########
- from splinter import Browser
- from splinter import exceptions
- import time
- import sys
- import pickle
- import pdb
- #############
- # 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 linkedin 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:
- print "signing in to facebook..."
- browser = self.browser
- 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:
- print "checking if URL exists..."
- self.does_url_exist()
- self.sign_in()
- def say_happy_birthday(self):
- """ iteratively post "Happy Birthday!" to all friends whose birthday it is
- Returns:
- None
- Args:
- None
- 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
- while True:
- try:
- print "\tsaying happy birthday!"
- browser.fill("message_text","Happy Birthday!\r")
- 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> <linkedin password>"
- 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. james1029384756@hotmail.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)
- session = Facebook(url,email,password)
- session.sign_in()
- session.say_happy_birthday()
- session.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement