Advertisement
Guest User

facebook login script

a guest
Oct 10th, 2013
366
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # coding=utf-8
  2. # author foreigner.web@gmail.com
  3. import sys
  4. import os
  5. import traceback
  6. from selenium import webdriver
  7. from selenium.webdriver.common.action_chains import ActionChains
  8. from selenium.webdriver.common.by import By
  9. from selenium.webdriver.support.ui import WebDriverWait
  10. from selenium.webdriver.support import expected_conditions as ec
  11. from selenium.common.exceptions import TimeoutException, NoSuchElementException, ElementNotVisibleException
  12.  
  13.  
  14. class NotLoggedInError(Exception):
  15.     pass
  16.  
  17.  
  18. class Facebook(object):
  19.  
  20.     def __init__(self, login, password):
  21.         self.login = login
  22.         self.password = password
  23.         self.nick = None
  24.         self.normalized_nick = None
  25.         self.avatar_image = "/Users/foreigner/Downloads/avatar.jpg"
  26.         self.login_url = u"https://www.facebook.com/login.php"
  27.         self.update_url = u"https://www.facebook.com/{0}?sk=info&edit=eduwork"
  28.         self.cookie = None
  29.         self.driver = webdriver.Firefox()
  30.  
  31.     def log_in(self):
  32.         self.driver.get(self.login_url)
  33.         self.driver.find_element(By.NAME, u"email").send_keys(self.login)
  34.         self.driver.find_element(By.NAME, u"pass").send_keys(self.password)
  35.         self.driver.find_element(By.NAME, u"login").submit()
  36.  
  37.         def normalize_nick(text):
  38.             nick = '.'.join(text.split(' '))
  39.             return nick.lower()
  40.  
  41.         try:
  42.             WebDriverWait(self.driver, 10).until(ec.presence_of_element_located((By.ID, u"navHome")))
  43.             self.cookie = self.driver.get_cookies()
  44.             self.nick = self.driver.find_element(By.CLASS_NAME, u"headerTinymanName").text
  45.             self.normalized_nick = normalize_nick(self.nick)
  46.             self.update_url = self.update_url.format(self.normalized_nick)
  47.         except TimeoutException as e:
  48.             print "Request Timeout. {0}".format(e.message)
  49.         except TypeError as te:
  50.             print 'Error occurred. {0}'.format(te.message)
  51.             import traceback
  52.             type_, value_, trace_ = sys.exc_info()
  53.             print traceback.format_tb(trace_)
  54.         except:
  55.             print "Something went wrong. {0}".format(sys.exc_info()[0])
  56.  
  57.     def _stack_trace(self, exc_info):
  58.         """
  59.  
  60.        :param exc_info:
  61.        """
  62.         _type, _value, _trace = exc_info
  63.         print "Caught exception:\n\t{0}\n\t{1}\nStacktrace: {2}".format(_type, _value, traceback.format_tb(_trace))
  64.  
  65.     def _update_avatar(self):
  66.         if not self.cookie:
  67.             raise NotLoggedInError("No cookies found. Possible Log in failure")
  68.         try:
  69.             if not os.path.exists(self.avatar_image):
  70.                 raise IOError("Avatar file not found")
  71.             self.driver.find_element(By.CLASS_NAME, u"sx_53a53c").click()
  72.             WebDriverWait(self.driver, 10).until(ec.presence_of_element_located((By.CLASS_NAME, u"fbTimelineSelectorFileInput")))
  73.             WebDriverWait(self.driver, 10).until(ec.presence_of_element_located((By.NAME, u"pic")))
  74.             self.driver.find_element(By.NAME, u"pic").send_keys(self.avatar_image)
  75.         except NoSuchElementException as NSE:
  76.             print "Element not found! {0}".format(NSE.msg)
  77.         except TimeoutException as TE:
  78.             print "Request Timeout. {0}".format(TE.message)
  79.         except ElementNotVisibleException as ENV:
  80.             print "Button not visible! {0}".format(ENV.msg)
  81.         except IOError as IOE:
  82.             print IOE.message
  83.             print IOE.filename
  84.         except:
  85.             self._stack_trace(sys.exc_info())
  86.  
  87.     def _update_hometown(self):
  88.         try:
  89.             curr_city = self.driver.find_element(By.ID, u"current_city")
  90.             curr_city.find_element(By.CLASS_NAME, u"fbProfileBlankStateIcon").click()
  91.             WebDriverWait(self.driver, 15).until(ec.presence_of_element_located((By.ID, u"edit_hometown")))
  92.             input_field = self.driver.find_element(By.ID, u"edit_hometown").find_element(By.CLASS_NAME, u"inputtext")
  93.             input_field.send_keys(u"Минск")
  94.             input_field.click()
  95.             WebDriverWait(self.driver, 15).until(ec.presence_of_element_located((By.CLASS_NAME, u"text")))
  96.             self.driver.find_element(By.CLASS_NAME, u"text").click()
  97.             self.driver.find_element(By.NAME, "save").click()
  98.  
  99.         except ElementNotVisibleException as NVE:
  100.             print "Button not visible! {0}".format(NVE.msg)
  101.             self._stack_trace(sys.exc_info())
  102.         except TimeoutException as TE:
  103.             print "Timeout failure. {0}".format(TE.msg)
  104.  
  105.         except NoSuchElementException as nse:
  106.             print 'Error. Element not found! {0}'.format(nse.msg)
  107.             import traceback
  108.             type_, value_, trace_ = sys.exc_info()
  109.             print traceback.format_tb(trace_)
  110.         except:
  111.             print "Something went wrong."
  112.             import traceback
  113.             type_, value_, trace_ = sys.exc_info()
  114.             print type_, "\t", value_
  115.             print traceback.format_tb(trace_)
  116.  
  117.     def update(self):
  118.         if not self.cookie:
  119.             raise NotLoggedInError("No cookies found! Possible Log in failure")
  120.         self.driver.get(self.update_url)
  121.         # self._update_avatar()
  122.         self._update_hometown()
  123.  
  124. account = Facebook("login", "pass")
  125. account.log_in()
  126. account.update()
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement