api 有变化, v2 to v3
Browse files- ocr/getdata.py +45 -9
ocr/getdata.py
CHANGED
|
@@ -16,11 +16,11 @@ from selenium.webdriver.support import expected_conditions as EC
|
|
| 16 |
from selenium.webdriver.support.ui import WebDriverWait
|
| 17 |
|
| 18 |
TARGET_PATTERNS = {
|
| 19 |
-
"paragraphs": re.compile(r"/api/ancientlib/read/book/paragraphs/
|
| 20 |
-
"pages": re.compile(r"/api/ancientlib/read/book/pages/
|
| 21 |
-
"word_box": re.compile(r"/api/ancientlib/read/word-box-page-content/m-get/(?:\?|$)"),
|
| 22 |
-
"loader": re.compile(r"__loader=
|
| 23 |
-
"volume": re.compile(r"/api/ancientlib/read/get/volume/(?:\?|$)"),
|
| 24 |
}
|
| 25 |
|
| 26 |
IMAGE_URL_RE = re.compile(r"https?://[^ ]+(?:\.webp|\.image|\.png|\.jpe?g|\.bmp)(?:\?.*)?$", re.IGNORECASE)
|
|
@@ -217,8 +217,10 @@ def _drain_network(
|
|
| 217 |
kind = k
|
| 218 |
break
|
| 219 |
if not kind:
|
| 220 |
-
|
| 221 |
-
|
|
|
|
|
|
|
| 222 |
if not request_id:
|
| 223 |
continue
|
| 224 |
status = int(response.get("status", 0) or 0)
|
|
@@ -235,6 +237,8 @@ def _drain_network(
|
|
| 235 |
print(f"[DEBUG] Failed to get response body for image {url}: {e}")
|
| 236 |
continue
|
| 237 |
if kind == "image":
|
|
|
|
|
|
|
| 238 |
if max_images > 0 and len(img_saved) >= max_images:
|
| 239 |
print(f"[DEBUG] Max images reached, skipping {url}")
|
| 240 |
continue
|
|
@@ -299,6 +303,39 @@ def _drain_network(
|
|
| 299 |
body_text = base64.b64decode(body_text).decode("utf-8", errors="replace")
|
| 300 |
except Exception:
|
| 301 |
body_text = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
|
| 303 |
if kind == "loader":
|
| 304 |
try:
|
|
@@ -313,7 +350,7 @@ def _drain_network(
|
|
| 313 |
}
|
| 314 |
body_text = json.dumps(para_payload, ensure_ascii=False)
|
| 315 |
kind = "paragraphs"
|
| 316 |
-
url = "https://www.shidianguji.com/api/ancientlib/read/book/paragraphs/
|
| 317 |
print(f"[DEBUG] Transformed loader response to paragraphs format")
|
| 318 |
except Exception as e:
|
| 319 |
print(f"[DEBUG] Failed to parse loader JSON: {e}")
|
|
@@ -858,4 +895,3 @@ def main() -> int:
|
|
| 858 |
|
| 859 |
if __name__ == "__main__":
|
| 860 |
raise SystemExit(main())
|
| 861 |
-
|
|
|
|
| 16 |
from selenium.webdriver.support.ui import WebDriverWait
|
| 17 |
|
| 18 |
TARGET_PATTERNS = {
|
| 19 |
+
"paragraphs": re.compile(r"/api/ancientlib/read/book/paragraphs/v\d+(?:/)?(?:\?|$)"),
|
| 20 |
+
"pages": re.compile(r"/api/ancientlib/read/book/pages/v\d+(?:/)?(?:\?|$)"),
|
| 21 |
+
"word_box": re.compile(r"/api/ancientlib/read/word-box-page-content/m-get(?:/)?(?:\?|$)"),
|
| 22 |
+
"loader": re.compile(r"(?:\?|&)__loader=__[^&]+"),
|
| 23 |
+
"volume": re.compile(r"/api/ancientlib/read/get/volume(?:/)?(?:\?|$)"),
|
| 24 |
}
|
| 25 |
|
| 26 |
IMAGE_URL_RE = re.compile(r"https?://[^ ]+(?:\.webp|\.image|\.png|\.jpe?g|\.bmp)(?:\?.*)?$", re.IGNORECASE)
|
|
|
|
| 217 |
kind = k
|
| 218 |
break
|
| 219 |
if not kind:
|
| 220 |
+
if request_id and "/api/ancientlib/read/" in url and ("json" in mime_type.lower() or mime_type.startswith("text/")):
|
| 221 |
+
kind = "unknown_read_api"
|
| 222 |
+
else:
|
| 223 |
+
continue
|
| 224 |
if not request_id:
|
| 225 |
continue
|
| 226 |
status = int(response.get("status", 0) or 0)
|
|
|
|
| 237 |
print(f"[DEBUG] Failed to get response body for image {url}: {e}")
|
| 238 |
continue
|
| 239 |
if kind == "image":
|
| 240 |
+
if max_images == 0:
|
| 241 |
+
continue
|
| 242 |
if max_images > 0 and len(img_saved) >= max_images:
|
| 243 |
print(f"[DEBUG] Max images reached, skipping {url}")
|
| 244 |
continue
|
|
|
|
| 303 |
body_text = base64.b64decode(body_text).decode("utf-8", errors="replace")
|
| 304 |
except Exception:
|
| 305 |
body_text = ""
|
| 306 |
+
|
| 307 |
+
if kind == "unknown_read_api":
|
| 308 |
+
payload = _json_loads_maybe(body_text)
|
| 309 |
+
if isinstance(payload, dict):
|
| 310 |
+
if isinstance(payload.get("paragraphList"), list):
|
| 311 |
+
para_payload = {
|
| 312 |
+
"errorCode": 0,
|
| 313 |
+
"errorMsg": "",
|
| 314 |
+
"data": {"paragraphs": payload["paragraphList"]},
|
| 315 |
+
}
|
| 316 |
+
body_text = json.dumps(para_payload, ensure_ascii=False)
|
| 317 |
+
kind = "paragraphs"
|
| 318 |
+
print(f"[DEBUG] Reclassified unknown_read_api as paragraphs via paragraphList")
|
| 319 |
+
elif isinstance(payload.get("data"), dict):
|
| 320 |
+
data = payload["data"]
|
| 321 |
+
if isinstance(data.get("paragraphList"), list):
|
| 322 |
+
para_payload = {
|
| 323 |
+
"errorCode": 0,
|
| 324 |
+
"errorMsg": "",
|
| 325 |
+
"data": {"paragraphs": data["paragraphList"]},
|
| 326 |
+
}
|
| 327 |
+
body_text = json.dumps(para_payload, ensure_ascii=False)
|
| 328 |
+
kind = "paragraphs"
|
| 329 |
+
print(f"[DEBUG] Reclassified unknown_read_api as paragraphs via data.paragraphList")
|
| 330 |
+
elif isinstance(data.get("paragraphs"), list):
|
| 331 |
+
kind = "paragraphs"
|
| 332 |
+
print(f"[DEBUG] Reclassified unknown_read_api as paragraphs via data.paragraphs")
|
| 333 |
+
elif isinstance(data.get("pages"), list):
|
| 334 |
+
kind = "pages"
|
| 335 |
+
print(f"[DEBUG] Reclassified unknown_read_api as pages via data.pages")
|
| 336 |
+
elif isinstance(data.get("pageId2WordBoxContent"), dict):
|
| 337 |
+
kind = "word_box"
|
| 338 |
+
print(f"[DEBUG] Reclassified unknown_read_api as word_box via data.pageId2WordBoxContent")
|
| 339 |
|
| 340 |
if kind == "loader":
|
| 341 |
try:
|
|
|
|
| 350 |
}
|
| 351 |
body_text = json.dumps(para_payload, ensure_ascii=False)
|
| 352 |
kind = "paragraphs"
|
| 353 |
+
url = "https://www.shidianguji.com/api/ancientlib/read/book/paragraphs/v3?mock=from_loader"
|
| 354 |
print(f"[DEBUG] Transformed loader response to paragraphs format")
|
| 355 |
except Exception as e:
|
| 356 |
print(f"[DEBUG] Failed to parse loader JSON: {e}")
|
|
|
|
| 895 |
|
| 896 |
if __name__ == "__main__":
|
| 897 |
raise SystemExit(main())
|
|
|