Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Login: Resolve a required checkpoint & reuse browser session from chrome #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pillow
Requests>=2.13.0, <3
pycookiecheat==0.4.3
44 changes: 43 additions & 1 deletion start.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import sys
import argparse
import getpass
import json
import os
import requests
from display import display_to_terminal
import pycookiecheat as pcc

def get_credential():
if not os.path.exists('credential.json'):
Expand Down Expand Up @@ -84,6 +86,28 @@ def get_login_session(credential):
return None, two_factor_response
return session, login_response

def add_instagram_cookies(session, browser):
added = False
cookies = list()

if (browser.lower() == 'chrome'):
cookies = pcc.chrome_cookies('https://instagram.com')
for key,value in cookies.items():
session.cookies.set(key,value)

if (len(cookies) > 0):
added = True

return (session, added)

def reuse_browser_session(session):
browser = input('What browser? ')
session, success = add_instagram_cookies(session, browser)
if not success:
print('Unsupported brower: browser not in [chrome]')
return (None, False)
return (session, True)

def login(credential):
if credential:
session, _ = get_login_session(credential)
Expand All @@ -94,6 +118,17 @@ def login(credential):
user = input('Username: ')
pwd = getpass.getpass(prompt='Password: ')
session, res = get_login_session({"username": user, "password": pwd})
if 'checkpoint_required' in res['message']:
print(''.join([
'You have a check point URL to complete:\n',
'https://instagram.com', res['checkpoint_url']
]))
input('Resolve the challenge in [chrome] then, hit enter..')
print('We are going to import your instagram browser cookies..')
session, success = reuse_browser_session(session)
if not success:
sys.exit(1)
break
if res['authenticated']:
break
if not res['authenticated']:
Expand All @@ -112,7 +147,14 @@ def main():
parser.add_argument('--color', action='store_true', help='Display image with color')
display_color = parser.parse_args().color
credential = get_credential()
session = login(credential)
reuse_sess = input('Would you like to use an existing browser session? (y,n): ')
if (reuse_sess.lower() == 'y'):
session,success = reuse_browser_session(requests.session())
if not success:
print('There is something wrong with your browser session')
sys.exit(1)
else:
session = login(credential)
remove_images()
posts_info = fetch_news_feed(session)
save_image(posts_info, session)
Expand Down