-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamazon_http.py
More file actions
267 lines (233 loc) · 8.15 KB
/
Copy pathamazon_http.py
File metadata and controls
267 lines (233 loc) · 8.15 KB
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
264
265
266
267
"""
HTTP client (httpx) + cookies: sync from nodriver (CDP) or Chromium profile (fallback).
"""
from __future__ import annotations
import json
import os
import warnings
from typing import Any, Dict, List, Optional
import httpx
from httpx import Cookies
from amazon_config import AMAZON_BASE_URL
# Browser-like headers (aligned with a working Firefox/Chrome curl capture)
DEFAULT_HEADERS: dict[str, str] = {
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) "
"Gecko/20100101 Firefox/128.0"
),
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "fr,fr-FR;q=0.9,en-US;q=0.8,en;q=0.7",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Connection": "keep-alive",
"Upgrade-Insecure-Requests": "1",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "none",
"Sec-Fetch-User": "?1",
"Priority": "u=0, i",
}
def requests_cookiejar_to_httpx_cookies(jar: Any) -> Cookies:
"""Convert RequestsCookieJar / http.cookiejar to httpx.Cookies."""
hc = Cookies()
try:
for c in jar:
domain = getattr(c, "domain", "") or ""
path = getattr(c, "path", "/") or "/"
name = getattr(c, "name", "")
value = getattr(c, "value", "")
if name:
hc.set(name=name, value=value, domain=domain, path=path)
except TypeError:
try:
for c in jar:
hc.set(c.name, c.value, domain=c.domain, path=c.path)
except Exception:
pass
return hc
def nodriver_cookies_to_client(cookie_objects: List[Any]) -> httpx.Client:
"""cookie_objects = result of await browser.cookies.get_all(requests_cookie_format=True)."""
import requests.cookies
jar = requests.cookies.RequestsCookieJar()
for c in cookie_objects:
jar.set_cookie(c)
return httpx.Client(
headers=DEFAULT_HEADERS,
cookies=requests_cookiejar_to_httpx_cookies(jar),
follow_redirects=True,
timeout=30.0,
)
def write_amazon_cookies_json_from_profile(user_data_dir: str, out_path: str) -> bool:
"""
After **closing** Chrome: read ``Default/Network/Cookies`` (SQLite) and write ``amazon_cookies.json``.
Same strategy as GoupixDex / Vinted: avoids CDP ``Storage.getCookies`` which can hang forever.
"""
try:
import browser_cookie3
except ImportError:
warnings.warn(
"browser_cookie3 missing — pip install browser-cookie3",
stacklevel=2,
)
return False
cookie_file = os.path.join(user_data_dir, "Default", "Network", "Cookies")
if not os.path.isfile(cookie_file):
cookie_file = os.path.join(user_data_dir, "Default", "Cookies")
if not os.path.isfile(cookie_file):
return False
local_state = os.path.join(user_data_dir, "Local State")
key_file = local_state if os.path.isfile(local_state) else None
try:
cj = browser_cookie3.chromium(
cookie_file=cookie_file,
key_file=key_file,
)
except Exception as e:
warnings.warn(f"Lecture cookies SQLite profil: {e}", stacklevel=2)
return False
host = AMAZON_BASE_URL.replace("https://", "").replace("http://", "").split("/")[0].lower()
serializable: List[Dict[str, Any]] = []
for c in cj:
dom = (getattr(c, "domain", "") or "").lower()
if "amazon" not in dom and host not in dom:
continue
name = getattr(c, "name", "") or ""
if not name:
continue
serializable.append(
{
"name": name,
"value": getattr(c, "value", "") or "",
"domain": getattr(c, "domain", "") or "",
"path": getattr(c, "path", "/") or "/",
"secure": bool(getattr(c, "secure", False)),
}
)
if not serializable:
return False
try:
out_dir = os.path.dirname(os.path.abspath(out_path))
if out_dir:
os.makedirs(out_dir, exist_ok=True)
with open(out_path, "w", encoding="utf-8") as f:
json.dump(serializable, f)
except OSError:
return False
return True
def cookies_from_chromium_profile(user_data_dir: str) -> Optional[httpx.Client]:
"""
Plan B : lire les cookies depuis le fichier SQLite du profil (sans navigateur ouvert).
"""
try:
import browser_cookie3
except ImportError:
return None
cookie_file = os.path.join(user_data_dir, "Default", "Network", "Cookies")
if not os.path.isfile(cookie_file):
cookie_file = os.path.join(user_data_dir, "Default", "Cookies")
if not os.path.isfile(cookie_file):
return None
local_state = os.path.join(user_data_dir, "Local State")
try:
cj = browser_cookie3.chromium(
cookie_file=cookie_file,
key_file=local_state if os.path.isfile(local_state) else None,
)
except Exception as e:
warnings.warn(f"browser_cookie3 (profil Chromium): {e}", stacklevel=2)
return None
import requests.cookies
jar = requests.cookies.RequestsCookieJar()
host = AMAZON_BASE_URL.replace("https://", "").replace("http://", "").split("/")[0]
for c in cj:
try:
dom = getattr(c, "domain", "") or ""
if "amazon" in dom or host in dom:
jar.set_cookie(c)
except Exception:
continue
if not jar:
return None
return httpx.Client(
headers=DEFAULT_HEADERS,
cookies=requests_cookiejar_to_httpx_cookies(jar),
follow_redirects=True,
timeout=30.0,
)
def cookies_from_legacy_json_export(path: str) -> Optional[httpx.Client]:
"""
Charge `amazon_cookies.json` (export nodriver ou ancien format Selenium : liste de dicts).
"""
if not os.path.isfile(path):
return None
try:
with open(path, encoding="utf-8") as f:
data = json.load(f)
except (json.JSONDecodeError, OSError):
return None
if not isinstance(data, list):
return None
hc = Cookies()
n = 0
for row in data:
if not isinstance(row, dict):
continue
name = row.get("name")
if not name:
continue
value = row.get("value") or ""
domain = row.get("domain") or ""
path_c = row.get("path") or "/"
hc.set(name=name, value=value, domain=domain, path=path_c)
n += 1
if n == 0:
return None
return httpx.Client(
headers=DEFAULT_HEADERS,
cookies=hc,
follow_redirects=True,
timeout=30.0,
)
def looks_like_blocked_or_bot(html: str) -> bool:
if len(html) < 2500:
if "productTitle" not in html and "s-search-result" not in html:
return True
low = html.lower()
if "robot check" in low or "enter the characters you see" in low:
return True
if "sorry, we just need to make sure you're not a robot" in low:
return True
return False
def fetch_html_http(client: httpx.Client, url: str) -> str:
r = client.get(url)
r.raise_for_status()
return r.text
def post_hdp_request_invite(
client: httpx.Client,
*,
post_url: str,
csrf_token: str,
slate_token: Optional[str],
referer_dp_url: str,
origin_base: str,
) -> httpx.Response:
"""
POST an empty body to `data.amazon.fr/.../request-invite/{uuid}` (same contract as the browser).
"""
headers: dict[str, str] = {
"Accept": (
'application/vnd.com.amazon.api+json; type="aapi.highdemandproductcontracts.request-invite/v1"'
),
"Accept-Language": "fr-FR",
"Content-Type": (
'application/vnd.com.amazon.api+json; type="aapi.highdemandproductcontracts.request-invite.request/v1"'
),
"Origin": origin_base.rstrip("/"),
"Referer": referer_dp_url,
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-site",
"x-api-csrf-token": csrf_token,
}
if slate_token:
headers["x-amzn-encrypted-slate-token"] = slate_token
return client.post(post_url, headers=headers, content="{}")