chore: ruff 代码检查与修复
130 issues auto-fixed (import ordering, UP045/UP006 type annotations), 33 issues manually fixed (E712/E501/E402/E722 + N818 rename + per-file wechat ignore for N8xx naming conventions). All 33 tests pass.
This commit is contained in:
+2
-3
@@ -2,7 +2,6 @@ import hashlib
|
||||
from abc import ABC, abstractmethod
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from typing import List, Optional
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -12,7 +11,7 @@ class CrawlResult:
|
||||
total_count: int = 0
|
||||
new_count: int = 0
|
||||
announcements: list = field(default_factory=list)
|
||||
error_message: Optional[str] = None
|
||||
error_message: str | None = None
|
||||
crawled_at: datetime = field(default_factory=datetime.now)
|
||||
duration: float = 0.0
|
||||
|
||||
@@ -24,7 +23,7 @@ class CrawlResult:
|
||||
@dataclass
|
||||
class PipelineConfig:
|
||||
filter_enabled: bool = True
|
||||
keywords: List[str] = field(default_factory=list)
|
||||
keywords: list[str] = field(default_factory=list)
|
||||
dedup_enabled: bool = True
|
||||
notify_mode: str = "filtered"
|
||||
mark_sent: bool = False
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import asyncio
|
||||
import random
|
||||
from datetime import datetime
|
||||
|
||||
import httpx
|
||||
|
||||
from app.config import settings
|
||||
from app.crawler.base import BaseSpider, CrawlResult, PipelineConfig
|
||||
from app.crawler.parsers import parse_dahuagov_html
|
||||
|
||||
@@ -2,11 +2,12 @@ import json
|
||||
import random
|
||||
import time
|
||||
from datetime import datetime
|
||||
from typing import List, Optional
|
||||
|
||||
import httpx
|
||||
|
||||
from app.config import settings
|
||||
from app.crawler.base import BaseSpider, CrawlResult, PipelineConfig
|
||||
from app.crawler.parsers import parse_gxgp_api_response, extract_pagination
|
||||
from app.crawler.parsers import extract_pagination, parse_gxgp_api_response
|
||||
|
||||
|
||||
class GXGPSpider(BaseSpider):
|
||||
@@ -27,8 +28,8 @@ class GXGPSpider(BaseSpider):
|
||||
mark_sent=False,
|
||||
)
|
||||
|
||||
async def crawl(self, sources: Optional[List[str]] = None,
|
||||
max_pages: Optional[int] = None) -> CrawlResult:
|
||||
async def crawl(self, sources: list[str] | None = None,
|
||||
max_pages: int | None = None) -> CrawlResult:
|
||||
if max_pages is None:
|
||||
max_pages = settings.crawler_max_pages
|
||||
if sources is None:
|
||||
@@ -88,7 +89,7 @@ class GXGPSpider(BaseSpider):
|
||||
)
|
||||
|
||||
async def _fetch_page(self, client: httpx.AsyncClient, source_code: str,
|
||||
category_id: int, page_no: int) -> Optional[dict]:
|
||||
category_id: int, page_no: int) -> dict | None:
|
||||
payload = {
|
||||
"keyword": "",
|
||||
"publishDateBegin": "",
|
||||
@@ -102,7 +103,7 @@ class GXGPSpider(BaseSpider):
|
||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
|
||||
"Content-Type": "application/json;charset=UTF-8",
|
||||
"Origin": self.base_url,
|
||||
"Referer": f"{self.base_url}/site/category?parentId={category_id}&childrenCode={source_code}",
|
||||
"Referer": f"{self.base_url}/site/category?parentId={category_id}&childrenCode={source_code}", # noqa: E501
|
||||
}
|
||||
response = await client.post(
|
||||
self.announcement_api, json=payload, headers=headers
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
import hashlib
|
||||
from datetime import datetime
|
||||
from typing import Any, Dict, List
|
||||
from typing import Any
|
||||
from urllib.parse import urljoin
|
||||
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
|
||||
def parse_gxgp_api_response(
|
||||
response_data: Dict[str, Any],
|
||||
response_data: dict[str, Any],
|
||||
source_code: str,
|
||||
source_name: str,
|
||||
crawled_at: datetime,
|
||||
category_id: int,
|
||||
) -> List[Dict[str, Any]]:
|
||||
) -> list[dict[str, Any]]:
|
||||
if not response_data.get("success"):
|
||||
return []
|
||||
data = response_data.get("result", {}).get("data", {})
|
||||
@@ -62,7 +62,7 @@ def parse_gxgp_api_response(
|
||||
return results
|
||||
|
||||
|
||||
def extract_pagination(response_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
def extract_pagination(response_data: dict[str, Any]) -> dict[str, Any]:
|
||||
data = response_data.get("result", {}).get("data", {})
|
||||
return {
|
||||
"total": data.get("total", 0),
|
||||
@@ -75,7 +75,7 @@ def extract_pagination(response_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
}
|
||||
|
||||
|
||||
def parse_dahuagov_html(html: str, crawled_at: datetime) -> List[Dict[str, Any]]:
|
||||
def parse_dahuagov_html(html: str, crawled_at: datetime) -> list[dict[str, Any]]:
|
||||
soup = BeautifulSoup(html, "html.parser")
|
||||
lists = soup.find_all("ul", class_="more-list")
|
||||
if not lists:
|
||||
@@ -131,7 +131,7 @@ def parse_dahuagov_html(html: str, crawled_at: datetime) -> List[Dict[str, Any]]
|
||||
return results
|
||||
|
||||
|
||||
def _generate_hash(ann: Dict[str, Any]) -> str:
|
||||
def _generate_hash(ann: dict[str, Any]) -> str:
|
||||
content = (
|
||||
f"{ann['title']}|{ann['publish_date'].strftime('%Y-%m-%d')}"
|
||||
f"|{ann['purchase_name']}|{ann['content_url']}|{ann['source_code']}"
|
||||
|
||||
Reference in New Issue
Block a user