Skip to content

Commit 25a57fd

Browse files
committed
Setup files
1 parent a0526bc commit 25a57fd

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

check.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from instagrapi import Client
2+
from instagrapi.exceptions import LoginRequired
3+
from credentials import *
4+
5+
6+
7+
def login_user():
8+
"""
9+
Attempts to login to Instagram using either the provided session information
10+
or the provided username and password.
11+
"""
12+
13+
cl = Client()
14+
# cl.handle_exception = handle_exception
15+
cl.delay_range = [1, 3]
16+
17+
18+
session = cl.load_settings("session.json")
19+
20+
login_via_session = False
21+
login_via_pw = False
22+
23+
if session:
24+
try:
25+
cl.set_settings(session)
26+
cl.login(USERNAME, PASSWORD)
27+
28+
# check if session is valid
29+
try:
30+
cl.get_timeline_feed()
31+
except LoginRequired:
32+
print("Session is invalid, need to login via username and password")
33+
34+
old_session = cl.get_settings()
35+
36+
# use the same device uuids across logins
37+
cl.set_settings({})
38+
cl.set_uuids(old_session["uuids"])
39+
40+
cl.login(USERNAME, PASSWORD)
41+
login_via_session = True
42+
except Exception as e:
43+
print(f"Couldn't login user using session information: {e}")
44+
45+
if not login_via_session:
46+
try:
47+
if cl.login(USERNAME, PASSWORD):
48+
login_via_pw = True
49+
except Exception as e:
50+
print(f"Couldn't login user using username and password: {e}")
51+
52+
if not login_via_pw and not login_via_session:
53+
raise Exception("Couldn't login user with either password or session")
54+
55+
return cl
56+
57+
58+
59+
if __name__ == "__main__":
60+
cl = login_user()

first_time.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from instagrapi import Client
2+
from credentials import *
3+
4+
5+
cl = Client()
6+
cl.login(USERNAME, PASSWORD)
7+
cl.dump_settings("session.json")

0 commit comments

Comments
 (0)