-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreads_interface.py
235 lines (195 loc) · 6.55 KB
/
threads_interface.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
"""
Provide a public interface for the Threads.
"""
import json
import re
import requests
from base_interface import BaseThreadsInterface
import csv
import os
import pandas as pd
class ThreadsInterface(BaseThreadsInterface):
"""
A public interface for interacting with Threads.
Each unique endpoint requires a unique document ID, predefined by the developers.
"""
THREADS_API_URL = 'https://www.threads.net/api/graphql'
def __init__(self):
"""
Initialize the object.
"""
super().__init__()
self.api_token = self._generate_api_token()
self.default_headers = {
'Authority': 'www.threads.net',
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.9',
'Cache-Control': 'no-cache',
'Content-Type': 'application/x-www-form-urlencoded',
'Origin': 'https://www.threads.net',
'Pragma': 'no-cache',
'Sec-Fetch-Site': 'same-origin',
'X-ASBD-ID': '129477',
'X-FB-LSD': self.api_token,
'X-IG-App-ID': '238260118697367',
}
def retrieve_user(self, user_id: int) -> dict:
"""
Retrieve a user.
Args:
user_id (int): The user's unique identifier.
Returns:
The user as a dictionary.
"""
headers = self.default_headers.copy()
headers['X-FB-Friendly-Name'] = 'BarcelonaProfileRootQuery'
response = requests.post(
url=self.THREADS_API_URL,
headers=headers,
data={
'lsd': self.api_token,
'variables': json.dumps(
{
'userID': user_id,
}
),
'doc_id': '23996318473300828',
},
)
return response.json()
def retrieve_user_threads(self, user_id: int) -> dict:
"""
Retrieve a user's threads.
Args:
user_id (int): The user's unique identifier.
Returns:
The list of user's threads inside a dictionary.
"""
headers = self.default_headers.copy()
headers['X-FB-Friendly-Name'] = 'BarcelonaProfileThreadsTabQuery'
response = requests.post(
url=self.THREADS_API_URL,
headers=headers,
data={
'lsd': self.api_token,
'variables': json.dumps(
{
'userID': user_id,
}
),
'doc_id': '6232751443445612',
},
)
return response.json()
def retrieve_user_replies(self, user_id: int) -> dict:
"""
Retrieve a user's replies.
Args:
user_id (int): The user's unique identifier.
Returns:
The list of user's replies inside a dictionary.
"""
headers = self.default_headers.copy()
headers['X-FB-Friendly-Name'] = 'BarcelonaProfileRepliesTabQuery'
response = requests.post(
url=self.THREADS_API_URL,
headers=headers,
data={
'lsd': self.api_token,
'variables': json.dumps(
{
'userID': user_id,
}
),
'doc_id': '6307072669391286',
},
)
return response.json()
def retrieve_thread(self, thread_id: int) -> dict:
"""
Retrieve a thread.
Args:
thread_id (int): The thread's unique identifier.
Returns:
The thread as a dictionary.
"""
headers = self.default_headers.copy()
headers['X-FB-Friendly-Name'] = 'BarcelonaPostPageQuery'
response = requests.post(
url=self.THREADS_API_URL,
headers=headers,
data={
'lsd': self.api_token,
'variables': json.dumps(
{
'postID': thread_id,
}
),
'doc_id': '5587632691339264',
},
)
return response.json()
def retrieve_thread_likers(self, thread_id: int) -> dict:
"""
Retrieve the likers of a thread.
Args:
thread_id (int): The thread's unique identifier.
Returns:
The list of likers of the thread inside a dictionary.
"""
response = requests.post(
url=self.THREADS_API_URL,
headers=self.default_headers,
data={
'lsd': self.api_token,
'variables': json.dumps(
{
'mediaID': thread_id,
}
),
'doc_id': '9360915773983802',
},
)
return response.json()
def _generate_api_token(self) -> str:
"""
Generate a token for the Threads.
The token, called `lsd` internally, is required for any request.
For anonymous users, it is just generated automatically from the back-end and passed to the front-end.
Returns:
The token for the Threads as a string.
"""
response = requests.get(
url='https://www.instagram.com/instagram',
headers=self.headers_for_html_fetching,
)
token_key_value = re.search(
'LSD",\\[\\],{"token":"(.*?)"},\\d+\\]', response.text).group()
token_key_value = token_key_value.replace('LSD",[],{"token":"', '')
token = token_key_value.split('"')[0]
return token
def save_data_to_csv(self, data: dict
"""
Save the provided data into a CSV file.
Args:
data (dict): The data to be saved.
filename (str): The filename of the CSV file.
"""
# Convert the dictionary to a DataFrame
df = pd.DataFrame(data)
# Check if file exists
if os.path.isfile(filename):
# If it exists, append without writing headers
df.to_csv(filename, mode='a', header=False, index=False)
else:
# If it doesn't exist, write the DataFrame with headers
df.to_csv(filename, index=False)
def save_data_to_json(self, data: dict, filename: str):
"""
Save the provided data into a JSON file.
Args:
data (dict): The data to be saved.
filename (str): The filename of the JSON file.
"""
with open(filename, 'a') as json_file:
json.dump(data, json_file)