-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstagram.py
64 lines (47 loc) · 2.14 KB
/
instagram.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import instagrapi
import requests
import env
class Client(instagrapi.Client):
def __init__(self):
super().__init__()
def _login(self):
self.username = env.INSTAGRAM_USERNAME
self.mfa_code = ''
self.session_settings_path = f"{env.WORKING_DIRECTORY_PATH}/ig-{self.username}.session"
#~ Load session settings, if exists
if os.path.exists(self.session_settings_path):
self.load_settings(self.session_settings_path)
#~ Login
if env.INSTAGRAM_2FA_SEED:
self.mfa_code = self.totp_generate_code(env.INSTAGRAM_2FA_SEED)
try:
self.login(env.INSTAGRAM_USERNAME, env.INSTAGRAM_PASSWORD, verification_code=self.mfa_code)
except instagrapi.exceptions.TwoFactorRequired:
print("Instagram | 2FA is enabled. But you did not provide a 2FA seed. Please provide a 2FA seed in the .env file.")
self.mfa_code = input("Instagram | Please enter the 2FA code: ")
self.login(env.INSTAGRAM_USERNAME, env.INSTAGRAM_PASSWORD, verification_code=self.mfa_code)
self.dump_settings(self.session_settings_path)
print("Instagram | Successfully logged in as", self.username)
def getOurNote(self) -> instagrapi.types.Note:
try:
return list(
filter(
lambda note: note.user_id == str(self.user_id),
self.get_notes()
)
)[0]
except:
raise Exception('User has no note. Please create one first.')
def deleteNote(self, noteId: str):
try:
if self.delete_note(noteId) == False:
print("Instagram | Failed to delete note")
except requests.exceptions.RetryError:
print("Instagram | Failed to delete note")
self.setStatus("🎶 Not playing", audience=env.INSTAGRAM_DEFAULT_AUDIENCE)
def setStatus(self, status: str, audience: int = 0):
if len(status) > 60:
# raise Exception("Status is too long. Max 60 characters.")
status = status[:57] + "..."
self.create_note(status, audience)