-
Notifications
You must be signed in to change notification settings - Fork 12
/
app.py
281 lines (236 loc) · 7.96 KB
/
app.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
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
from collections.abc import Callable
from functools import partial
from html import unescape
from json import JSONDecodeError, dumps, loads
from urllib.parse import parse_qs, unquote, urlparse
from curl_cffi import CurlError
from lib import logger
from lib.archives import archive_org_data, archive_today_data
from lib.commons import (
ReturnError,
data_to_sfn_cit_ref,
isbn_10or13_search,
uninum2en,
)
from lib.doi import doi_data, doi_search
from lib.googlebooks import google_books_data
from lib.html import (
ALLOW_ALL_ORIGINS,
CSS,
CSS_HEADERS,
CSS_PATH,
DEFAULT_SCR,
JS,
JS_HEADERS,
JS_PATH,
scr_to_html,
)
from lib.isbn_oclc import isbn_data, oclc_data, worldcat_data
from lib.jstor import jstor_data
from lib.ketabir import ketabir_data
from lib.noorlib import noorlib_data
from lib.noormags import noormags_data
from lib.pubmed import pmcid_data, pmid_data
from lib.urls import MAX_RESPONSE_LENGTH, url_data, url_text
def google_encrypted_data(url, parsed_url) -> dict:
if parsed_url[2][:7] in {'/books', '/books/'}:
# sample urls:
# https://encrypted.google.com/books?id=6upvonUt0O8C
# https://www.google.com/books?id=bwfoCAAAQBAJ&pg=PA32
# https://www.google.com/books/edition/_/bwfoCAAAQBAJ?gbpv=1&pg=PA32
return google_books_data(parsed_url)
return url_data(url)
get_resolver = {
'archive': archive_today_data,
'books.google': google_books_data,
'books.google.co': google_books_data,
'books.google.com': google_books_data,
'encrypted.google': google_encrypted_data,
'google': google_encrypted_data,
'jstor': jstor_data,
'ketab': ketabir_data,
'noorlib': noorlib_data,
'noormags': noormags_data,
'web-beta.archive': archive_org_data,
'web.archive': archive_org_data,
'worldcat': worldcat_data,
}.get
http_headers = (
('Content-Type', 'text/html; charset=UTF-8'),
ALLOW_ALL_ORIGINS,
)
json_headers = (
('Content-Type', 'application/json'),
ALLOW_ALL_ORIGINS,
)
def html_data(user_input: dict):
return url_data(user_input['url'], html=user_input['html'])
def url_doi_isbn_data(user_input: str, /) -> dict:
en_user_input = unquote(uninum2en(user_input))
# Checking the user input for dot is important because
# the use of dotless domains is prohibited.
# See: https://features.icann.org/dotless-domains
if '.' in en_user_input:
# Try predefined URLs
# Todo: The following code could be done in threads.
if not (url_input := user_input.startswith('http')):
url = 'http://' + user_input
else:
url = user_input
parsed_url = urlparse(url)
hostname_core = parsed_url[1].rpartition('.')[0].removeprefix('www.')
# todo: make lazy?
if (data_func := get_resolver(hostname_core)) is not None:
if data_func is google_books_data:
return data_func(parsed_url)
elif data_func is google_encrypted_data:
return data_func(url, parsed_url)
return data_func(url)
# DOIs contain dots
if (m := doi_search(unescape(en_user_input))) is not None:
try:
return doi_data(m[0], True)
except JSONDecodeError:
if url_input is False:
raise
# continue with urls_scr
return url_data(url)
# We can check user inputs containing dots for ISBNs, but that sounds
# error-prone.
if (m := isbn_10or13_search(en_user_input)) is not None:
return isbn_data(m[0], True)
raise ValueError('invalid user_input')
BytesTuple = tuple[bytes]
StartResponse = Callable[[str, list[tuple[str, str]]], Callable]
def css(start_response: StartResponse, _) -> BytesTuple:
start_response('200 OK', [*CSS_HEADERS])
return (CSS,)
def js(start_response: StartResponse, _) -> BytesTuple:
start_response('200 OK', [*JS_HEADERS])
return (JS,)
def page_does_not_exist(start_response: StartResponse, _) -> BytesTuple:
start_response(
'404 not found',
[
('Content-Type', 'text/plain'),
],
)
return (b'404 not found',)
def echo(url: str, _: str, /):
try:
url, text = url_text(url)
except Exception as e:
url, text = type(e).__name__, ''
raise ReturnError(url, '', text)
input_type_to_resolver = {
'': url_doi_isbn_data,
'url-doi-isbn': url_doi_isbn_data,
'pmid': pmid_data,
'pmcid': pmcid_data,
'oclc': oclc_data,
'echo': echo,
'html': html_data,
}
def read_body(environ: dict, /) -> bytes:
length = int(environ.get('CONTENT_LENGTH') or 0)
if length > MAX_RESPONSE_LENGTH:
logger.error(f'CONTENT_LENGTH was too long; {length:,} bytes')
return b'' # do not process the input
return environ['wsgi.input'].read(length)
def parse_params(
environ: dict,
) -> tuple[
str,
str,
str,
str | dict, # user_input is dict when input_type == html
list[tuple[str, str]],
Callable[[tuple[str, str, str]], str],
]:
body = read_body(environ)
if body:
get = loads(body).get
return (
get('dateformat') or '%Y-%m-%d',
get('pipeformat') or ' | ',
get('input_type', ''),
get('user_input', ''), # string user_input is trimmed in common.js
[*json_headers],
dumps,
)
query_get = parse_qs(environ['QUERY_STRING']).get
date_format = query_get('dateformat', ('%Y-%m-%d',))[0].strip()
pipe_format = query_get('pipeformat', [' | '])[0].replace('+', ' ')
input_type = query_get('input_type', ('',))[0]
return (
date_format,
pipe_format,
input_type,
query_get('user_input', ('',))[0].strip(),
# for the bookmarklet; also if user directly goes to query page
[*http_headers],
partial(
scr_to_html,
date_format=date_format,
pipe_format=pipe_format,
input_type=input_type,
),
)
def root(start_response: StartResponse, environ: dict) -> BytesTuple:
(
date_format,
pipe_format,
input_type,
user_input,
headers,
scr_to_resp_body,
) = parse_params(environ)
if not user_input:
response_body = scr_to_html(
DEFAULT_SCR, date_format, pipe_format, input_type
).encode()
start_response('200 OK', [*http_headers])
return (response_body,)
data_func = input_type_to_resolver[input_type]
try:
d = data_func(user_input)
except Exception as e:
status = '500 Internal Server Error'
if isinstance(e, ReturnError):
scr = e.args
else:
if not isinstance(e, CurlError):
logger.exception(user_input)
scr = type(e).__name__, '', ''
else:
scr = data_to_sfn_cit_ref(d, date_format, pipe_format)
status = '200 OK'
response_body = scr_to_resp_body(scr).encode()
start_response(status, headers)
return (response_body,)
get_handler: Callable[[str], Callable[[StartResponse, dict], BytesTuple]] = {
f'/{CSS_PATH}.css': css,
f'/{JS_PATH}.js': js,
'/': root,
'/citer.fcgi': root, # for backward compatibility
}.get # type: ignore
def app(environ: dict, start_response: StartResponse) -> BytesTuple:
# noinspection PyBroadException
try:
return (get_handler(environ['PATH_INFO']) or page_does_not_exist)(
start_response, environ
)
except Exception:
start_response('500 Internal Server Error', [])
logger.exception('app error, environ:\n%s', environ)
return (b'Unknown Error',)
if __name__ == '__main__':
# note that app.py is not run as '__main__' in kubernetes
# only for local computer
from wsgiref.simple_server import make_server
httpd = make_server('localhost', 5000, app)
print('serving on http://localhost:5000')
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass