Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1.  
  2. ###########
  3. # Imports #
  4. ###########
  5.  
  6. from splinter import Browser
  7.  
  8. from splinter import exceptions
  9.  
  10. import sys
  11.  
  12.  
  13. #############
  14. # Functions #
  15. #############
  16.  
  17. class Facebook(object):
  18.  
  19. def __init__(self, link_address, email, password):
  20. self.exists = None
  21. self.link_address = link_address
  22. self.signed_in = False
  23. self.browser = None
  24. self.connections = None
  25. self.email = email
  26. self.password = password
  27.  
  28. def does_url_exist(self):
  29. """ Checks if url exists, stores the Browser() session in self.browser for continuity.
  30.  
  31. Returns:
  32. url_exists: True or False
  33. Args:
  34. url: url of the site to test
  35. Raises:
  36. None
  37. Usage:
  38. url_exists = does_url_exist(url)
  39.  
  40. """
  41. browser = Browser()
  42. try:
  43. browser.visit(self.link_address)
  44. self.exists=True
  45. self.browser = browser
  46. except:
  47. self.exists=False
  48. browser.quit()
  49. raise Exception("the linkedin URL does not exist: "+self.link_address)
  50.  
  51. def sign_in(self):
  52. """ signs into Facebook, stores Browser() session into self.browser for continuity.
  53.  
  54. Returns:
  55. None
  56. Args:
  57. None
  58. Raises:
  59. None
  60. Usage:
  61. self.sign_in()
  62.  
  63. """
  64.  
  65. if self.exists:
  66.  
  67. print "signing in to facebook..."
  68.  
  69. browser = self.browser
  70.  
  71. browser.fill("email",self.email) # e.g.:self.email:"james1029394756@hotmail.com"
  72.  
  73. browser.fill("pass",self.password) # e.g.:self.password:"*********"
  74.  
  75. browser.driver.find_element_by_id("u_0_l").click() # clicks the "login" button
  76.  
  77. self.signed_in = True
  78.  
  79. self.browser = browser
  80.  
  81. else:
  82.  
  83. print "checking if URL exists..."
  84.  
  85. self.does_url_exist()
  86.  
  87. self.sign_in()
  88.  
  89. def say_happy_birthday(self):
  90.  
  91. """ iteratively post "Happy Birthday!" to all friends whose birthday it is
  92.  
  93. Returns:
  94. None
  95. Args:
  96. None
  97. Raises:
  98. None
  99. Usage:
  100. self.say_happy_birthday()
  101.  
  102. """
  103.  
  104. if self.signed_in:
  105.  
  106. browser = self.browser
  107.  
  108. print "celebrating birthdays..."
  109.  
  110. # Click "events"
  111. browser.find_link_by_partial_text("Events").click()
  112.  
  113. # Expand birthdays
  114. browser.find_link_by_partial_href("/events/birthdays/?acontext")[0].click()
  115.  
  116. # Say happy birthday to everyone until there are no more birthdays
  117. while True:
  118.  
  119. try:
  120.  
  121. print "\tsaying happy birthday!"
  122.  
  123. browser.fill("message_text","Happy Birthday!\r")
  124.  
  125. except exceptions.ElementDoesNotExist:
  126.  
  127. print "\tno more birthdays to celebrate!"
  128.  
  129. break
  130.  
  131. self.browser = browser
  132.  
  133. else:
  134.  
  135. self.sign_in()
  136.  
  137. self.say_happy_birthday()
  138.  
  139.  
  140. def quit(self):
  141.  
  142. """ closes the browser
  143.  
  144. Args:
  145. None
  146. Returns:
  147. None
  148. Raises:
  149. None
  150. E.g.:Usage:
  151. self.quit()
  152.  
  153. """
  154.  
  155. browser = self.browser
  156.  
  157. browser.quit()
  158.  
  159. ########
  160. # Main #
  161. ########
  162.  
  163.  
  164. platform = "facebook" # @changeme
  165.  
  166. help_message = "Usage: python ./"+platform+" <"+platform+" email address> <linkedin password>"
  167.  
  168. url = "https://www.facebook.com" # @changeme
  169.  
  170.  
  171. # Is the script run from within python?
  172. # YES:
  173. if len(sys.argv)==1:
  174. email = raw_input("type your "+platform+" email (e.g. some@email.com)...")
  175. password= raw_input("type your "+platform+" password (e.g. *********)...")
  176. else:
  177. # NO:
  178. try:
  179. email = sys.argv[1] # e.g.:james1029384756@hotmail.com
  180. except IndexError:
  181. raise Exception(help_message)
  182. try:
  183. password = sys.argv[2] # e.g.:*********
  184. except IndexError:
  185. raise Exception(help_message)
  186.  
  187.  
  188. session = Facebook(url,email,password)
  189.  
  190. session.sign_in()
  191.  
  192. session.say_happy_birthday()
  193.  
  194. session.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement