fix: content quality, image format, task monitor, calendar data source, search UI & sort
This commit is contained in:
@@ -321,6 +321,27 @@ def _extract_zhihu_post_id(url: str) -> str:
|
||||
raise ValueError("无法从URL中提取知乎文章/回答ID")
|
||||
|
||||
|
||||
def _fetch_zhihu_page(post_id: str) -> dict:
|
||||
"""多种方式尝试获取知乎文章数据,返回原始 JSON"""
|
||||
UA = _ZHIHU_UA
|
||||
# 方式1:专栏 API
|
||||
urls = [
|
||||
f"https://zhuanlan.zhihu.com/api/posts/{post_id}",
|
||||
f"https://www.zhihu.com/api/v4/posts/{post_id}",
|
||||
f"https://www.zhihu.com/api/v4/answers/{post_id}",
|
||||
]
|
||||
exceptions = []
|
||||
for url in urls:
|
||||
try:
|
||||
resp = http_requests.get(url, headers={"User-Agent": UA}, timeout=10)
|
||||
if resp.status_code == 200:
|
||||
return resp.json()
|
||||
exceptions.append(f"{url} → {resp.status_code}")
|
||||
except Exception as e:
|
||||
exceptions.append(f"{url} → {e}")
|
||||
raise RuntimeError(f"知乎API已封禁,无法获取数据({'; '.join(exceptions)})")
|
||||
|
||||
|
||||
class ZhihuFetchRequest(BaseModel):
|
||||
topic_id: str
|
||||
zhihu_url: str
|
||||
@@ -338,17 +359,9 @@ def fetch_zhihu_metrics(
|
||||
raise HTTPException(status_code=404, detail="选题不存在")
|
||||
|
||||
post_id = _extract_zhihu_post_id(data.zhihu_url)
|
||||
api_url = f"https://zhuanlan.zhihu.com/api/posts/{post_id}"
|
||||
|
||||
try:
|
||||
resp = http_requests.get(api_url, headers={"User-Agent": _ZHIHU_UA}, timeout=15)
|
||||
if resp.status_code == 404:
|
||||
api_url = f"https://www.zhihu.com/api/v4/answers/{post_id}"
|
||||
resp = http_requests.get(api_url, headers={"User-Agent": _ZHIHU_UA}, timeout=15)
|
||||
if resp.status_code != 200:
|
||||
raise HTTPException(status_code=502, detail=f"知乎API返回 {resp.status_code}")
|
||||
|
||||
raw = resp.json()
|
||||
raw = _fetch_zhihu_page(post_id)
|
||||
platform = "zhihu"
|
||||
|
||||
existing = db.query(ContentMetrics).filter(
|
||||
|
||||
Reference in New Issue
Block a user