-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
231 lines (200 loc) · 6.99 KB
/
main.py
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
import html
import logging
import re
from html.parser import HTMLParser
import httpx
from fastapi import FastAPI, Header
from fastapi.exceptions import RequestValidationError
from fastapi.responses import HTMLResponse, JSONResponse, PlainTextResponse
from pydantic import BaseModel
from pydantic.error_wrappers import ErrorWrapper
from utils import sanitize_html, strip_tags
class Publication(BaseModel):
url: str
title: str | None
year: int | None
journal: str | None
volume: str | None
issue: str | None
pages: str | None
authors: str | None
def as_text(self):
parts = [
(self.authors if self.authors is not None else "N.N.")
+ " ("
+ (str(self.year) if self.year is not None else "n.d.")
+ ")"
]
if self.title is not None:
parts.append(strip_tags(self.title))
if self.journal is not None:
text = self.journal
if self.volume is not None:
text += ", " + self.volume
if self.issue is not None:
text += "(" + self.issue + ")"
if self.pages is not None:
text += ", " + self.pages
parts.append(text)
parts.append(self.url)
return ". ".join(parts)
def as_html(self):
parts = [
(html.escape(self.authors) if self.authors is not None else "N.N.")
+ " ("
+ (str(self.year) if self.year is not None else "n.d.")
+ ")"
]
if self.title is not None:
parts.append(self.title)
if self.journal is not None:
text = "<i>" + html.escape(self.journal) + "</i>"
if self.volume is not None:
text += ", <i>" + html.escape(self.volume) + "</i>"
if self.issue is not None:
text += "(" + html.escape(self.issue) + ")"
if self.pages is not None:
text += ", " + html.escape(self.pages)
parts.append(text)
parts.append(
'<a href="' + html.escape(self.url) + '">' + html.escape(self.url) + "</a>"
)
return ". ".join(parts)
def as_json(self):
return {
"url": self.url,
"title": self.title,
"year": self.year,
"journal": self.journal,
"volume": self.volume,
"issue": self.issue,
"pages": self.pages,
"authors": self.authors,
}
app = FastAPI()
logger = logging.getLogger(__name__)
def format_authors(authors: list) -> str:
if len(authors) <= 2:
return " & ".join(authors)
return authors[0] + " et al."
async def fetch_crossref(doi: str) -> Publication:
url = f"https://api.crossref.org/works/{doi}"
try:
logger.info(f"querying {url}")
async with httpx.AsyncClient() as client:
response = await client.get(url)
response.raise_for_status()
data = response.json()["message"]
except:
logger.exception(f"querying {url} failed")
raise
try:
title = sanitize_html(data["title"][0])
except (KeyError, IndexError):
title = None
logger.warning(f"no title in {url}")
try:
year = data["published"]["date-parts"][0][0]
except (KeyError, IndexError):
year = None
logger.warning(f"no year in {url}")
try:
journal = strip_tags(data["short-container-title"][0])
except (KeyError, IndexError):
try:
journal = strip_tags(data["container-title"][0])
except (KeyError, IndexError):
journal = None
logger.warning(f"no journal in {url}")
try:
volume = data["volume"]
except (KeyError, IndexError):
volume = None
logger.warning(f"no volume in {url}")
try:
issue = data["issue"]
except (KeyError, IndexError):
issue = None
logger.warning(f"no issue in {url}")
try:
pages = data["page"]
except (KeyError, IndexError):
pages = None
logger.warning(f"no pages in {url}")
try:
authors = format_authors([author["family"] for author in data["author"]])
except (KeyError, IndexError):
authors = None
logger.warning(f"no authors in {url}")
return Publication(
url=f"https://doi.org/{doi}",
title=title,
year=year,
journal=journal,
volume=volume,
issue=issue,
pages=pages,
authors=authors,
)
class MyHTMLParser(HTMLParser): # pylint: disable=W0223
def __init__(self):
super().__init__()
self.title = None
self.year = None
self.authors = []
def handle_starttag(self, tag, attrs):
attrs = dict(attrs)
if tag == "meta":
if attrs.get("name") == "citation_title":
self.title = attrs.get("content")
elif attrs.get("name") == "citation_date":
if date := attrs.get("content"):
if match := re.search(r"\d{4}", date):
self.year = match[0]
elif attrs.get("name") == "citation_author":
if author := attrs.get("content"):
last_name = author.split(",")[0].strip()
self.authors.append(last_name)
async def fetch_url(url: str) -> Publication:
try:
logger.info(f"querying {url}")
async with httpx.AsyncClient() as client:
response = await client.get(url, follow_redirects=True)
response.raise_for_status()
parser = MyHTMLParser()
parser.feed(response.text)
return Publication(
url=url,
title=sanitize_html(parser.title),
year=parser.year,
authors=format_authors(parser.authors),
)
except:
logger.exception(f"querying {url} failed")
raise
DOI_RE = r"((https?://)?(dx\.)?doi\.org/|doi:)(?P<doi>.*)"
HDL_RE = r"((https?://)?hdl\.handle\.net/|hdl:)(?P<hdl>.*)"
def render(publication: Publication, accept: str) -> PlainTextResponse:
renderers = {
"text/plain": lambda: PlainTextResponse(publication.as_text()),
"text/html": lambda: HTMLResponse(publication.as_html()),
"application/json": lambda: JSONResponse(publication.as_json()),
}
for media_type in accept.split(","):
media_type = media_type.split(";")[0].strip()
if media_type in renderers:
return renderers[media_type]()
return renderers["text/plain"]()
@app.get("/")
async def root(uri: str, accept: str = Header(default="text/plain")):
if match := re.match(DOI_RE, uri):
return render(await fetch_crossref(match["doi"]), accept)
if match := re.match(HDL_RE, uri):
return render(await fetch_url("https://hdl.handle.net/" + match["hdl"]), accept)
raise RequestValidationError(
[
ErrorWrapper(
ValueError("expected doi.org or hdl.handle.net URI"), ("query", "uri")
)
]
)