Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2018
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 KB | None | 0 0
  1. """Module only used for the login part of the script"""
  2. from .time_util import sleep
  3. from selenium.webdriver.common.action_chains import ActionChains
  4. from selenium.common.exceptions import NoSuchElementException
  5. from selenium.common.exceptions import WebDriverException
  6. from .util import update_activity
  7. import pickle
  8.  
  9.  
  10. def bypass_suspicious_login(browser):
  11. """Bypass suspicious loggin attempt verification. This should be only enabled
  12. when there isn't available cookie for the username, otherwise it will and
  13. shows "Unable to locate email or phone button" message, folollowed by
  14. CRITICAL - Wrong login data!"""
  15. # close sign up Instagram modal if available
  16. try:
  17. close_button = browser.find_element_by_xpath("[text()='Close']")
  18. ActionChains(
  19. browser).move_to_element(close_button).click().perform()
  20. except NoSuchElementException:
  21. pass
  22.  
  23. try:
  24. # click on "This was me" button if challenge page was called
  25. this_was_me_button = browser.find_element_by_xpath(
  26. "//button[@name='choice'][text()='This Was Me']")
  27. ActionChains(
  28. browser).move_to_element(this_was_me_button).click().perform()
  29. except NoSuchElementException:
  30. # no verification needed
  31. pass
  32.  
  33. try:
  34. user_email = browser.find_element_by_xpath(
  35. "//label[@for='choice_1']").text
  36. except NoSuchElementException:
  37. try:
  38. user_email = browser.find_element_by_xpath(
  39. "//label[@class='_q0nt5']").text
  40. except:
  41. try:
  42. user_email = browser.find_element_by_xpath(
  43. "//label[@class='_q0nt5 _a7z3k']").text
  44. except:
  45. print("Unable to locate email or phone button, maybe "
  46. "bypass_suspicious_login=True isn't needed anymore.")
  47. return False
  48.  
  49. send_security_code_button = browser.find_element_by_xpath(
  50. ("//button[text()='Send Security Code']"))
  51. (ActionChains(browser)
  52. .move_to_element(send_security_code_button)
  53. .click()
  54. .perform())
  55.  
  56. print('Instagram detected an unusual login attempt')
  57. print('A security code wast sent to your {}'.format(user_email))
  58. security_code = input('Type the security code here: ')
  59.  
  60. security_code_field = browser.find_element_by_xpath((
  61. "//input[@id='security_code']"))
  62. (ActionChains(browser)
  63. .move_to_element(security_code_field)
  64. .click().send_keys(security_code).perform())
  65.  
  66. submit_security_code_button = browser.find_element_by_xpath((
  67. "//button[text()='Submit']"))
  68.  
  69. (ActionChains(browser)
  70. .move_to_element(submit_security_code_button)
  71. .click().perform())
  72.  
  73. try:
  74. sleep(5)
  75. # locate wrong security code message
  76. wrong_login = browser.find_element_by_xpath((
  77. "//p[text()='Please check the code we sent you and try "
  78. "again.']"))
  79. if wrong_login is not None:
  80. print(('Wrong security code! Please check the code Instagram'
  81. 'sent you and try again.'))
  82. except NoSuchElementException:
  83. # correct security code
  84. pass
  85.  
  86.  
  87. def login_user(browser,
  88. username,
  89. password,
  90. logfolder,
  91. switch_language=True,
  92. bypass_suspicious_attempt=False):
  93. """Logins the user with the given username and password"""
  94. browser.get('https://www.instagram.com')
  95. # update server calls
  96. update_activity()
  97. cookie_loaded = False
  98.  
  99. # try to load cookie from username
  100. try:
  101. browser.get('https://www.google.com')
  102. for cookie in pickle.load(open('{0}{1}_cookie.pkl'
  103. .format(logfolder,username), 'rb')):
  104. browser.add_cookie(cookie)
  105. cookie_loaded = True
  106. except (WebDriverException, OSError, IOError):
  107. print("Cookie file not found, creating cookie...")
  108.  
  109. browser.get('https://www.instagram.com')
  110.  
  111. # Cookie has been loaded, user should be logged in. Ensurue this is true
  112. login_elem = browser.find_elements_by_xpath(
  113. "//*[contains(text(), 'Log in')]")
  114. # Login text is not found, user logged in
  115. # If not, issue with cookie, create new cookie
  116. if len(login_elem) == 0:
  117. return True
  118.  
  119. # If not, issue with cookie, create new cookie
  120. if cookie_loaded:
  121. print("Issue with cookie for user " + username
  122. + ". Creating new cookie...")
  123.  
  124.  
  125.  
  126.  
  127.  
  128. # Changes instagram language to english, to ensure no errors ensue from
  129. # having the site on a different language
  130. # Might cause problems if the OS language is english
  131. if switch_language:
  132. browser.find_element_by_xpath(
  133. "//select[@class='_fsoey']/option[text()='English']").click()
  134.  
  135. # Check if the first div is 'Create an Account' or 'Log In'
  136. login_elem = browser.find_element_by_xpath(
  137. "//article/div/div/p/a[text()='Log in']")
  138. if login_elem is not None:
  139. ActionChains(browser).move_to_element(login_elem).click().perform()
  140.  
  141.  
  142. # Enter username and password and logs the user in
  143. # Sometimes the element name isn't 'Username' and 'Password'
  144. # (valid for placeholder too)
  145. input_username = browser.find_elements_by_xpath(
  146. "//input[@name='username']")
  147.  
  148. ActionChains(browser).move_to_element(input_username[0]). \
  149. click().send_keys(username).perform()
  150. sleep(1)
  151. input_password = browser.find_elements_by_xpath(
  152. "//input[@name='password']")
  153. ActionChains(browser).move_to_element(input_password[0]). \
  154. click().send_keys(password).perform()
  155.  
  156. login_button = browser.find_element_by_xpath(
  157. "//form/span/button[text()='Log in']")
  158. ActionChains(browser).move_to_element(login_button).click().perform()
  159. # update server calls
  160. update_activity()
  161.  
  162. if bypass_suspicious_attempt is True:
  163. bypass_suspicious_login(browser)
  164.  
  165. sleep(5)
  166.  
  167. # Check if user is logged-in (If there's two 'nav' elements)
  168. nav = browser.find_elements_by_xpath('//nav')
  169. if len(nav) == 2:
  170. # create cookie for username
  171. pickle.dump(browser.get_cookies(),
  172. open('{0}{1}_cookie.pkl'.format(logfolder,username), 'wb'))
  173. return True
  174. else:
  175. return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement