MRiabov commited on
Commit
c12199a
·
1 Parent(s): 33b9fd0

Add fetching of READMEs to final repos

Browse files
.gitignore CHANGED
@@ -3,6 +3,8 @@ output/
3
  md-failed.txt
4
  github_links.txt
5
  awesome-repos.txt
 
 
6
 
7
 
8
  # Byte-compiled / optimized / DLL files
 
3
  md-failed.txt
4
  github_links.txt
5
  awesome-repos.txt
6
+ awesome-repos.parquet
7
+
8
 
9
 
10
  # Byte-compiled / optimized / DLL files
data_collection_utils/awesome_final_repos.py CHANGED
@@ -42,6 +42,7 @@ from github_api_utils import (
42
  fetch_repo_description,
43
  fetch_repo_readme_markdown,
44
  )
 
45
 
46
  load_dotenv()
47
 
@@ -233,18 +234,31 @@ async def crawl_awesome_final_entries(
233
  queue.append((o, r, depth + 1))
234
  else:
235
  if cu not in results:
 
236
  results[cu] = {
237
  "name": e["name"],
238
  "link": cu,
239
  "description": e["description"],
240
- "source_repo": f"{owner}/{repo}",
 
241
  }
242
  else:
243
- # Prefer keeping the first occurrence; if existing description is empty and new is not, update
 
 
 
 
 
244
  if not results[cu]["description"] and e["description"]:
245
  results[cu]["description"] = e["description"]
246
 
247
- return list(results.values())
 
 
 
 
 
 
248
 
249
 
250
  async def fetch_repo_stars(
@@ -276,6 +290,26 @@ async def enrich_with_stars(
276
  await asyncio.gather(*(one(r) for r in rows))
277
 
278
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
279
  def _desc_is_missing(desc: Optional[str]) -> bool:
280
  if desc is None:
281
  return True
@@ -297,7 +331,9 @@ async def enrich_missing_descriptions(
297
  if desc:
298
  row["description"] = desc
299
 
300
- await asyncio.gather(*(one(r) for r in rows))
 
 
301
 
302
 
303
  def main() -> None:
@@ -338,6 +374,18 @@ def main() -> None:
338
  default=cfg.get("cache_dir", "output/awesome_parse_cache"),
339
  help="Cache directory for README content",
340
  )
 
 
 
 
 
 
 
 
 
 
 
 
341
  # YAML-configurable flag to fetch stars for each parsed repo
342
  fetch_stars_value = bool(cfg.get("fetch_stars", False))
343
  args = ap.parse_args()
@@ -365,6 +413,14 @@ def main() -> None:
365
  if rows:
366
  print("Filling missing descriptions from GitHub repo metadata...")
367
  await enrich_missing_descriptions(session, rows, args.workers)
 
 
 
 
 
 
 
 
368
  out_parquet = output_dir / "awesome-repos.parquet"
369
  output_dir.mkdir(parents=True, exist_ok=True)
370
  # include stars column if present
@@ -378,6 +434,7 @@ def main() -> None:
378
  "description",
379
  "source_repo",
380
  "stars",
 
381
  ]
382
  if c in df.columns
383
  ]
 
42
  fetch_repo_description,
43
  fetch_repo_readme_markdown,
44
  )
45
+ from tqdm.auto import tqdm
46
 
47
  load_dotenv()
48
 
 
234
  queue.append((o, r, depth + 1))
235
  else:
236
  if cu not in results:
237
+ # Track the depth at which we found this repo so we can prefer the most specific (deepest) source list
238
  results[cu] = {
239
  "name": e["name"],
240
  "link": cu,
241
  "description": e["description"],
242
+ "source_repo": f"{repo}",
243
+ "source_depth": depth,
244
  }
245
  else:
246
+ # Prefer the deepest (most specific) source list as the source_repo
247
+ prev_depth = results[cu].get("source_depth")
248
+ if prev_depth is None or depth > prev_depth:
249
+ results[cu]["source_repo"] = f"{repo}"
250
+ results[cu]["source_depth"] = depth
251
+ # If existing description is empty and new is not, update
252
  if not results[cu]["description"] and e["description"]:
253
  results[cu]["description"] = e["description"]
254
 
255
+ # Drop internal helper key before returning
256
+ out: List[Dict[str, str]] = []
257
+ for v in results.values():
258
+ if "source_depth" in v:
259
+ v = {k: v[k] for k in v.keys() if k != "source_depth"}
260
+ out.append(v)
261
+ return out
262
 
263
 
264
  async def fetch_repo_stars(
 
290
  await asyncio.gather(*(one(r) for r in rows))
291
 
292
 
293
+ async def enrich_with_readme_preview(
294
+ session: aiohttp.ClientSession,
295
+ rows: List[Dict[str, str]],
296
+ concurrency: int,
297
+ max_chars: int,
298
+ ) -> None:
299
+ sem = asyncio.Semaphore(concurrency if concurrency and concurrency > 0 else 10)
300
+
301
+ async def one(row: Dict[str, str]):
302
+ async with sem:
303
+ owner, repo = parse_owner_repo(row["link"]) # link is canonical
304
+ md = await fetch_repo_readme_markdown(session, owner, repo)
305
+ if md is not None:
306
+ row["readme_preview"] = md[:max_chars]
307
+
308
+ tasks = [asyncio.create_task(one(r)) for r in rows]
309
+ for fut in tqdm(asyncio.as_completed(tasks), total=len(tasks), desc="README enrichment"):
310
+ await fut
311
+
312
+
313
  def _desc_is_missing(desc: Optional[str]) -> bool:
314
  if desc is None:
315
  return True
 
331
  if desc:
332
  row["description"] = desc
333
 
334
+ tasks = [asyncio.create_task(one(r)) for r in rows]
335
+ for fut in tqdm(asyncio.as_completed(tasks), total=len(tasks), desc="Description enrichment"):
336
+ await fut
337
 
338
 
339
  def main() -> None:
 
374
  default=cfg.get("cache_dir", "output/awesome_parse_cache"),
375
  help="Cache directory for README content",
376
  )
377
+ ap.add_argument(
378
+ "--fetch-readme-preview",
379
+ action="store_true",
380
+ default=cfg.get("fetch_readme_preview", False),
381
+ help="When set, fetch first N characters of README for each final repo and include as readme_preview",
382
+ )
383
+ ap.add_argument(
384
+ "--readme-preview-chars",
385
+ type=int,
386
+ default=cfg.get("readme_preview_chars", 1000),
387
+ help="Number of characters from README to include in readme_preview",
388
+ )
389
  # YAML-configurable flag to fetch stars for each parsed repo
390
  fetch_stars_value = bool(cfg.get("fetch_stars", False))
391
  args = ap.parse_args()
 
413
  if rows:
414
  print("Filling missing descriptions from GitHub repo metadata...")
415
  await enrich_missing_descriptions(session, rows, args.workers)
416
+ # Optionally fetch README preview for each repo
417
+ if getattr(args, "fetch_readme_preview", False) and rows:
418
+ print(
419
+ f"Fetching README previews (first {args.readme_preview_chars} chars) for {len(rows)} repos..."
420
+ )
421
+ await enrich_with_readme_preview(
422
+ session, rows, args.workers, args.readme_preview_chars
423
+ )
424
  out_parquet = output_dir / "awesome-repos.parquet"
425
  output_dir.mkdir(parents=True, exist_ok=True)
426
  # include stars column if present
 
434
  "description",
435
  "source_repo",
436
  "stars",
437
+ "readme_preview",
438
  ]
439
  if c in df.columns
440
  ]
data_collection_utils/awesome_scrap_config.yaml CHANGED
@@ -19,4 +19,11 @@ workers: 16
19
 
20
  # Optionally fetch stargazers_count for each parsed repo (extra API requests)
21
  # Set to true to include a 'stars' column in awesome-repos.parquet
22
- fetch_stars: false
 
 
 
 
 
 
 
 
19
 
20
  # Optionally fetch stargazers_count for each parsed repo (extra API requests)
21
  # Set to true to include a 'stars' column in awesome-repos.parquet
22
+ fetch_stars: false
23
+
24
+ # Optionally fetch README preview for each parsed repo (extra API requests)
25
+ # Set to true to include a 'readme_preview' column in awesome-repos.parquet
26
+ fetch_readme_preview: false
27
+
28
+ # Number of characters from README to include in readme_preview
29
+ readme_preview_chars: 1000