Skip to content

Commit 27c059f

Browse files
committed
Bugfix. html_parsing/howlongtobeat_com/search.py
1 parent 824cfe0 commit 27c059f

1 file changed

Lines changed: 25 additions & 3 deletions

File tree

html_parsing/howlongtobeat_com/search.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import re
88

9+
from datetime import datetime
910
from urllib.parse import urljoin
1011
from typing import Any
1112

@@ -18,13 +19,16 @@ def api_search(text: str, page: int = 1) -> dict[str, Any]:
1819
rs = session.get(url_first)
1920
rs.raise_for_status()
2021

22+
headers = {"Referer": url_first}
23+
24+
# NOTE: Получение url из js. Старая защита. Возможно, будет комбинироваться с новой
2125
m = re.search(r'<script src="([\w/]+_app-[\w/]+\.js)"', rs.text)
2226
if not m:
2327
raise Exception("<script> не найдено!")
2428

2529
url_js: str = urljoin(URL_BASE, m.group(1))
2630

27-
rs = session.get(url_js)
31+
rs = session.get(url_js, headers=headers)
2832
rs.raise_for_status()
2933

3034
data = {
@@ -60,7 +64,9 @@ def api_search(text: str, page: int = 1) -> dict[str, Any]:
6064

6165
text_js: str = rs.text
6266

63-
m_uri_api_search = re.search("/api/(search|find|lookup|s|ouch|seek|locate)/", text_js)
67+
m_uri_api_search = re.search(
68+
"/api/(search|find|lookup|s|ouch|seek|locate)/", text_js
69+
)
6470
if m_uri_api_search:
6571
uri_api_search = m_uri_api_search.group()
6672
else:
@@ -91,9 +97,25 @@ def api_search(text: str, page: int = 1) -> dict[str, Any]:
9197
if m_k:
9298
data["searchOptions"]["games"]["gameplay"][k] = m_k.group(1)
9399

100+
# NOTE: Получение token. Новая защита
101+
rs_token = session.get(
102+
f"{URL_BASE}/api/search/init",
103+
params={"t": int(datetime.now().timestamp()) * 1000},
104+
headers=headers,
105+
)
106+
try:
107+
rs_token.raise_for_status()
108+
token: str = rs_token.json()["token"]
109+
except Exception:
110+
token = ""
111+
if not token:
112+
raise Exception("Не получен token!")
113+
114+
headers["x-auth-token"] = token
115+
94116
rs = session.post(
95117
url_api_search,
96-
headers={"Referer": url_first},
118+
headers=headers,
97119
json=data,
98120
)
99121
rs.raise_for_status()

0 commit comments

Comments
 (0)