-
Notifications
You must be signed in to change notification settings - Fork 0
/
articlesummary.py
executable file
·212 lines (159 loc) · 5.47 KB
/
articlesummary.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
#!/home/iaross/miniconda3/bin/python
from easydict import easydict
import time
import json
import sys
import html
import requests
import urllib.parse
from typing import List
def jload(fn: str) -> dict:
return easydict(json.load(open(fn)))
def plural_s(n: str) -> str:
return "" if n == 1 else "s"
_html_template = u"""\
<!DOCTYPE html>
<html>
<head>
<title>{_title}</title>
<style>
body {{ font-family: monospace }}
.ar {{ text-align: right }}
</style>
</head>
<body>
<h2>Article Count by Source</h2>
<p>Total Articles: {all.hits.total:,}</p>
<p>Last {since}: {day.hits.total:,}</p>
<p>
<table border=1>
<tr>
<th>Source</th>
<th>Last {since}</th>
<th>Total</th>
</tr>
{_source_table_rows}
</table>
</p>
<hr/>
<h2>Article Count by Pubname / Source</h2>
<p>Total Articles: {all.hits.total:,}</p>
<p>Last {since}: {day.hits.total:,}</p>
<p>
<table border=1>
<tr>
<th>Pubname</th>
<th>Source</th>
<th>Last {since}</th>
<th>Total</th>
</tr>
{_pubsrc_table_rows}
</table>
</p>
<h2>API Response checks</h2>
<p>
<table border=1>
<tr>
<th>URL</th>
<th>Name</th>
<th>Outcome</th>
</tr>
{_endpoints}
</table>
</p>
</body>
</html>
"""
def dcmap(xx) -> dict:
return { x.key: x.doc_count for x in xx.buckets }
def bsort(xx, mapby):
def key(b):
return (-mapby.get(b.key, 0), -b.doc_count, b.key)
return sorted(xx.buckets, key=key)
def mk_source_table_rows(sources, sources_24h) -> List[List]:
map24h = dcmap(sources_24h.aggregations.sources)
return [ [s.key, map24h.get(s.key, 0), s.doc_count]
for s in bsort(sources.aggregations.sources, map24h) ]
def mk_pubsrc_table_rows(pubsrc, pubsrc_24h) -> List[List]:
totals = { (p.key, s.key): s.doc_count
for p in pubsrc.aggregations.pubnames.buckets
for s in p.sources.buckets }
return [ [p.key, s.key, s.doc_count, totals.get((p.key, s.key), 0)]
for p in pubsrc_24h.aggregations.pubnames.buckets
for s in p.sources.buckets ]
def mk_td(s: str) -> str:
if isinstance(s, int):
fmt = u"<td class='ar'>{:,}</td>"
else:
fmt = u"<td>{}</td>"
s = html.escape(s)
return fmt.format(s)
def mk_table_row(row: List) -> str:
return u"<tr>\n%s\n</tr>" % u"\n".join( mk_td(cell) for cell in row )
def mk_table_rows(matrix: List[List]) -> str:
return u"\n".join( mk_table_row(row) for row in matrix )
def mk_email(since: str, sources_out: str, sources_24h_out: str, pubsrc_out: str, pubsrc_24h_out: str):
now = time.time()
dstamp = time.strftime("%F", time.localtime(now))
tstamp = time.strftime("%F %H:%M", time.localtime(now))
desc = "Source and Pubname Summary"
subject = "xDD New Document Summary for last %s as of %s" % (since, dstamp)
s = jload(sources_out)
s24 = jload(sources_24h_out)
p = jload(pubsrc_out)
p24 = jload(pubsrc_24h_out)
e = easydict()
e.all = s
e.day = s24
e.since = since
e._source_table_rows = mk_table_rows(mk_source_table_rows(s, s24))
e._pubsrc_table_rows = mk_table_rows(mk_pubsrc_table_rows(p, p24))
e._title = subject
e._endpoints = check_endpoints()
html = _html_template.format(**e)
print("Subject: %s" % subject)
print()
print(html)
# print(check_endpoints())
# s.hits.total
# s.aggregations.sources.buckets[0].key
# s.aggregations.sources.buckets[0].doc_count
# p.hits.total
# p.aggregations.pubnames.buckets[0].key
# p.aggregations.pubnames.buckets[0].doc_count
# p.aggregations.pubnames.buckets[0].sources.buckets[0].key
# p.aggregations.pubnames.buckets[0].sources.buckets[0].doc_count
def check_endpoint(url: str, expected_n: int=None, json: bool=True) -> str:
resp = requests.get(url)
if resp.status_code != 200:
return "Error! Non-200 status code returned."
elif json and "error" in resp.json():
return "Error! API returned a non-success."
elif json and expected_n is not None and "success" in resp.json() and len(resp.json()['success']['data']) != expected_n:
return f"Error! Unexpected number of results returned. Expected {expected_n} and got {len(resp.json()['success']['data'])}"
return "Success"
def check_endpoints() -> str:
"""
Check some xDD public-facing endpoints; ensure a 200 status code and, where appropriate, quickly check results.
"""
endpoints = [
("Articles route - basic search", "https://xdd.wisc.edu/api/articles?term=test&max=10", 10, True),
("Articles route - scan and scroll", "https://xdd.wisc.edu/api/articles?term=test&full_results=true&per_page=500", 500, True),
("Journals route", "https://xdd.wisc.edu/api/journals?all", None, True),
("Dictionaries route", "https://xdd.wisc.edu/api/dictionaries?all", None, True),
("Basic metrics", "https://xdd.wisc.edu/api/metrics/basic", 4, True),
("Website", "https://xdd.wisc.edu", None, False),
("xdd-covid-19 - word2vec", "https://xdd.wisc.edu/sets/xdd-covid-19/word2vec/api/most_similar?word=test", 10, True),
("xdd-covid-19 - COSMOS", "https://xdd.wisc.edu/sets/xdd-covid-19/cosmos/api/", None, False),
("COSMOS - set visualizer", "https://xdd.wisc.edu/set_visualizer/", None, False),
]
results = []
for check, url, n_max, json in endpoints:
results.append([check, url, check_endpoint(url, n_max, json)])
return mk_table_rows(results)
# table_results()
# return ""
def main():
mk_email(*sys.argv[1:])
if __name__ == '__main__':
main()