|
@@ -3,6 +3,7 @@
|
|
|
from __future__ import annotations
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from datetime import datetime
|
|
from datetime import datetime
|
|
|
|
|
+import json
|
|
|
import time
|
|
import time
|
|
|
from typing import Any
|
|
from typing import Any
|
|
|
|
|
|
|
@@ -47,15 +48,39 @@ class HotContentFlowService:
|
|
|
return self.build_summary(hot_titles, selected_contents, decode_resp)
|
|
return self.build_summary(hot_titles, selected_contents, decode_resp)
|
|
|
|
|
|
|
|
def fetch_and_save_hot_titles(self) -> list[dict[str, Any]]:
|
|
def fetch_and_save_hot_titles(self) -> list[dict[str, Any]]:
|
|
|
- hot_url = build_url(self.config.crawapi_base_url, self.config.hot_rank_path)
|
|
|
|
|
saved_titles: list[dict[str, Any]] = []
|
|
saved_titles: list[dict[str, Any]] = []
|
|
|
seen_keys: set[str] = set()
|
|
seen_keys: set[str] = set()
|
|
|
- resp = self.api_client.post_json(hot_url, HOT_RANK_PAYLOAD)
|
|
|
|
|
|
|
+ response_cache: dict[str, dict[str, Any]] = {}
|
|
|
|
|
|
|
|
for source_config in self.config.sources:
|
|
for source_config in self.config.sources:
|
|
|
|
|
+ hot_rank_base_url = (
|
|
|
|
|
+ source_config.hot_rank_base_url or self.config.crawapi_base_url
|
|
|
|
|
+ )
|
|
|
|
|
+ hot_rank_path = source_config.hot_rank_path or self.config.hot_rank_path
|
|
|
|
|
+ hot_rank_payload = (
|
|
|
|
|
+ source_config.hot_rank_payload
|
|
|
|
|
+ if source_config.hot_rank_payload is not None
|
|
|
|
|
+ else HOT_RANK_PAYLOAD
|
|
|
|
|
+ )
|
|
|
|
|
+ cache_key = json.dumps(
|
|
|
|
|
+ {
|
|
|
|
|
+ "base_url": hot_rank_base_url,
|
|
|
|
|
+ "path": hot_rank_path,
|
|
|
|
|
+ "payload": hot_rank_payload,
|
|
|
|
|
+ },
|
|
|
|
|
+ ensure_ascii=False,
|
|
|
|
|
+ sort_keys=True,
|
|
|
|
|
+ )
|
|
|
|
|
+ if cache_key not in response_cache:
|
|
|
|
|
+ hot_url = build_url(hot_rank_base_url, hot_rank_path)
|
|
|
|
|
+ response_cache[cache_key] = self.api_client.post_json(
|
|
|
|
|
+ hot_url,
|
|
|
|
|
+ hot_rank_payload,
|
|
|
|
|
+ )
|
|
|
|
|
+ resp = response_cache[cache_key]
|
|
|
rank_items = extract_rank_items(resp, source_config.source)
|
|
rank_items = extract_rank_items(resp, source_config.source)
|
|
|
for rank_item in rank_items:
|
|
for rank_item in rank_items:
|
|
|
- title = str(rank_item.get("title") or "").strip()
|
|
|
|
|
|
|
+ title = str(rank_item.get("title") or rank_item.get("name") or "").strip()
|
|
|
if not title:
|
|
if not title:
|
|
|
continue
|
|
continue
|
|
|
unique_key = unique_title_key(source_config.source, title)
|
|
unique_key = unique_title_key(source_config.source, title)
|