Fix source audit metadata
Browse files
data/resource_source_audit.csv
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
scripts/audit_resource_sources.py
CHANGED
|
@@ -16,6 +16,7 @@ import time
|
|
| 16 |
import urllib.error
|
| 17 |
import urllib.request
|
| 18 |
from datetime import datetime, timezone
|
|
|
|
| 19 |
from pathlib import Path
|
| 20 |
from urllib.parse import urldefrag, urlparse
|
| 21 |
|
|
@@ -23,12 +24,8 @@ from urllib.parse import urldefrag, urlparse
|
|
| 23 |
ROOT = Path(__file__).resolve().parents[1]
|
| 24 |
RESOURCES_CSV = ROOT / "data" / "resources.csv"
|
| 25 |
AUDIT_CSV = ROOT / "data" / "resource_source_audit.csv"
|
|
|
|
| 26 |
|
| 27 |
-
TITLE_RE = re.compile(r"<title[^>]*>(.*?)</title>", re.IGNORECASE | re.DOTALL)
|
| 28 |
-
META_DESC_RE = re.compile(
|
| 29 |
-
r"<meta[^>]+(?:name|property)=[\"'](?:description|og:description)[\"'][^>]+content=[\"'](.*?)[\"'][^>]*>",
|
| 30 |
-
re.IGNORECASE | re.DOTALL,
|
| 31 |
-
)
|
| 32 |
ARXIV_RE = re.compile(r"arxiv\.org/(?:abs|pdf)/(?P<id>\d{4}\.\d{4,5})(?:v\d+)?")
|
| 33 |
|
| 34 |
FIELDS = [
|
|
@@ -64,6 +61,38 @@ def clean(value: str | None) -> str:
|
|
| 64 |
return " ".join(html.unescape(value).strip().split())
|
| 65 |
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
def read_rows() -> list[dict[str, str]]:
|
| 68 |
with RESOURCES_CSV.open(encoding="utf-8", newline="") as handle:
|
| 69 |
return list(csv.DictReader(handle))
|
|
@@ -88,9 +117,12 @@ def html_metadata(body: bytes, content_type: str) -> tuple[str, str]:
|
|
| 88 |
if "html" not in content_type.lower():
|
| 89 |
return "", ""
|
| 90 |
text = body[:750_000].decode("utf-8", errors="ignore")
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
|
| 96 |
def fetch_url(url: str, timeout: float, attempts: int) -> dict[str, str]:
|
|
@@ -98,7 +130,7 @@ def fetch_url(url: str, timeout: float, attempts: int) -> dict[str, str]:
|
|
| 98 |
last_error = ""
|
| 99 |
|
| 100 |
for attempt in range(1, attempts + 1):
|
| 101 |
-
for method in ("
|
| 102 |
request = urllib.request.Request(
|
| 103 |
url,
|
| 104 |
method=method,
|
|
@@ -106,12 +138,8 @@ def fetch_url(url: str, timeout: float, attempts: int) -> dict[str, str]:
|
|
| 106 |
)
|
| 107 |
try:
|
| 108 |
with urllib.request.urlopen(request, timeout=timeout, context=context) as response:
|
| 109 |
-
body = b""
|
| 110 |
content_type = response.headers.get("content-type", "")
|
| 111 |
-
if method == "GET"
|
| 112 |
-
body = response.read(750_000)
|
| 113 |
-
else:
|
| 114 |
-
body = b""
|
| 115 |
title, description = html_metadata(body, content_type)
|
| 116 |
return {
|
| 117 |
"audit_status": "ok",
|
|
@@ -195,8 +223,8 @@ def audit_row(row: dict[str, str], retrieved_at: str, timeout: float, attempts:
|
|
| 195 |
base.update(
|
| 196 |
{
|
| 197 |
"audit_status": "local_ok",
|
| 198 |
-
"final_url":
|
| 199 |
-
"source_title":
|
| 200 |
}
|
| 201 |
)
|
| 202 |
else:
|
|
@@ -211,18 +239,6 @@ def audit_row(row: dict[str, str], retrieved_at: str, timeout: float, attempts:
|
|
| 211 |
return base
|
| 212 |
|
| 213 |
|
| 214 |
-
def local_title(path: Path) -> str:
|
| 215 |
-
if not path.is_file() or path.suffix.lower() not in {".md", ".json", ".html", ".xml", ".txt"}:
|
| 216 |
-
return ""
|
| 217 |
-
for line in path.read_text(encoding="utf-8", errors="ignore").splitlines():
|
| 218 |
-
stripped = line.strip()
|
| 219 |
-
if stripped.startswith("# "):
|
| 220 |
-
return clean(stripped.removeprefix("# "))
|
| 221 |
-
if stripped:
|
| 222 |
-
return clean(stripped)[:160]
|
| 223 |
-
return ""
|
| 224 |
-
|
| 225 |
-
|
| 226 |
def github_stats_via_gh(repo: str, timeout: float) -> dict[str, str]:
|
| 227 |
if not repo:
|
| 228 |
return {}
|
|
|
|
| 16 |
import urllib.error
|
| 17 |
import urllib.request
|
| 18 |
from datetime import datetime, timezone
|
| 19 |
+
from html.parser import HTMLParser
|
| 20 |
from pathlib import Path
|
| 21 |
from urllib.parse import urldefrag, urlparse
|
| 22 |
|
|
|
|
| 24 |
ROOT = Path(__file__).resolve().parents[1]
|
| 25 |
RESOURCES_CSV = ROOT / "data" / "resources.csv"
|
| 26 |
AUDIT_CSV = ROOT / "data" / "resource_source_audit.csv"
|
| 27 |
+
REPO_BLOB_URL = "https://github.com/ChaoYue0307/awesome-loop-engineering/blob/main"
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
ARXIV_RE = re.compile(r"arxiv\.org/(?:abs|pdf)/(?P<id>\d{4}\.\d{4,5})(?:v\d+)?")
|
| 30 |
|
| 31 |
FIELDS = [
|
|
|
|
| 61 |
return " ".join(html.unescape(value).strip().split())
|
| 62 |
|
| 63 |
|
| 64 |
+
class MetadataParser(HTMLParser):
|
| 65 |
+
def __init__(self) -> None:
|
| 66 |
+
super().__init__()
|
| 67 |
+
self.in_title = False
|
| 68 |
+
self.title_parts: list[str] = []
|
| 69 |
+
self.description = ""
|
| 70 |
+
|
| 71 |
+
@property
|
| 72 |
+
def title(self) -> str:
|
| 73 |
+
return clean(" ".join(self.title_parts))
|
| 74 |
+
|
| 75 |
+
def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
|
| 76 |
+
if tag.lower() == "title":
|
| 77 |
+
self.in_title = True
|
| 78 |
+
return
|
| 79 |
+
if tag.lower() != "meta" or self.description:
|
| 80 |
+
return
|
| 81 |
+
|
| 82 |
+
attr_map = {key.lower(): value or "" for key, value in attrs}
|
| 83 |
+
name = (attr_map.get("name") or attr_map.get("property") or "").lower()
|
| 84 |
+
if name in {"description", "og:description", "twitter:description"}:
|
| 85 |
+
self.description = clean(attr_map.get("content"))
|
| 86 |
+
|
| 87 |
+
def handle_endtag(self, tag: str) -> None:
|
| 88 |
+
if tag.lower() == "title":
|
| 89 |
+
self.in_title = False
|
| 90 |
+
|
| 91 |
+
def handle_data(self, data: str) -> None:
|
| 92 |
+
if self.in_title:
|
| 93 |
+
self.title_parts.append(data)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
def read_rows() -> list[dict[str, str]]:
|
| 97 |
with RESOURCES_CSV.open(encoding="utf-8", newline="") as handle:
|
| 98 |
return list(csv.DictReader(handle))
|
|
|
|
| 117 |
if "html" not in content_type.lower():
|
| 118 |
return "", ""
|
| 119 |
text = body[:750_000].decode("utf-8", errors="ignore")
|
| 120 |
+
parser = MetadataParser()
|
| 121 |
+
try:
|
| 122 |
+
parser.feed(text)
|
| 123 |
+
except Exception: # noqa: BLE001 - source metadata should not fail the URL audit.
|
| 124 |
+
return "", ""
|
| 125 |
+
return parser.title, parser.description
|
| 126 |
|
| 127 |
|
| 128 |
def fetch_url(url: str, timeout: float, attempts: int) -> dict[str, str]:
|
|
|
|
| 130 |
last_error = ""
|
| 131 |
|
| 132 |
for attempt in range(1, attempts + 1):
|
| 133 |
+
for method in ("GET", "HEAD"):
|
| 134 |
request = urllib.request.Request(
|
| 135 |
url,
|
| 136 |
method=method,
|
|
|
|
| 138 |
)
|
| 139 |
try:
|
| 140 |
with urllib.request.urlopen(request, timeout=timeout, context=context) as response:
|
|
|
|
| 141 |
content_type = response.headers.get("content-type", "")
|
| 142 |
+
body = response.read(750_000) if method == "GET" else b""
|
|
|
|
|
|
|
|
|
|
| 143 |
title, description = html_metadata(body, content_type)
|
| 144 |
return {
|
| 145 |
"audit_status": "ok",
|
|
|
|
| 223 |
base.update(
|
| 224 |
{
|
| 225 |
"audit_status": "local_ok",
|
| 226 |
+
"final_url": f"{REPO_BLOB_URL}/{local_target}" + (f"#{fragment}" if fragment else ""),
|
| 227 |
+
"source_title": row["title"],
|
| 228 |
}
|
| 229 |
)
|
| 230 |
else:
|
|
|
|
| 239 |
return base
|
| 240 |
|
| 241 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
def github_stats_via_gh(repo: str, timeout: float) -> dict[str, str]:
|
| 243 |
if not repo:
|
| 244 |
return {}
|