-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsta_loader.py
35 lines (32 loc) · 1.41 KB
/
insta_loader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import instaloader
from helpers import get_random_user_agent
class InstaLoaderWrapper:
def __init__(self):
self.loader = instaloader.Instaloader()
def login(self, username, password):
self.loader.context._session.headers.update({'User-Agent': get_random_user_agent()})
try:
self.loader.load_session_from_file(username)
return "Logged in successfully using existing session."
except FileNotFoundError:
try:
self.loader.login(username, password)
self.loader.save_session_to_file()
return "Logged in successfully."
except instaloader.exceptions.BadCredentialsException:
raise ValueError("Incorrect username or password.")
except instaloader.exceptions.ConnectionException:
raise ConnectionError("Connection error occurred.")
except Exception as e:
raise e
def get_profile(self, username):
try:
profile = instaloader.Profile.from_username(self.loader.context, username)
return profile
except instaloader.exceptions.ProfileNotExistsException:
raise ValueError("Profile does not exist.")
except instaloader.exceptions.ConnectionException:
raise ConnectionError("Connection error occurred.")
except Exception as e:
raise e
# GPCSSI2024CW407