Advertisement
Guest User

auto facebook birthdays

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