Repository navigation
Expand file tree
/
Copy pathgenerate_publist.py
More file actions
188 lines (156 loc) · 6.42 KB
/
Copy pathgenerate_publist.py
File metadata and controls
188 lines (156 loc) · 6.42 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
#!/usr/bin/env python3
"""Generate the publications page for garrethmartin.github.io.
Fetches papers from an ADS library and writes _posts/2019-04-06-Publications.md.
Run from the repo root: python generate_publist.py
"""
import json
import os
import re
import requests
from collections import defaultdict
ADS_TOKEN = "7rbhCKGe8EGuSLM3XpV2RZ1v5RpjvnOqUgb13X7i"
LIBRARY_ID = "nThU2Yw3SUytqSYjksZ8uA"
OUT_FILE = "_posts/2019-04-06-Publications.md"
HEADERS = {"Authorization": f"Bearer {ADS_TOKEN}"}
SCHOLAR_CACHE = os.path.join(os.path.expanduser("~/Code/CV_stuff"), "scholar_cache.json")
# ADS records still carry authors' former names; override the display form here
# without touching the underlying ADS query (author search strings stay as-is).
AUTHOR_NAME_OVERRIDES = {
"butler, j": "Butler, M.",
"butler, joseph": "Butler, Madeline",
}
def apply_author_override(name):
key = name.strip().lower().rstrip(".")
return AUTHOR_NAME_OVERRIDES.get(key, name)
def latex_to_utf8(text):
replacements = {
r"\'a": "á", r'\"a': "ä", r"\'e": "é", r'\"e': "ë",
r"\'i": "í", r'\"i': "ï", r"\'o": "ó", r'\"o': "ö",
r"\'u": "ú", r'\"u': "ü", r"\~n": "ñ", r"\c{c}": "ç",
r"\'A": "Á", r'\"A': "Ä", r"\&": "&", r"``": "“", r"''": "”",
}
for latex, char in replacements.items():
text = text.replace(latex, char)
return text
def load_scholar_data(path):
try:
with open(path) as f:
data = json.load(f)
except (OSError, json.JSONDecodeError) as e:
print(f"WARNING: could not load scholar cache ({e}); skipping Scholar stats")
return {}
return data
def is_catalog(p):
doctype = (p.get("doctype") or "").lower()
journal = ((p.get("bibstem") or [""])[0] if p.get("bibstem") else (p.get("pub") or "")).lower()
title = ((p.get("title") or [""])[0]).lower()
return (
doctype == "catalog"
or journal.startswith("ycat")
or "vizier online data catalog" in title
)
def format_paper(p):
raw_title = (p.get("title") or [""])[0]
title = f"*{latex_to_utf8(raw_title)}*"
authors = [apply_author_override(latex_to_utf8(a)) for a in (p.get("author") or [])]
authors = [f"**{a}**" if "Martin, G" in a else a for a in authors]
author_str = ", ".join(authors)
year = p.get("year") or "n.d."
date = p.get("pubdate") or ""
month = date.split("-")[1] if date else ""
bibstem = p.get("bibstem") or []
journal = bibstem[0] if bibstem else (p.get("pub") or "")
volume = p.get("volume") or ""
pages = p.get("page") or []
page = pages[0] if pages else ""
dois = p.get("doi") or []
doi = dois[0] if dois else ""
ref = f"{journal} {volume}, {page}".strip()
ref_str = f"[{ref}](https://doi.org/{doi})" if doi else ref
return f"{title} \n{author_str} \n{month}/{year} | {ref_str}"
def main():
scholar_data = load_scholar_data(SCHOLAR_CACHE)
lib_url = f"https://api.adsabs.harvard.edu/v1/biblib/libraries/{LIBRARY_ID}"
lib_resp = requests.get(lib_url, headers=HEADERS, params={"start": 0, "rows": 500})
lib_resp.raise_for_status()
solr = lib_resp.json()["solr"]["response"]
bibcodes = [d["bibcode"] for d in solr["docs"]]
print(f"Library contains {solr['numFound']} docs, fetching {len(bibcodes)}.")
query = " OR ".join(f"bibcode:{bc}" for bc in bibcodes)
search_resp = requests.get(
"https://api.adsabs.harvard.edu/v1/search/query",
headers=HEADERS,
params={
"q": query,
"fl": "title,author,pubdate,year,pub,bibstem,volume,page,doi,doctype",
"rows": len(bibcodes),
},
)
search_resp.raise_for_status()
all_papers = search_resp.json()["response"]["docs"]
first_author_papers = []
all_by_year = defaultdict(list)
for p in all_papers:
if is_catalog(p):
continue
year = p.get("year") or "0"
date = p.get("pubdate") or ""
month = int(date.split("-")[1]) if date and "-" in date else 0
formatted = format_paper(p)
authors = [latex_to_utf8(a) for a in (p.get("author") or [])]
if authors and authors[0].startswith("Martin, G"):
first_author_papers.append((year, month, formatted))
all_by_year[year].append((month, formatted))
first_author_papers.sort(key=lambda x: (x[0], x[1]), reverse=True)
for year in all_by_year:
all_by_year[year].sort(key=lambda x: x[0], reverse=True)
n_papers = sum(len(v) for v in all_by_year.values())
sep = " ■ "
if scholar_data:
stats_parts = [
f"{n_papers} peer-reviewed publications and preprints",
f"{scholar_data['citedby']:,} citations (Scholar)",
f"h-index {scholar_data['hindex']} (Scholar)",
f"i10-index {scholar_data['i10index']} (Scholar)",
__import__('datetime').date.today().strftime('%B %Y'),
]
else:
stats_parts = [
f"{n_papers} peer-reviewed publications and preprints",
"citations",
"h-index",
__import__('datetime').date.today().strftime('%B %Y'),
]
stats_line = f"*{sep.join(stats_parts)}*"
lines = [
"---",
"layout: post",
"title: Publications",
"cover: NHz.png",
"date: 2013-12-09 12:00:00",
"categories: posts",
'icon: "📄"',
"---",
"",
"[`ADS Library`](https://ui.adsabs.harvard.edu/public-libraries/nThU2Yw3SUytqSYjksZ8uA \"ADS library\")"
f"{sep}[`Google Scholar`](https://scholar.google.com/citations?user=4O8TNrgAAAAJ \"Google Scholar\")"
f"{sep}[`Publication list (PDF)`](/files/publication_list.pdf \"Publication list\")",
"",
stats_line,
"",
"## First-author publications",
"",
]
for _, _, entry in first_author_papers:
lines.append(entry + "\n")
lines += ["", "## All peer-reviewed publications and preprints", ""]
for year in sorted(all_by_year.keys(), reverse=True):
lines.append(f"### {year}\n")
for _, entry in all_by_year[year]:
indented = "\n".join(">" + line for line in entry.split("\n"))
lines.append(indented + "\n")
with open(OUT_FILE, "w") as f:
f.write("\n".join(lines))
print(f"Written to {OUT_FILE} ({n_papers} papers after filtering)")
if __name__ == "__main__":
main()