Advertisement
hello_there_andy

auto facebook birthday

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