-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathutils.py
263 lines (225 loc) · 9.76 KB
/
utils.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import logging
import random
import time
from requests_html import HTMLSession
import requests
from bs4 import BeautifulSoup
from meta_ai_api.exceptions import FacebookInvalidCredentialsException
def generate_offline_threading_id() -> str:
"""
Generates an offline threading ID.
Returns:
str: The generated offline threading ID.
"""
# Maximum value for a 64-bit integer in Python
max_int = (1 << 64) - 1
mask22_bits = (1 << 22) - 1
# Function to get the current timestamp in milliseconds
def get_current_timestamp():
return int(time.time() * 1000)
# Function to generate a random 64-bit integer
def get_random_64bit_int():
return random.getrandbits(64)
# Combine timestamp and random value
def combine_and_mask(timestamp, random_value):
shifted_timestamp = timestamp << 22
masked_random = random_value & mask22_bits
return (shifted_timestamp | masked_random) & max_int
timestamp = get_current_timestamp()
random_value = get_random_64bit_int()
threading_id = combine_and_mask(timestamp, random_value)
return str(threading_id)
def extract_value(text: str, start_str: str, end_str: str) -> str:
"""
Helper function to extract a specific value from the given text using a key.
Args:
text (str): The text from which to extract the value.
start_str (str): The starting key.
end_str (str): The ending key.
Returns:
str: The extracted value.
"""
start = text.find(start_str) + len(start_str)
end = text.find(end_str, start)
return text[start:end]
def format_response(response: dict) -> str:
"""
Formats the response from Meta AI to remove unnecessary characters.
Args:
response (dict): The dictionnary containing the response to format.
Returns:
str: The formatted response.
"""
text = ""
for content in (
response.get("data", {})
.get("node", {})
.get("bot_response_message", {})
.get("composed_text", {})
.get("content", [])
):
text += content["text"] + "\n"
return text
# Function to perform the login
def get_fb_session(email, password, proxies=None):
login_url = "https://mbasic.facebook.com/login/"
headers = {
"authority": "mbasic.facebook.com",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"accept-language": "en-US,en;q=0.9",
"sec-ch-ua": '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"macOS"',
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "none",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
}
# Send the GET request
response = requests.get(login_url, headers=headers, proxies=proxies)
soup = BeautifulSoup(response.text, "html.parser")
# Parse necessary parameters from the login form
lsd = soup.find("input", {"name": "lsd"})["value"]
jazoest = soup.find("input", {"name": "jazoest"})["value"]
li = soup.find("input", {"name": "li"})["value"]
m_ts = soup.find("input", {"name": "m_ts"})["value"]
# Define the URL and body for the POST request to submit the login form
post_url = "https://mbasic.facebook.com/login/device-based/regular/login/?refsrc=deprecated&lwv=100"
data = {
"lsd": lsd,
"jazoest": jazoest,
"m_ts": m_ts,
"li": li,
"try_number": "0",
"unrecognized_tries": "0",
"email": email,
"pass": password,
"login": "Log In",
"bi_xrwh": "0",
}
headers = {
"authority": "mbasic.facebook.com",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"accept-language": "en-US,en;q=0.9",
"cache-control": "no-cache",
"content-type": "application/x-www-form-urlencoded",
"cookie": f"datr={response.cookies.get('datr')}; sb={response.cookies.get('sb')}; ps_n=1; ps_l=1",
"dpr": "2",
"origin": "https://mbasic.facebook.com",
"pragma": "no-cache",
"referer": "https://mbasic.facebook.com/login/",
"sec-fetch-site": "same-origin",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
"viewport-width": "1728",
}
# Send the POST request
session = requests.session()
session.proxies = proxies
result = session.post(post_url, headers=headers, data=data)
if "sb" not in session.cookies:
raise FacebookInvalidCredentialsException(
"Was not able to login to Facebook. Please check your credentials. "
"You may also have been rate limited. Try to connect to Facebook manually."
)
cookies = {
**result.cookies.get_dict(),
"sb": session.cookies["sb"],
"xs": session.cookies["xs"],
"fr": session.cookies["fr"],
"c_user": session.cookies["c_user"],
}
response_login = {
"cookies": cookies,
"headers": result.headers,
"response": response.text,
}
meta_ai_cookies = get_cookies()
url = "https://www.meta.ai/state/"
payload = f'__a=1&lsd={meta_ai_cookies["lsd"]}'
headers = {
"authority": "www.meta.ai",
"accept": "*/*",
"accept-language": "en-US,en;q=0.9",
"cache-control": "no-cache",
"content-type": "application/x-www-form-urlencoded",
"cookie": f'ps_n=1; ps_l=1; dpr=2; _js_datr={meta_ai_cookies["_js_datr"]}; abra_csrf={meta_ai_cookies["abra_csrf"]}; datr={meta_ai_cookies["datr"]};; ps_l=1; ps_n=1',
"origin": "https://www.meta.ai",
"pragma": "no-cache",
"referer": "https://www.meta.ai/",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
}
response = requests.request("POST", url, headers=headers, data=payload, proxies=proxies)
state = extract_value(response.text, start_str='"state":"', end_str='"')
url = f"https://www.facebook.com/oidc/?app_id=1358015658191005&scope=openid%20linking&response_type=code&redirect_uri=https%3A%2F%2Fwww.meta.ai%2Fauth%2F&no_universal_links=1&deoia=1&state={state}"
payload = {}
headers = {
"authority": "www.facebook.com",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"accept-language": "en-US,en;q=0.9",
"cache-control": "no-cache",
"cookie": f"datr={response_login['cookies']['datr']}; sb={response_login['cookies']['sb']}; c_user={response_login['cookies']['c_user']}; xs={response_login['cookies']['xs']}; fr={response_login['cookies']['fr']}; m_page_voice={response_login['cookies']['m_page_voice']}; abra_csrf={meta_ai_cookies['abra_csrf']};",
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "cross-site",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
}
session = requests.session()
session.proxies = proxies
response = session.get(url, headers=headers, data=payload, allow_redirects=False)
next_url = response.headers["Location"]
url = next_url
payload = {}
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:125.0) Gecko/20100101 Firefox/125.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
"Referer": "https://www.meta.ai/",
"Connection": "keep-alive",
"Cookie": f'dpr=2; abra_csrf={meta_ai_cookies["abra_csrf"]}; datr={meta_ai_cookies["_js_datr"]}',
"Upgrade-Insecure-Requests": "1",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "cross-site",
"Sec-Fetch-User": "?1",
"TE": "trailers",
}
session.get(url, headers=headers, data=payload)
cookies = session.cookies.get_dict()
if "abra_sess" not in cookies:
raise FacebookInvalidCredentialsException(
"Was not able to login to Facebook. Please check your credentials. "
"You may also have been rate limited. Try to connect to Facebook manually."
)
logging.info("Successfully logged in to Facebook.")
return cookies
def get_cookies() -> dict:
"""
Extracts necessary cookies from the Meta AI main page.
Returns:
dict: A dictionary containing essential cookies.
"""
session = HTMLSession()
response = session.get("https://www.meta.ai/")
return {
"_js_datr": extract_value(
response.text, start_str='_js_datr":{"value":"', end_str='",'
),
"abra_csrf": extract_value(
response.text, start_str='abra_csrf":{"value":"', end_str='",'
),
"datr": extract_value(
response.text, start_str='datr":{"value":"', end_str='",'
),
"lsd": extract_value(
response.text, start_str='"LSD",[],{"token":"', end_str='"}'
),
}