-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaper_queue.py
More file actions
1525 lines (1375 loc) · 50.6 KB
/
paper_queue.py
File metadata and controls
1525 lines (1375 loc) · 50.6 KB
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Paper queue: discover, score, and rank papers awaiting podcast coverage.
Produces queue.yaml (machine-readable), podcasts/queue.xml (RSS),
and podcasts/queue.html (static page) so the backlog is visible
and subscribable.
"""
import glob
import html
import json
import os
import re
import subprocess
import sys
import time
import xml.etree.ElementTree as ET
import yaml
from concurrent.futures import ThreadPoolExecutor, as_completed
from datetime import datetime, timezone
from email.utils import format_datetime
from pathlib import Path
from db import get_connection, init_db, get_all_episode_arxiv_ids, get_covered_topics
from sources.arxiv_source import fetch_arxiv_papers, fetch_arxiv_papers_extended
from sources.hf_daily import fetch_hf_daily_papers
from sources.semantic import enrich_papers
from sources.social_signals import fetch_social_signals
# --- Colored terminal output ---
def _isatty():
return hasattr(sys.stderr, "isatty") and sys.stderr.isatty()
_COLORS = {
"reset": "\033[0m",
"bold": "\033[1m",
"dim": "\033[2m",
"cyan": "\033[36m",
"green": "\033[32m",
"yellow": "\033[33m",
"magenta": "\033[35m",
"red": "\033[31m",
"blue": "\033[34m",
}
def _c(color, text):
if not _isatty():
return text
return f"{_COLORS.get(color, '')}{text}{_COLORS['reset']}"
def _log(tag, msg, color="cyan"):
print(f"{_c(color, tag)} {msg}", file=sys.stderr)
def run_queue(config):
"""Main entry point: fetch, score, dedup, and write queue outputs."""
t0 = time.monotonic()
date_str = datetime.now(timezone.utc).strftime("%Y-%m-%d")
editorial_enabled = config.get("editorial", {}).get("enabled", False)
# Init DB, collect all arXiv IDs already covered
conn = get_connection()
init_db(conn)
episode_ids = get_all_episode_arxiv_ids(conn)
covered_topics = get_covered_topics(conn)
conn.close()
draft_ids = _get_draft_arxiv_ids()
exclude_ids = episode_ids | draft_ids
_log("[Queue]", f"Excluding {_c('bold', str(len(exclude_ids)))} "
"already-covered papers")
categories = config.get("arxiv_categories", [])
github_repo = config.get("github", {}).get("repo", "")
# Determine fetch mode: extended (6 months) or standard (48h)
fetch_mode = config.get("queue", {}).get("fetch_mode", "standard")
days_back = config.get("queue", {}).get("days_back", 180)
max_arxiv_results = config.get("queue", {}).get("max_arxiv_results", 5000)
# --- Parallel fetch: arXiv + HF Daily + GitHub Issues + model preload ---
_log("[Queue]", f"Fetching sources in parallel "
f"(mode={_c('bold', fetch_mode)})...", "magenta")
with ThreadPoolExecutor(max_workers=5) as pool:
if fetch_mode == "extended":
fut_arxiv = pool.submit(
fetch_arxiv_papers_extended, categories,
days_back=days_back, max_results=max_arxiv_results)
else:
fut_arxiv = pool.submit(fetch_arxiv_papers, categories)
fut_hf = pool.submit(fetch_hf_daily_papers)
fut_gh = pool.submit(
fetch_github_issues, github_repo) if github_repo else None
if editorial_enabled:
fut_scorer = pool.submit(
_preload_editorial_scorer, config,
episode_ids, covered_topics)
else:
fut_scorer = pool.submit(
_preload_scorer, config, episode_ids)
papers = fut_arxiv.result()
hf_ids = fut_hf.result()
gh_papers = fut_gh.result() if fut_gh else []
scorer = fut_scorer.result()
if not papers:
_log("[Queue]", "No papers fetched from arXiv.", "red")
sys.exit(1)
# Apply HF trending signal
for p in papers:
if p["arxiv_id"] in hf_ids:
p["hf_daily"] = True
# Semantic Scholar enrichment (sequential, rate-limited)
papers = enrich_papers(papers)
# Social/influencer signals
social_signals = {}
if config.get("social_signals", {}).get("enabled", True):
social_signals = fetch_social_signals(papers, config)
_log("[Queue]", f"Social signals: {_c('bold', str(len(social_signals)))} "
f"papers with signals")
# Merge GitHub Issues submissions
if gh_papers:
seen = {p["arxiv_id"] for p in papers}
added = 0
for gp in gh_papers:
if gp["arxiv_id"] not in seen:
papers.append(gp)
seen.add(gp["arxiv_id"])
added += 1
if added:
_log("[Queue]", f"Added {_c('green', str(added))} papers "
"from GitHub Issues")
# Remove already-covered papers
before = len(papers)
papers = [p for p in papers if p["arxiv_id"] not in exclude_ids]
_log("[Queue]", f"{_c('bold', str(len(papers)))} candidates "
f"({before - len(papers)} excluded)")
if not papers:
_log("[Queue]", "No new papers to queue.", "yellow")
return
base = Path(__file__).parent
if editorial_enabled:
_run_editorial_queue(scorer, papers, config, date_str, base,
social_signals=social_signals)
else:
_run_legacy_queue(scorer, papers, config, date_str, base)
elapsed = time.monotonic() - t0
_log("[Queue]",
f"Done in {_c('bold', f'{elapsed:.1f}s')}", "green")
def _run_legacy_queue(scorer, papers, config, date_str, base):
"""Original single-score pipeline (unchanged)."""
papers = scorer.score_papers(papers)
top_n = config.get("queue", {}).get("top_n", 30)
top_papers = papers[:top_n]
for p in top_papers:
p.setdefault("added", date_str)
p.setdefault("source", "digest")
_write_queue_yaml(top_papers, base / "queue.yaml")
generate_queue_feed(top_papers, config)
generate_queue_html(top_papers, config)
_log("[Queue]",
f"{_c('green', str(len(top_papers)))} papers queued (legacy)")
def _run_editorial_queue(scorer, papers, config, date_str, base,
social_signals=None):
"""Two-pass editorial pipeline with dual lenses."""
from llm_reviewer import LLMReviewer
# First pass: editorial scoring (with social signals)
records = scorer.score_papers(papers, social_signals=social_signals)
shortlist = scorer.select_shortlist(records)
# Second pass: LLM review on shortlist
reviewer = LLMReviewer(config)
reviewed = reviewer.review_papers(shortlist)
# Build final queue sections
sections = build_final_queue(reviewed, records, config)
# Log section breakdown
_log("[Queue]",
f"Editorial sections: "
f"{_c('green', 'Bridge')} {len(sections.get('bridge', []))}, "
f"{_c('blue', 'Public')} {len(sections.get('public', []))}, "
f"{_c('magenta', 'Memory')} {len(sections.get('memory', []))}, "
f"{_c('dim', 'Monitor')} {len(sections.get('monitor', []))}, "
f"{_c('yellow', 'Deferred')} "
f"{len(sections.get('deferred', []))}, "
f"{_c('red', 'Out of scope')} "
f"{len(sections.get('out_of_scope', []))}")
# Write outputs
write_queue_json(sections, records, base / "queue.json")
_write_queue_yaml_v2(sections, base / "queue.yaml")
generate_queue_feed_v2(sections, config)
generate_queue_html_v2(sections, config)
total = sum(len(v) for v in sections.values())
_log("[Queue]",
f"{_c('green', str(total))} papers queued (editorial)")
def _preload_scorer(config, episode_ids):
"""Load InterestScorer in a background thread while I/O runs."""
from interests import InterestScorer
return InterestScorer(config, podcasted_ids=episode_ids)
def _preload_editorial_scorer(config, episode_ids, covered_topics):
"""Load EditorialScorer in a background thread while I/O runs."""
from editorial_scorer import EditorialScorer
return EditorialScorer(
config, podcasted_ids=episode_ids,
covered_topic_texts=covered_topics if covered_topics else None)
def build_final_queue(reviewed, all_records, config):
"""Partition reviewed papers into Bridge/Public/Memory/Monitor.
Returns a dict with keys: bridge, public, memory, monitor.
"""
fq = config.get("editorial", {}).get("final_queue", None)
if fq is None:
with open(Path(__file__).parent / "weights.yaml") as f:
fq = yaml.safe_load(f).get("final_queue", {})
n_bridge = fq.get("bridge", 10)
n_public = fq.get("public", 10)
n_memory = fq.get("memory", 10)
diversity_cap = fq.get("diversity_cap", 3)
bridge_min = fq.get("bridge_min_score", 0.30)
public_min = fq.get("public_min_score", 0.35)
memory_min = fq.get("memory_min_score", 0.25)
memory_first_score = fq.get("memory_first_score", 0.40)
cover_now = [r for r in reviewed if r.status == "Cover now"]
monitor = [r for r in reviewed if r.status == "Monitor"]
deferred = [r for r in reviewed
if r.status == "Deferred this cycle"]
out_of_scope = [r for r in reviewed
if r.status == "Out of scope"]
# Sort deferred/out-of-scope by highest axis score (most
# interesting deferrals first for auditing)
deferred.sort(key=lambda r: r.max_axis_score, reverse=True)
out_of_scope.sort(
key=lambda r: r.max_axis_score, reverse=True)
# Partition Cover now into Bridge, Public-first, Memory-first
bridge = []
public_first = []
memory_first = []
for r in cover_now:
if _is_bridge_candidate(r, bridge_min):
bridge.append(r)
elif _is_memory_candidate(r, memory_min):
_add_memory_bucket_label(
r, memory_first_score=memory_first_score)
memory_first.append(r)
else:
public_first.append(r)
# Sort each bucket by quality
bridge.sort(key=lambda r: r.bridge_score, reverse=True)
public_first.sort(
key=lambda r: r.public_interest_score, reverse=True)
memory_first.sort(
key=lambda r: r.memory_score, reverse=True)
monitor.sort(
key=lambda r: r.quality_score * r.teachability,
reverse=True)
# Apply diversity cap and take N from each bucket
bridge = _apply_diversity_cap(bridge, diversity_cap)[:n_bridge]
public_first = _apply_diversity_cap(
public_first, diversity_cap)[:n_public]
memory_first = _apply_diversity_cap(
memory_first, diversity_cap)[:n_memory]
# Backfill sparse buckets from Monitor, but keep category integrity.
for bucket, target, pred, ranker in [
(bridge, n_bridge,
lambda r: _is_bridge_candidate(r, bridge_min),
lambda r: r.bridge_score + 0.2 * r.quality_score),
(public_first, n_public,
lambda r: r.public_interest_score >= public_min,
lambda r: r.public_interest_score + 0.2 * r.quality_score),
(memory_first, n_memory,
lambda r: _is_memory_candidate(r, memory_min),
lambda r: (
r.memory_score + 0.2 * r.quality_score
+ 0.1 * r.bandwidth_capacity
))]:
for cand in sorted(monitor, key=ranker, reverse=True):
if len(bucket) >= target:
break
if not pred(cand):
continue
if cand in bucket:
continue
if bucket is memory_first:
_add_memory_bucket_label(
cand, memory_first_score=memory_first_score)
bucket.append(cand)
monitor.remove(cand)
# Remaining reviewed papers that weren't placed go to monitor,
# but skip those already in deferred or out_of_scope
placed_ids = set()
for bucket in (bridge, public_first, memory_first):
for r in bucket:
placed_ids.add(r.arxiv_id)
deferred_ids = {r.arxiv_id for r in deferred}
oos_ids = {r.arxiv_id for r in out_of_scope}
for r in reviewed:
if (r.arxiv_id not in placed_ids
and r not in monitor
and r.arxiv_id not in deferred_ids
and r.arxiv_id not in oos_ids):
monitor.append(r)
return {
"bridge": bridge,
"public": public_first,
"memory": memory_first,
"monitor": monitor[:20],
"deferred": deferred,
"out_of_scope": out_of_scope,
}
def _is_bridge_candidate(rec, bridge_min):
return (
"Bridge" in rec.badges
or (rec.public_interest_score >= bridge_min
and rec.memory_score >= bridge_min)
)
def _is_memory_candidate(rec, memory_min):
return (
"Memory/Storage Core" in rec.badges
or "Memory/Storage Adjacent" in rec.badges
or rec.memory_score >= memory_min
)
def _add_memory_bucket_label(rec, memory_first_score):
label = "Memory-first" if (
"Memory/Storage Core" in rec.badges
or rec.memory_score >= memory_first_score
or rec.memory_score >= rec.public_interest_score
) else "Memory-relevant"
if label not in rec.badges:
rec.badges.append(label)
def _apply_diversity_cap(records, cap):
"""Limit very similar papers within a bucket.
Uses a simple title-based clustering: if more than `cap`
papers share a common 3-gram in their title, trim extras.
"""
if cap <= 0 or not records:
return records
result = []
cluster_counts = {}
for r in records:
# Simple cluster key: first significant bigram
words = r.title.lower().split()[:6]
key = " ".join(words[:3]) if len(words) >= 3 else r.title.lower()
cluster_counts[key] = cluster_counts.get(key, 0) + 1
if cluster_counts[key] <= cap:
result.append(r)
return result
def write_queue_json(sections, all_records, path):
"""Write full PaperRecord data as JSON for diffing."""
data = {}
for section_name, records in sections.items():
data[section_name] = [r.to_dict() for r in records]
with open(path, "w") as f:
json.dump(data, f, indent=2, default=str)
_log("[Queue]", f"JSON -> {_c('dim', str(path))}", "blue")
def _write_queue_yaml_v2(sections, path):
"""Write sectioned queue as YAML."""
data = {}
for section_name, records in sections.items():
entries = []
for r in records:
authors = r.authors
if len(authors) > 3:
authors_str = ", ".join(authors[:3]) + " et al."
else:
authors_str = ", ".join(authors)
entries.append({
"arxiv_id": r.arxiv_id,
"title": r.title,
"authors": authors_str,
"arxiv_url": r.url,
"published": r.published_at,
"public_interest_score": round(
r.public_interest_score, 4),
"memory_score": round(r.memory_score, 4),
"status": r.status,
"badges": r.badges,
})
data[section_name] = entries
with open(path, "w") as f:
yaml.dump(data, f, default_flow_style=False,
allow_unicode=True, sort_keys=False)
total = sum(len(v) for v in data.values())
_log("[Queue]", f"YAML -> {_c('dim', str(path))} "
f"({total} papers)", "blue")
def fetch_github_issues(repo):
"""Fetch paper submissions from GitHub Issues labeled 'paper-submission'.
Uses the gh CLI. Returns a list of paper dicts with arXiv metadata
fetched from the arXiv API. Gracefully returns [] on any failure.
"""
try:
result = subprocess.run(
["gh", "api", f"/repos/{repo}/issues",
"--jq", ".",
"-q", "label:paper-submission",
"--paginate"],
capture_output=True, text=True, timeout=30,
)
if result.returncode != 0:
_log("[Queue]", f"gh CLI failed: {result.stderr.strip()}",
"yellow")
return []
issues = json.loads(result.stdout) if result.stdout.strip() else []
except FileNotFoundError:
_log("[Queue]", "gh CLI not found, skipping GitHub Issues", "dim")
return []
except (subprocess.TimeoutExpired, json.JSONDecodeError) as e:
_log("[Queue]", f"GitHub Issues error: {e}", "yellow")
return []
# Filter to issues with the paper-submission label
papers = []
arxiv_re = re.compile(r'(\d{4}\.\d{4,5})')
for issue in issues:
labels = [lb.get("name", "") for lb in issue.get("labels", [])]
if "paper-submission" not in labels:
continue
body = issue.get("body", "") or ""
m = arxiv_re.search(body)
if not m:
continue
arxiv_id = m.group(1)
meta = _fetch_arxiv_meta(arxiv_id)
if meta:
meta["source"] = "github-issue"
meta["issue_number"] = issue.get("number")
papers.append(meta)
return papers
def _fetch_arxiv_meta(arxiv_id):
"""Fetch title, authors, abstract from the arXiv API for a single paper."""
import urllib.request
import xml.etree.ElementTree as ET
url = f"http://export.arxiv.org/api/query?id_list={arxiv_id}"
try:
with urllib.request.urlopen(url, timeout=15) as resp:
data = resp.read()
except Exception as e:
_log("[Queue]", f"arXiv API error for {arxiv_id}: {e}", "yellow")
return None
ns = {"atom": "http://www.w3.org/2005/Atom"}
root = ET.fromstring(data)
entry = root.find("atom:entry", ns)
if entry is None:
return None
title_el = entry.find("atom:title", ns)
abstract_el = entry.find("atom:summary", ns)
authors = [
a.find("atom:name", ns).text
for a in entry.findall("atom:author", ns)
if a.find("atom:name", ns) is not None
]
published_el = entry.find("atom:published", ns)
return {
"arxiv_id": arxiv_id,
"title": title_el.text.replace("\n", " ").strip() if title_el is not None else "",
"authors": authors,
"abstract": abstract_el.text.replace("\n", " ").strip() if abstract_el is not None else "",
"categories": [],
"arxiv_url": f"http://arxiv.org/abs/{arxiv_id}",
"published": published_el.text if published_el is not None else "",
}
def _get_draft_arxiv_ids():
"""Scan drafts/**/*.json for source_urls to find arXiv IDs in progress."""
ids = set()
arxiv_re = re.compile(r'(\d{4}\.\d{4,5})')
for json_path in glob.glob("drafts/**/*.json", recursive=True):
try:
with open(json_path) as f:
data = json.load(f)
for url in data.get("source_urls", []):
m = arxiv_re.search(url)
if m:
ids.add(m.group(1))
except (json.JSONDecodeError, OSError):
pass
return ids
def _write_queue_yaml(papers, path):
"""Write scored papers as a YAML list."""
entries = []
for p in papers:
authors = p.get("authors", [])
if len(authors) > 3:
authors_str = ", ".join(authors[:3]) + " et al."
else:
authors_str = ", ".join(authors)
entries.append({
"arxiv_id": p["arxiv_id"],
"title": p["title"],
"authors": authors_str,
"arxiv_url": p.get("arxiv_url", f"http://arxiv.org/abs/{p['arxiv_id']}"),
"published": p.get("published", ""),
"score": p.get("score", 0.0),
"score_reason": p.get("score_reason", ""),
"source": p.get("source", "digest"),
"added": p.get("added", ""),
})
with open(path, "w") as f:
yaml.dump(entries, f, default_flow_style=False, allow_unicode=True,
sort_keys=False)
_log("[Queue]", f"YAML -> {_c('dim', str(path))} "
f"({len(entries)} papers)", "blue")
def generate_queue_feed(papers, config):
"""Generate RSS 2.0 feed at podcasts/queue.xml."""
show = config.get("spotify", {}).get("show", {})
show_title = show.get("title", "AI Post Transformers")
rss = ET.Element("rss", version="2.0")
channel = ET.SubElement(rss, "channel")
ET.SubElement(channel, "title").text = f"{show_title} \u2014 Paper Queue"
ET.SubElement(channel, "description").text = (
"Ranked list of AI research papers under consideration for "
f"upcoming {show_title} episodes."
)
ET.SubElement(channel, "link").text = show.get(
"link", "https://podcast.do-not-panic.com")
ET.SubElement(channel, "language").text = "en"
ET.SubElement(channel, "lastBuildDate").text = format_datetime(
datetime.now(timezone.utc))
for p in papers:
item = ET.SubElement(channel, "item")
ET.SubElement(item, "title").text = p.get("title", "")
arxiv_url = p.get("arxiv_url", f"http://arxiv.org/abs/{p['arxiv_id']}")
ET.SubElement(item, "link").text = arxiv_url
abstract = p.get("abstract", "")
if len(abstract) > 500:
abstract = abstract[:497] + "..."
score = p.get("score", 0.0)
ET.SubElement(item, "description").text = (
f"{abstract}\n\nInterest score: {score:.3f}"
)
published = p.get("published", "")
if published:
try:
dt = datetime.fromisoformat(published.replace("Z", "+00:00"))
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
ET.SubElement(item, "pubDate").text = format_datetime(dt)
except (ValueError, TypeError):
pass
guid = ET.SubElement(item, "guid", isPermaLink="false")
guid.text = p["arxiv_id"]
feed_path = Path(__file__).parent / "podcasts" / "queue.xml"
feed_path.parent.mkdir(parents=True, exist_ok=True)
tree = ET.ElementTree(rss)
ET.indent(tree, space=" ")
tree.write(str(feed_path), xml_declaration=True, encoding="UTF-8")
_log("[Queue]", f"RSS -> {_c('dim', str(feed_path))} "
f"({len(papers)} items)", "blue")
return str(feed_path)
_REASON_COLORS = [
("matches '", "tip-similarity"),
("secondary interest", "tip-similarity"),
("keywords:", "tip-keyword"),
("HF trending", "tip-hf"),
("cited", "tip-citation"),
("already podcasted", "tip-penalty"),
]
def _color_reason_line(reason):
"""Wrap a score_reason part in a colored tip-line div."""
css_class = ""
lower = reason.lower()
for prefix, cls in _REASON_COLORS:
if prefix.lower() in lower:
css_class = cls
break
escaped = html.escape(reason)
if css_class:
return f'<div class="tip-line {css_class}">{escaped}</div>'
return f'<div class="tip-line">{escaped}</div>'
def generate_queue_html(papers, config):
"""Generate a static HTML page at podcasts/queue.html."""
show = config.get("spotify", {}).get("show", {})
show_title = show.get("title", "AI Post Transformers")
github_repo = config.get("github", {}).get("repo", "")
rows_html = []
for i, p in enumerate(papers, 1):
authors = p.get("authors", [])
if isinstance(authors, list):
if len(authors) > 3:
authors_str = ", ".join(authors[:3]) + " et al."
else:
authors_str = ", ".join(authors)
else:
authors_str = str(authors)
arxiv_url = p.get("arxiv_url", f"http://arxiv.org/abs/{p['arxiv_id']}")
published = p.get("published", "")
if published:
try:
dt = datetime.fromisoformat(published.replace("Z", "+00:00"))
published = dt.strftime("%Y-%m-%d")
except (ValueError, TypeError):
pass
score = p.get("score", 0.0)
reason = html.escape(p.get("score_reason", ""))
source = p.get("source", "digest")
source_badge = ""
if source == "github-issue":
issue_num = p.get("issue_number", "")
source_badge = f' <span class="badge">#{issue_num}</span>'
# Break score_reason into labeled parts for the tooltip
reason_parts = p.get("score_reason", "").split("; ")
reason_lines = "".join(
_color_reason_line(r) for r in reason_parts if r
)
rows_html.append(
f'<tr>'
f'<td class="rank">{i}</td>'
f'<td><a href="{html.escape(arxiv_url)}" target="_blank">'
f'{html.escape(p.get("title", ""))}</a>{source_badge}</td>'
f'<td class="authors">{html.escape(authors_str)}</td>'
f'<td class="date">{html.escape(published)}</td>'
f'<td class="score">{score:.3f}'
f'<div class="score-tip">'
f'<div class="tip-header">Score breakdown</div>'
f'{reason_lines}</div></td>'
f'</tr>'
)
table_rows = "\n".join(rows_html)
count = len(papers)
n_categories = len(config.get("arxiv_categories", []))
top_n = config.get("queue", {}).get("top_n", 30)
# GitHub Issues JS block (only included when repo is configured)
gh_js = ""
if github_repo:
gh_js = f"""
<script>
(function() {{
var repo = "{html.escape(github_repo)}";
var url = "https://api.github.com/repos/" + repo +
"/issues?labels=paper-submission&state=open&per_page=20";
fetch(url)
.then(function(r) {{ return r.json(); }})
.then(function(issues) {{
if (!issues.length) return;
var sec = document.getElementById("gh-submissions");
sec.style.display = "block";
var list = sec.querySelector("ul");
issues.forEach(function(iss) {{
var li = document.createElement("li");
var a = document.createElement("a");
a.href = iss.html_url;
a.target = "_blank";
a.textContent = iss.title;
li.appendChild(a);
list.appendChild(li);
}});
}})
.catch(function() {{}});
}})();
</script>"""
page = f"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Paper Queue — {html.escape(show_title)}</title>
<style>
*, *::before, *::after {{ box-sizing: border-box; margin: 0; padding: 0; }}
body {{
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
Helvetica, Arial, sans-serif;
background: url("images/queue-bg.png") center center / cover no-repeat fixed,
#141414;
color: #e5e5e5;
line-height: 1.5;
min-height: 100vh;
}}
body::before {{
content: "";
position: fixed;
inset: 0;
background: rgba(14, 14, 14, 0.82);
z-index: 0;
pointer-events: none;
}}
body > * {{
position: relative;
z-index: 1;
}}
a {{ color: #ccc; text-decoration: none; }}
a:hover {{ color: #fff; text-decoration: underline; }}
.hero {{
position: relative;
padding: 3rem 2rem 2rem;
text-align: center;
background: transparent;
overflow: visible;
z-index: 10;
}}
.hero::before {{
content: "";
position: absolute;
inset: 0;
background: radial-gradient(ellipse at 50% 0%, rgba(229,9,20,0.12) 0%, transparent 70%);
pointer-events: none;
}}
.hero h1 {{
font-size: 2rem;
font-weight: 700;
color: #fff;
margin-bottom: 0.4rem;
position: relative;
}}
.hero p {{
max-width: 580px;
margin: 0 auto 1rem;
color: #999;
font-size: 0.95rem;
position: relative;
}}
.hero-links {{
display: flex;
gap: 0.75rem;
justify-content: center;
flex-wrap: wrap;
position: relative;
}}
.hero-links a {{
padding: 0.5rem 1.4rem;
border-radius: 4px;
font-size: 0.9rem;
font-weight: 600;
transition: background 0.2s;
}}
.btn-primary {{
background: #333;
color: #e5e5e5;
}}
.btn-primary:hover {{ background: #444; text-decoration: none; }}
.btn-outline {{
border: 1px solid #555;
color: #e5e5e5;
}}
.btn-outline:hover {{ border-color: #e5e5e5; text-decoration: none; }}
.section {{
max-width: 1100px;
margin: 0 auto;
padding: 2rem 1.5rem;
}}
table {{
width: 100%;
border-collapse: collapse;
font-size: 0.88rem;
}}
th {{
text-align: left;
padding: 0.6rem 0.8rem;
border-bottom: 2px solid #333;
color: #999;
font-weight: 600;
font-size: 0.78rem;
text-transform: uppercase;
letter-spacing: 0.05em;
}}
td {{
padding: 0.55rem 0.8rem;
border-bottom: 1px solid #222;
vertical-align: top;
}}
tr:hover {{ background: #1c1c1c; }}
.rank {{ width: 3rem; text-align: center; color: #555; }}
.authors {{ color: #888; font-size: 0.82rem; max-width: 220px; }}
.date {{ white-space: nowrap; color: #666; font-size: 0.82rem; }}
.score-header {{
position: relative;
text-align: right;
cursor: help;
}}
.score-header:hover .score-tip {{
opacity: 1;
visibility: visible;
transform: translateY(0);
pointer-events: auto;
}}
.score-header .score-tip {{
text-transform: none;
letter-spacing: normal;
font-weight: normal;
}}
.tip-line strong.tip-similarity {{ color: #8ab4f8; }}
.tip-line strong.tip-keyword {{ color: #c9a0dc; }}
.tip-line strong.tip-hf {{ color: #f0b27a; }}
.tip-line strong.tip-citation {{ color: #7dcea0; }}
.tip-line strong.tip-penalty {{ color: #e57373; }}
.tip-line.tip-similarity {{ color: #8ab4f8; }}
.tip-line.tip-keyword {{ color: #c9a0dc; }}
.tip-line.tip-hf {{ color: #f0b27a; }}
.tip-line.tip-citation {{ color: #7dcea0; }}
.tip-line.tip-penalty {{ color: #e57373; }}
.score {{
position: relative;
text-align: right;
font-family: "SF Mono", Menlo, Consolas, monospace;
font-size: 0.82rem;
color: #999;
cursor: help;
}}
.score-tip {{
position: absolute;
right: 0;
top: 100%;
width: 280px;
background: #1c1c1c;
border-radius: 6px;
padding: 0.8rem 1rem;
box-shadow: 0 10px 30px rgba(0,0,0,0.6);
z-index: 20;
opacity: 0;
visibility: hidden;
transform: translateY(-6px);
transition: opacity 0.2s ease, visibility 0.2s ease,
transform 0.2s cubic-bezier(.25,.46,.45,.94);
pointer-events: none;
text-align: left;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
Helvetica, Arial, sans-serif;
}}
.score:hover .score-tip {{
opacity: 1;
visibility: visible;
transform: translateY(0);
pointer-events: auto;
}}
.tip-header {{
font-size: 0.75rem;
font-weight: 600;
color: #888;
margin-bottom: 0.4rem;
padding-bottom: 0.3rem;
border-bottom: 1px solid #2a2a2a;
}}
.tip-line {{
font-size: 0.8rem;
color: #bbb;
padding: 0.15rem 0;
line-height: 1.4;
}}
.badge {{
display: inline-block;
background: #252525;
color: #888;
font-size: 0.7rem;
padding: 0.1rem 0.4rem;
border-radius: 3px;
margin-left: 0.4rem;
vertical-align: middle;
}}
#gh-submissions {{
display: none;
margin-top: 2rem;
padding-top: 1.5rem;
border-top: 1px solid #333;
}}
#gh-submissions h2 {{
font-size: 1.1rem;
color: #fff;
margin-bottom: 0.8rem;
}}
#gh-submissions ul {{
list-style: none;
padding: 0;
}}
#gh-submissions li {{
padding: 0.3rem 0;
font-size: 0.88rem;
}}
.footer {{
text-align: center;
padding: 2rem;
color: #555;
font-size: 0.78rem;
border-top: 1px solid #222;
margin-top: 2rem;
}}
/* --- Pipeline tooltip on title --- */
.subtitle-wrap {{
position: relative;
display: inline-block;
}}
.subtitle {{
max-width: 580px;
margin: 0 auto 1rem;
color: #999;
font-size: 0.95rem;
position: relative;
cursor: help;
}}
.how-link {{
font-size: 0.95rem;
font-weight: 600;
color: #f0b27a;
border-bottom: 1px dotted #f0b27a;
margin-left: 0.4rem;
cursor: help;
}}
.subtitle-wrap:hover .how-link {{ color: #f8c88a; }}
.pipeline-tip {{
position: absolute;
top: 100%;
left: 50%;
width: 540px;
max-width: 92vw;
background: #1c1c1c;
border-radius: 6px;
padding: 0.8rem 1rem;
box-shadow: 0 10px 30px rgba(0,0,0,0.6);
z-index: 30;
opacity: 0;
visibility: hidden;
transform: translate(-50%, -6px);
transition: opacity 0.2s ease, visibility 0.2s ease,
transform 0.2s cubic-bezier(.25,.46,.45,.94);