-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadsq.py
More file actions
executable file
·297 lines (216 loc) · 7.55 KB
/
adsq.py
File metadata and controls
executable file
·297 lines (216 loc) · 7.55 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
#!/usr/bin/python3
# Written by Fergus Baker 2025 Copyleft GPL 3.0
__doc__ = """
Search or fetch BibTeX entries from NASA/ADS.
If the terms provided are `bibcode`, this program will fetch the BibTeX
entries. Everything else will be treated as a search query.
!!! note
You need to have a NASA/ADS token exported to the `ADS_TOKEN` environment
variable, if you want to do NASA/ADS things.
See the NASA/ADS API documentation for how to get one:
https://ui.adsabs.harvard.edu/help/api/
"""
import argparse
import logging
import json
import dataclasses
import urllib.request
import urllib.parse
import http.client
import re
import os
from enum import Enum
ADS_QUERY_URL = "https://api.adsabs.harvard.edu/v1/search/query"
ADS_EXPORT_BIBTEX_URL = "https://api.adsabs.harvard.edu/v1/export/bibtex"
ADS_TOKEN = os.environ.get("ADS_TOKEN")
ADS_BIBTEX_URL = "https://api.adsabs.harvard.edu/v1/export/bibtex"
MAX_AUTHORS = 4
BIBCODE_PATTERN = r"^[a-zA-Z0-9]+\.+[a-zA-Z0-9]+\.+[a-zA-Z0-9]+$"
logger = logging.getLogger(__name__)
class InvalidQuery(Exception): ...
def _get_auth_header() -> dict[str, str]:
if not ADS_TOKEN:
print(
"""
Error: NO ADS TOKEN
Please export an ADS API access token to the `ADS_TOKEN` environment
variable. On most shells this can be done with:
export ADS_TOKEN="..."
You can get a token (for free) by following the instructions here:
https://ui.adsabs.harvard.edu/help/api/
DO NOT SHARE YOUR TOKEN WITH ANYONE.
"""
)
exit(1)
return {"Authorization": "Bearer " + ADS_TOKEN}
def _url_escape(s: str) -> str:
return urllib.parse.quote_plus(s, safe="^")
@dataclasses.dataclass
class ADSQuery:
terms: None | list[str] = None
authors: None | list[str] = None
year: None | str = None
database: None | str = None
def is_valid(self) -> bool:
# year enough is not enough to perform a search
return self.authors or self.terms
def format_ads(self) -> str:
q = []
if self.terms:
q.append(_url_escape(" ".join(self.terms)))
if self.authors:
for author in self.authors:
q.append("author:" + _url_escape(author))
if self.year:
q.append("year:" + _url_escape(self.year))
if self.database:
q.append("database:" + _url_escape(self.database))
return "&fq=".join(q)
def _canonical_name(author: str) -> str:
return " ".join(i.strip() for i in reversed(author.split(",")))
def pretty_print_doc(doc: dict):
_done = set()
s: list[tuple[str, list[str]]] = []
if "author" in doc:
authors = doc["author"]
formatted_authors = [
_canonical_name(i) for i in authors[0 : min(len(authors), MAX_AUTHORS)]
]
if len(authors) > MAX_AUTHORS:
formatted_authors.append("et al.")
author = "; ".join(formatted_authors)
s.append(("Author", [author]))
_done.add("author")
if "date" in doc:
date = doc["date"]
s.append(("Date", [date]))
_done.add("date")
if "title" in doc:
title = "; ".join(doc["title"])
s.append(("Title", [title]))
_done.add("title")
if "bibcode" in doc:
bibcode = doc["bibcode"]
s.append(("Bibcode", [bibcode]))
s.append(("URL", [f"https://ui.adsabs.harvard.edu/abs/{bibcode}/abstract"]))
_done.add("bibcode")
if "links_data" in doc:
links = []
for link in doc["links_data"]:
l = json.loads(link)
link_type = l["type"]
url = l["url"]
access = l["access"]
suffix = ""
if access:
suffix = f"({access})"
links.append(f"{link_type} {suffix} {url}")
s.append(("Links", links))
_done.add("links_data")
for k, v in doc.items():
if not k in _done:
if isinstance(v, list):
s.append((k, v))
else:
s.append((k, [v]))
padding = max(len(i[0]) for i in s) + 2
space = " " * padding
text = ""
for k, lines in s:
text += k.rjust(padding) + ": "
if len(lines) > 1:
for i, v in enumerate(lines):
if i == 0:
text += "- " + str(v) + "\n"
else:
text += space + " - " + str(v) + "\n"
else:
text += str(lines[0]) + "\n"
print(text)
def _ads_search_query(query: str, fields: str) -> http.client.HTTPResponse:
encoded_query = "?q=" + query + "&fl=" + fields
req_url = ADS_QUERY_URL + encoded_query
logger.debug("Making query: %s", req_url)
req = urllib.request.Request(req_url, headers=_get_auth_header())
return urllib.request.urlopen(req)
def _ads_export_bibcode(bibcode: list[str]) -> str:
req_url = ADS_EXPORT_BIBTEX_URL
body = json.dumps({"bibcode": bibcode}).encode()
logger.debug("Making query: %s", req_url)
req = urllib.request.Request(req_url, headers=_get_auth_header(), data=body)
return urllib.request.urlopen(req)
def ads_search(query: str, fields: str) -> str:
resp = _ads_search_query(query, fields)
return resp.read().decode()
def ads_export(bibcode: list[str]) -> str:
resp = _ads_export_bibcode(bibcode)
return resp.read().decode()
def run_query(query: ADSQuery, fields: str, as_json: bool):
logger.debug("Query object: %s", query)
if not query.is_valid():
raise InvalidQuery("Not enough information for query supplied.")
q = query.format_ads()
logger.debug("Formatted query: %s", q)
data = ads_search(q, fields)
if as_json:
print(data)
return
d = json.loads(data)
docs = d["response"]["docs"]
for doc in sorted(docs, key=lambda x: x.get("citation_count", 0)):
pretty_print_doc(doc)
def is_bibcode(term: str) -> bool:
return re.match(BIBCODE_PATTERN, term) is not None
def fetch_bibtex(terms: list[str], as_json: bool):
logger.debug("Fields: %s", terms)
data = ads_export(terms)
if as_json:
print(data)
return
d = json.loads(data)
print(d["export"].strip())
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog="adsq",
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
"-a", "--author", action="append", help="Author name, in standard ADS format."
)
parser.add_argument("-y", "--year", default=None)
parser.add_argument("--loglevel", default="warning", help="Set the logging level.")
parser.add_argument(
"--json",
action="store_true",
help="Output fetched JSON.",
)
parser.add_argument(
"-b",
"--bibcode",
action="store_true",
help="Force interpretation of the argument as a bibcode",
)
parser.add_argument("terms", nargs="*")
parser.add_argument(
"--fields",
help="Which fields to request",
default="author,date,pub,title,bibcode,citation_count,links_data",
)
parser.add_argument(
"--database",
help="Which databse to request from.",
default="astronomy",
)
args = parser.parse_args()
logging.basicConfig(level=args.loglevel.upper())
if (
not args.author
and not args.year
and args.terms
and (all(is_bibcode(i) for i in args.terms) or args.bibcode)
):
fetch_bibtex(args.terms, args.json)
else:
query = ADSQuery(args.terms, args.author, args.year, args.database)
run_query(query, args.fields, args.json)