Skip to content

Commit

Permalink
Merge pull request InstaPy#3384 from converge/cookie_file_not_found_b…
Browse files Browse the repository at this point in the history
…ugfix

cookie file not found bugfix
  • Loading branch information
timgrossmann authored Nov 18, 2018
2 parents c558120 + 2dfdc4c commit 0fec215
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ The **goal** of this file is explaining to the users of our project the notable

_The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html)_.

## [Unreleased] - 2018-11-17
### Fixed
- "Cookie file not found, creating cookie..." bug fix

## [Unreleased] - 2018-11-07
### Changed
Expand Down
27 changes: 11 additions & 16 deletions instapy/login_util.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
"""Module only used for the login part of the script"""
import time
import pickle
from selenium.webdriver.common.action_chains import ActionChains

from .time_util import sleep
from .util import update_activity
from .util import web_address_navigator
from .util import explicit_wait
from .util import click_element

from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import WebDriverException

Expand Down Expand Up @@ -66,7 +64,7 @@ def bypass_suspicious_login(browser, bypass_with_mobile):

except Exception:
print("Unable to locate email or phone button, maybe "
"bypass_suspicious_login=True isn't needed anymore.")
"bypass_suspicious_login=True isn't needed anymore.")
return False

if bypass_with_mobile:
Expand Down Expand Up @@ -138,7 +136,6 @@ def bypass_suspicious_login(browser, bypass_with_mobile):
pass



def login_user(browser,
username,
password,
Expand All @@ -158,10 +155,8 @@ def login_user(browser,

# try to load cookie from username
try:
googledotcom = "https://www.google.com"
web_address_navigator(browser, googledotcom)
for cookie in pickle.load(open('{0}{1}_cookie.pkl'
.format(logfolder,username), 'rb')):
.format(logfolder, username), 'rb')):
browser.add_cookie(cookie)
cookie_loaded = True
except (WebDriverException, OSError, IOError):
Expand Down Expand Up @@ -191,8 +186,8 @@ def login_user(browser,

# If not, issue with cookie, create new cookie
if cookie_loaded:
print("Issue with cookie for user " + username
+ ". Creating new cookie...")
print("Issue with cookie for user {}. Creating "
"new cookie...".format(username))

# Check if the first div is 'Create an Account' or 'Log In'
login_elem = browser.find_element_by_xpath(
Expand Down Expand Up @@ -273,35 +268,35 @@ def login_user(browser,
nav = browser.find_elements_by_xpath('//nav')
if len(nav) == 2:
# create cookie for username
pickle.dump(browser.get_cookies(),
open('{0}{1}_cookie.pkl'.format(logfolder,username), 'wb'))
pickle.dump(browser.get_cookies(), open(
'{0}{1}_cookie.pkl'.format(logfolder, username), 'wb'))
return True
else:
return False



def dismiss_get_app_offer(browser, logger):
""" Dismiss 'Get the Instagram App' page after a fresh login """
offer_elem = "//*[contains(text(), 'Get App')]"
dismiss_elem = "//*[contains(text(), 'Not Now')]"

# wait a bit and see if the 'Get App' offer rises up
offer_loaded = explicit_wait(browser, "VOEL", [offer_elem, "XPath"], logger, 5, False)
offer_loaded = explicit_wait(
browser, "VOEL", [offer_elem, "XPath"], logger, 5, False)

if offer_loaded:
dismiss_elem = browser.find_element_by_xpath(dismiss_elem)
click_element(browser, dismiss_elem)



def dismiss_notification_offer(browser, logger):
""" Dismiss 'Turn on Notifications' offer on session start """
offer_elem_loc = "//div/h2[text()='Turn on Notifications']"
dismiss_elem_loc = "//button[text()='Not Now']"

# wait a bit and see if the 'Turn on Notifications' offer rises up
offer_loaded = explicit_wait(browser, "VOEL", [offer_elem_loc, "XPath"], logger, 4, False)
offer_loaded = explicit_wait(
browser, "VOEL", [offer_elem_loc, "XPath"], logger, 4, False)

if offer_loaded:
dismiss_elem = browser.find_element_by_xpath(dismiss_elem_loc)
Expand Down

0 comments on commit 0fec215

Please sign in to comment.