File size: 15,028 Bytes
f13fd7c 081627f f13fd7c 081627f f13fd7c 081627f f13fd7c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
"""Discover new Pashto-related resource candidates from public endpoints.
This script does not auto-merge into the main catalog. It writes candidates to
`resources/catalog/pending_candidates.json` for maintainer review.
Usage:
python scripts/sync_resources.py
python scripts/sync_resources.py --limit 20 --output resources/catalog/pending_candidates.json
"""
from __future__ import annotations
import argparse
import json
import re
import urllib.parse
import urllib.request
import xml.etree.ElementTree as ET
from datetime import datetime, timezone
from pathlib import Path
from typing import Any
USER_AGENT = "pashto-resource-sync/1.0"
def _slug(value: str) -> str:
value = value.lower()
value = re.sub(r"[^a-z0-9]+", "-", value)
value = re.sub(r"-+", "-", value).strip("-")
return value[:80] if value else "resource"
def _fetch_json(url: str, timeout: float = 20.0) -> Any:
req = urllib.request.Request(url, headers={"User-Agent": USER_AGENT})
with urllib.request.urlopen(req, timeout=timeout) as response:
return json.loads(response.read().decode("utf-8"))
def _fetch_text(url: str, timeout: float = 20.0) -> str:
req = urllib.request.Request(url, headers={"User-Agent": USER_AGENT})
with urllib.request.urlopen(req, timeout=timeout) as response:
return response.read().decode("utf-8", errors="replace")
def _candidate(
*,
rid: str,
title: str,
url: str,
category: str,
source: str,
summary: str,
evidence_text: str,
evidence_url: str,
markers: list[str],
tags: list[str],
) -> dict[str, Any]:
return {
"id": rid,
"title": title.strip(),
"url": url.strip(),
"category": category,
"source": source,
"status": "candidate",
"summary": summary.strip(),
"primary_use": "Needs maintainer review before promotion to verified catalog.",
"tasks": [],
"pashto_evidence": {
"evidence_text": evidence_text.strip(),
"evidence_url": evidence_url.strip(),
"markers": markers,
},
"tags": tags,
}
def fetch_huggingface(kind: str, limit: int) -> list[dict[str, Any]]:
if kind not in {"datasets", "models"}:
return []
query = urllib.parse.urlencode({"search": "pashto", "limit": str(limit)})
url = f"https://huggingface.co/api/{kind}?{query}"
payload = _fetch_json(url)
category = "dataset" if kind == "datasets" else "model"
out: list[dict[str, Any]] = []
for item in payload:
repo_id = item.get("id") or item.get("modelId")
if not repo_id:
continue
repo_url = f"https://huggingface.co/{'datasets/' if kind == 'datasets' else ''}{repo_id}"
rid = f"candidate-hf-{kind[:-1]}-{_slug(repo_id)}"
out.append(
_candidate(
rid=rid,
title=repo_id,
url=repo_url,
category=category,
source="huggingface",
summary=f"Candidate {category} returned from Hugging Face search for Pashto.",
evidence_text="Matched by Pashto keyword in Hugging Face search results.",
evidence_url=repo_url,
markers=["pashto"],
tags=["pashto", "candidate", category],
)
)
return out
def fetch_huggingface_spaces(limit: int) -> list[dict[str, Any]]:
query = urllib.parse.urlencode({"search": "pashto", "limit": str(limit)})
url = f"https://huggingface.co/api/spaces?{query}"
payload = _fetch_json(url)
out: list[dict[str, Any]] = []
for item in payload:
space_id = item.get("id")
if not space_id:
continue
space_url = f"https://huggingface.co/spaces/{space_id}"
rid = f"candidate-hf-project-{_slug(space_id)}"
summary = "Candidate project app returned from Hugging Face Spaces Pashto search."
out.append(
_candidate(
rid=rid,
title=space_id,
url=space_url,
category="project",
source="huggingface",
summary=summary,
evidence_text="Matched by Pashto keyword in Hugging Face Spaces search.",
evidence_url=space_url,
markers=["pashto"],
tags=["pashto", "candidate", "project", "space"],
)
)
return out
def fetch_kaggle_datasets(limit: int) -> list[dict[str, Any]]:
# Public Kaggle dataset listing endpoint (no auth needed for list responses).
query = urllib.parse.urlencode({"search": "pashto", "page": "1"})
url = f"https://www.kaggle.com/api/v1/datasets/list?{query}"
payload = _fetch_json(url)
out: list[dict[str, Any]] = []
for item in payload:
title = (item.get("titleNullable") or "").strip()
dataset_url = (item.get("urlNullable") or "").strip()
owner = (item.get("ownerRefNullable") or "").strip()
subtitle = (item.get("subtitleNullable") or "").strip()
if not title or not dataset_url:
continue
blob = f"{title} {subtitle}".lower()
if "pashto" not in blob and "pukhto" not in blob:
continue
owner_prefix = f"{owner}/" if owner else ""
rid = f"candidate-kaggle-dataset-{_slug(owner_prefix + title)}"
out.append(
_candidate(
rid=rid,
title=title,
url=dataset_url,
category="dataset",
source="kaggle",
summary=(subtitle or "Candidate Kaggle dataset returned from Pashto search.")[:240],
evidence_text="Kaggle dataset title/subtitle includes Pashto keyword.",
evidence_url=dataset_url,
markers=["Pashto"],
tags=["pashto", "candidate", "dataset", "kaggle"],
)
)
if len(out) >= limit:
break
return out
def fetch_github_pashto_repos(limit: int) -> list[dict[str, Any]]:
# Query by topic first for high precision, then by keyword for recall.
query_variants = [
"topic:pashto",
"pashto in:name,description,readme",
]
combined: dict[str, dict[str, Any]] = {}
for query_text in query_variants:
query = urllib.parse.urlencode(
{"q": query_text, "sort": "stars", "order": "desc", "per_page": str(limit)}
)
url = f"https://api.github.com/search/repositories?{query}"
payload = _fetch_json(url)
for item in payload.get("items", []):
full_name = item.get("full_name")
html_url = item.get("html_url")
if not full_name or not html_url:
continue
combined[full_name] = item
out: list[dict[str, Any]] = []
for full_name, item in sorted(combined.items(), key=lambda kv: kv[1].get("stargazers_count", 0), reverse=True):
name_blob = " ".join(
[
full_name or "",
item.get("name") or "",
item.get("description") or "",
" ".join(item.get("topics") or []),
]
).lower()
if "pashto" not in name_blob and "pukhto" not in name_blob:
continue
html_url = item["html_url"]
category = "project"
topics = item.get("topics") or []
if any(token in name_blob for token in ("toolkit", "library", "nlp", "asr", "tts", "ocr", "api", "code")):
category = "code"
rid = f"candidate-gh-{category}-{_slug(full_name)}"
description = (item.get("description") or "").strip()
summary = description or "Candidate Pashto-related GitHub repository."
out.append(
_candidate(
rid=rid,
title=full_name,
url=html_url,
category=category,
source="github",
summary=summary[:240] if summary else "Candidate Pashto-related GitHub repository.",
evidence_text="Repository metadata (name/description/topics) includes Pashto markers.",
evidence_url=html_url,
markers=["pashto"],
tags=["pashto", "candidate", category, "github", *(topics[:3])],
)
)
if len(out) >= limit:
break
return out
def fetch_arxiv(limit: int) -> list[dict[str, Any]]:
query = urllib.parse.urlencode(
{"search_query": "all:pashto", "start": "0", "max_results": str(limit)}
)
url = f"http://export.arxiv.org/api/query?{query}"
xml_text = _fetch_text(url)
root = ET.fromstring(xml_text)
ns = {"atom": "http://www.w3.org/2005/Atom"}
out: list[dict[str, Any]] = []
for entry in root.findall("atom:entry", ns):
title = (entry.findtext("atom:title", default="", namespaces=ns) or "").strip()
link = (entry.findtext("atom:id", default="", namespaces=ns) or "").strip()
summary = (entry.findtext("atom:summary", default="", namespaces=ns) or "").strip()
if not title or not link:
continue
rid = f"candidate-arxiv-{_slug(title)}"
out.append(
_candidate(
rid=rid,
title=title,
url=link,
category="paper",
source="arxiv",
summary=summary[:240] if summary else "Candidate paper returned from arXiv query for Pashto.",
evidence_text="Matched by arXiv query: all:pashto.",
evidence_url=link,
markers=["pashto"],
tags=["pashto", "candidate", "paper"],
)
)
return out
def fetch_semantic_scholar(limit: int) -> list[dict[str, Any]]:
fields = "title,url,abstract,year,externalIds"
query = urllib.parse.urlencode(
{"query": "pashto", "limit": str(limit), "fields": fields}
)
url = f"https://api.semanticscholar.org/graph/v1/paper/search?{query}"
payload = _fetch_json(url)
out: list[dict[str, Any]] = []
for item in payload.get("data", []):
title = (item.get("title") or "").strip()
if not title:
continue
paper_url = (item.get("url") or "").strip()
if not paper_url:
ext = item.get("externalIds") or {}
arxiv_id = ext.get("ArXiv")
if arxiv_id:
paper_url = f"https://arxiv.org/abs/{arxiv_id}"
if not paper_url:
continue
summary = (item.get("abstract") or "").strip()
rid = f"candidate-s2-{_slug(title)}"
out.append(
_candidate(
rid=rid,
title=title,
url=paper_url,
category="paper",
source="other",
summary=summary[:240] if summary else "Candidate paper returned from Semantic Scholar search for Pashto.",
evidence_text="Matched by Semantic Scholar query: pashto.",
evidence_url=paper_url,
markers=["pashto"],
tags=["pashto", "candidate", "paper"],
)
)
return out
def _dedupe_candidates(
candidates: list[dict[str, Any]],
existing_ids: set[str],
existing_urls: set[str],
) -> list[dict[str, Any]]:
unique: list[dict[str, Any]] = []
seen_ids = set(existing_ids)
seen_urls = set(existing_urls)
for item in candidates:
rid = item["id"]
url = item["url"].rstrip("/")
if rid in seen_ids or url in seen_urls:
continue
seen_ids.add(rid)
seen_urls.add(url)
unique.append(item)
return unique
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--catalog", default="resources/catalog/resources.json")
parser.add_argument("--output", default="resources/catalog/pending_candidates.json")
parser.add_argument("--limit", type=int, default=15)
args = parser.parse_args()
catalog_path = Path(args.catalog)
output_path = Path(args.output)
catalog = json.loads(catalog_path.read_text(encoding="utf-8"))
resources = catalog.get("resources", [])
existing_ids = {resource.get("id", "") for resource in resources if isinstance(resource, dict)}
existing_urls = {
resource.get("url", "").rstrip("/")
for resource in resources
if isinstance(resource, dict) and isinstance(resource.get("url"), str)
}
all_candidates: list[dict[str, Any]] = []
source_errors: list[str] = []
sources_used: list[str] = []
fetch_steps = [
("kaggle-datasets", lambda: fetch_kaggle_datasets(args.limit)),
("huggingface-datasets", lambda: fetch_huggingface("datasets", args.limit)),
("huggingface-models", lambda: fetch_huggingface("models", args.limit)),
("huggingface-spaces", lambda: fetch_huggingface_spaces(args.limit)),
("github-repositories", lambda: fetch_github_pashto_repos(args.limit)),
("arxiv", lambda: fetch_arxiv(args.limit)),
("semantic-scholar", lambda: fetch_semantic_scholar(args.limit)),
]
for source_name, step in fetch_steps:
try:
results = step()
all_candidates.extend(results)
sources_used.append(source_name)
except Exception as exc: # noqa: BLE001
source_errors.append(f"{source_name}: {exc}")
unique_candidates = _dedupe_candidates(all_candidates, existing_ids, existing_urls)
unique_candidates = sorted(unique_candidates, key=lambda item: item["title"].lower())
payload: dict[str, Any] = {
"generated_on": datetime.now(timezone.utc).isoformat(),
"sources": sources_used,
"candidate_count": len(unique_candidates),
"candidates": unique_candidates,
}
if source_errors:
payload["errors"] = source_errors
output_path.parent.mkdir(parents=True, exist_ok=True)
if output_path.exists():
try:
old_payload = json.loads(output_path.read_text(encoding="utf-8"))
except json.JSONDecodeError:
old_payload = None
if isinstance(old_payload, dict):
old_compare = {key: value for key, value in old_payload.items() if key != "generated_on"}
new_compare = {key: value for key, value in payload.items() if key != "generated_on"}
if old_compare == new_compare:
print(
f"Candidate sync complete: {len(unique_candidates)} new candidates, "
f"{len(source_errors)} source errors, no file changes"
)
return 0
output_path.write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
print(
f"Candidate sync complete: {len(unique_candidates)} new candidates, "
f"{len(source_errors)} source errors"
)
return 0
if __name__ == "__main__":
raise SystemExit(main())
|