Skip to content

Commit 714d618

Browse files
committed
Added initial settings config and other fixes
1 parent 1ea7226 commit 714d618

File tree

4 files changed

+94
-8
lines changed

4 files changed

+94
-8
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,4 @@ cython_debug/
161161

162162
# Project Specific
163163
threads_token.bin
164+
settings.json

threadspy/auth.py

+71-3
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,73 @@
66
from instagrapi import Client
77
from cryptography.fernet import Fernet
88
import os
9+
from dataclasses import dataclass
10+
from typing import Optional
11+
12+
@dataclass
13+
class Settings:
14+
uuids: dict
15+
mid: str
16+
ig_u_rur: Optional[str]
17+
ig_www_claim: Optional[str]
18+
authorization_data: dict
19+
cookies: dict
20+
last_login: float
21+
device_settings: dict
22+
user_agent: str
23+
country: str
24+
country_code: int
25+
locale: str
26+
timezone_offset: int
27+
28+
@classmethod
29+
def from_dict(cls, data: dict) -> 'Settings':
30+
return cls(
31+
uuids=data.get('uuids', {}),
32+
mid=data.get('mid', ''),
33+
ig_u_rur=data.get('ig_u_rur'),
34+
ig_www_claim=data.get('ig_www_claim'),
35+
authorization_data=data.get('authorization_data', {}),
36+
cookies=data.get('cookies', {}),
37+
last_login=data.get('last_login', 0.0),
38+
device_settings=data.get('device_settings', {}),
39+
user_agent=data.get('user_agent', ''),
40+
country=data.get('country', ''),
41+
country_code=data.get('country_code', 0),
42+
locale=data.get('locale', ''),
43+
timezone_offset=data.get('timezone_offset', 0)
44+
)
45+
46+
def to_dict(self) -> dict:
47+
return {
48+
'uuids': self.uuids,
49+
'mid': self.mid,
50+
'ig_u_rur': self.ig_u_rur,
51+
'ig_www_claim': self.ig_www_claim,
52+
'authorization_data': self.authorization_data,
53+
'cookies': self.cookies,
54+
'last_login': self.last_login,
55+
'device_settings': self.device_settings,
56+
'user_agent': self.user_agent,
57+
'country': self.country,
58+
'country_code': self.country_code,
59+
'locale': self.locale,
60+
'timezone_offset': self.timezone_offset
61+
}
62+
963

1064
class Authorization:
1165
def __init__(
1266
self,
1367
username: str = None,
1468
password: str = None,
1569
token_path: str = "",
70+
settings: Settings = None,
1671
):
1772
self.username = username
1873
self.password = password
1974
self.token_path = token_path if token_path else "threads_token.bin"
20-
75+
self.settings = settings
2176
self.headers = get_default_headers()
2277

2378
def generate_key_from_password(self, password):
@@ -47,20 +102,33 @@ def _retrieve_token(self):
47102

48103

49104
def get_instagram_api_token(self, refresh: bool = False) -> Union[str, None]:
50-
token = self._retrieve_token()
105+
# token = self._retrieve_token()
106+
token = None
51107
if token is not None and not refresh:
52108
return token
53109

54110
try:
55111
iapi = Client()
56-
iapi.login(self.username, self.password)
112+
if self.settings is not None:
113+
print("cached")
114+
iapi.set_settings(self.settings.to_dict())
115+
iapi.login(self.username, self.password)
116+
else:
117+
print("not cached")
118+
iapi.login(self.username, self.password)
119+
self.settings = Settings.from_dict(iapi.get_settings())
120+
57121
token = iapi.private.headers['Authorization'].split("Bearer IGT:2:")[1]
122+
print(token)
58123
self._store_token(token)
59124
return token
60125
except Exception as e:
61126
print(e)
62127
raise
63128

129+
def get_settings(self) -> Settings:
130+
return self.settings
131+
64132
def get_public_api_token(self) -> str:
65133
response = requests.get(
66134
url='https://www.instagram.com/instagram',

threadspy/client.py

+22-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import re
77
from threadspy.models import *
88
from threadspy.utils import get_default_headers
9-
from threadspy.auth import Authorization
9+
from threadspy.auth import Authorization, Settings
1010
import mimetypes
1111
import json
1212
from urllib.parse import quote
@@ -24,9 +24,8 @@ def __init__(
2424
password: str = None,
2525
timeout: int = 10,
2626
retries: int = 3,
27+
settings_file: str = "settings.json"
2728
):
28-
29-
self.auth = Authorization(username, password)
3029
self.timeout = timeout
3130
self.retries = retries
3231
self.session = self._create_session()
@@ -35,6 +34,10 @@ def __init__(
3534
self.private_token = None
3635
self.is_logged_in = False
3736
self.user_id = None
37+
self.settings = None
38+
self.settings_file = settings_file
39+
self._load_settings()
40+
self.auth = Authorization(username, password, self.settings)
3841

3942
@property
4043
def get_public_headers(self):
@@ -54,7 +57,18 @@ def get_private_headers(self):
5457
})
5558

5659
return headers
57-
60+
61+
def _save_settings(self):
62+
with open(self.settings_file, 'w') as file:
63+
file.write(json.dumps(self.settings.to_dict(), indent=4))
64+
65+
def _load_settings(self):
66+
if os.path.exists(self.settings_file):
67+
with open(self.settings_file, 'r') as file:
68+
self.settings = Settings.from_dict(json.loads(file.read()))
69+
else:
70+
self.settings = None
71+
5872
def _create_session(self):
5973
session = requests.Session()
6074
retry_strategy = Retry(
@@ -119,6 +133,10 @@ def login(self) -> bool:
119133
else:
120134
self.is_logged_in = True
121135

136+
if self.is_logged_in:
137+
self.settings = self.auth.get_settings()
138+
self._save_settings()
139+
122140
return self.is_logged_in
123141

124142
def get_user_id(self, username: str, instagram: bool = False) -> int:

threadspy/utils.py

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ def get_default_headers() -> dict:
2323
'AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.2 Safari/605.1.15'
2424
),
2525
'X-ASBD-ID': '129477',
26-
'X-FB-LSD': 'NjppQDEgONsU_1LCzrmp6q',
2726
'X-IG-App-ID': '238260118697367',
2827
}
2928

0 commit comments

Comments
 (0)