-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMatchup.py
279 lines (232 loc) · 10.3 KB
/
Matchup.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
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
#
# Matchups between new reports and papers already in CiteUlike
import csv
import urllib.parse
import yattag
import Papers
import HistoryDB
class Matchup(object):
"""
Pairs titles (and the list of papers with that title) and CUL entries with that title
"""
def __init__(self, papers, culEntries):
self.papers = papers
self.culEntries = culEntries # might be None
self.lowerTitle = papers[0].getTitleLower()
self.title = papers[0].getTitle()
return None
def debugPrint(self, descrip="", indent=""):
print(indent + "DEBUG: Matchup: " + descrip)
print(indent + " lowerTitle: " + self.lowerTitle)
print(indent + " title: " + self.title)
print(indent + " papers: ")
for paper in self.papers:
paper.debugPrint(indent=indent + " ")
print(indent + " culEntries: ")
if self.culEntries:
for culEntry in self.culEntries:
culEntry.debugPrint(indent=indent + " ")
print(indent + " DONE")
return(None)
def getDoiFromPapers(self):
"""
List is assumed to have been pre-verified to have consistent DOIs
"""
for paper in self.papers:
if paper.doi:
return(paper.doi)
return(None)
def getPapersSortedBySearch(self):
"""
Return list of matchup's papers, sorted by search string.
Used to produce output in the same order every time.
"""
return(sorted(self.papers, key=lambda paper: paper.search))
def getPapersLongestTitel(self):
"""
Get the longest title of any matching search for this paper.
Shy the longest? Google sometime's truncates title.
"""
longest = ""
for paper in self.papers:
if len(paper.title) > len(longest):
longest = paper.title
return(longest)
def createReport(matchupsByLowTitle, sectionTitle):
"""
Return an HTML report of what needs to be done.
"""
doc, tag, text = yattag.Doc().tagtext()
with tag("h2", style="width: 100%; background-color: #eeeeff"):
text(sectionTitle)
for matchup in matchupsByLowTitle.values():
with tag("h3"):
text(matchup.papers[0].title)
with tag("ol"):
for paper in matchup.papers:
with tag("li"):
text(paper.search)
with tag("ul"):
with tag("li"):
text(paper.authors)
if paper.doi:
with tag("li"):
with tag("a", href=paper.doiUrl, target="_blank"):
text(paper.doi)
with tag("li"):
text(paper.source)
with tag("li"):
text(paper.title)
if matchup.culEntries:
for culEntry in matchup.culEntries:
with tag("p"):
with tag("a", href=culEntry.getCulUrl()):
text("Paper @ CiteULike")
else:
with tag("ul"):
url = Papers.getUrlFromPaperList(matchup.papers)
if url:
# Got a url, post it to CiteULike, and link to it.
with tag("li"):
with tag("a", href="http://www.citeulike.org/posturl?url=" + url,
target="citeulike"):
text("Submit to CiteULike")
with tag("li"):
with tag("a", href=url, target="paper"):
text("See paper")
hopkinsUrl = Papers.getHopkinsUrlFromPaperList(matchup.papers)
if hopkinsUrl:
with tag("li"):
with tag("a", href=hopkinsUrl, target="paperhopkins"):
text("See paper @ Hopkins")
# Search for it at Hopkins; Google and pubmed too
with tag("li"):
with tag("a",
href="https://catalyst.library.jhu.edu/?utf8=%E2%9C%93&search_field=title&" +
urllib.parse.urlencode({"q": matchup.title}),
target="jhulib"):
text("Search Hopkins")
with tag("li"):
with tag("a",
href="https://www.google.com/search?q=" + matchup.title,
target="googletitlesearch"):
text("Search Google")
with tag("li"):
with tag("a",
href="https://scholar.google.com/scholar?q=" + matchup.title,
target="googlescholarsearch"):
text("Search Google Scholar")
with tag("li"):
with tag("a",
href="http://www.ncbi.nlm.nih.gov/pubmed/?term=" + matchup.title,
target="pubmedtitlesearch"):
text("Search Pubmed")
reportHtml = yattag.indent(doc.getvalue())
# do some cleanup
# fix a problem with some Google Scholar URLs. Google Scholar does not like & in place of &
reportHtml = reportHtml.replace("&", "&") # potentially risky outside of URLs
return(reportHtml)
def reportPaper(matchup, history):
"""
Return HTML report for this matchup
"""
if not hasattr(reportPaper, "newCounter"):
reportPaper.newCounter = 0 # it doesn't exist yet, so initialize it
reportPaper.knownCounter = 0
doc, tag, text = yattag.Doc().tagtext()
newPaper = not matchup.culEntries
if newPaper:
# reported paper already in CiteULike
reportPaper.newCounter += 1
bgColor = "#eef"
fontColor = "#000"
# if we saw this paper in the previous run, then it is newish.
leader = "New"
if history:
historyEntry = history.getByTitleLower(matchup.lowerTitle)
if not historyEntry:
historyEntry = history.getByDoi(matchup.getDoiFromPapers())
if historyEntry:
bgColor = "#ee8"
leader = "Newish [" + historyEntry[HistoryDB.COMMENTS] + "] "
leader += " (#" + str(reportPaper.newCounter) + "):"
hLevel = "h2"
else:
# report paper is known
reportPaper.knownCounter += 1
bgColor = "#ccc"
fontColor = "#666" # evil, very
leader = "Known (#" + str(reportPaper.knownCounter) + "):"
hLevel = "h3"
with tag("div", style="width: 100%; color: " + fontColor + "; background-color: " + bgColor):
with tag(hLevel):
text(leader)
with tag("br"):
pass
text(matchup.papers[0].title)
with tag("ol"):
for paper in matchup.getPapersSortedBySearch():
with tag("li"):
text(paper.search)
with tag("ul"):
with tag("li"):
text(paper.authors)
if paper.doi:
with tag("li"):
with tag("a", href=paper.doiUrl, target="_blank"):
text(paper.doi)
with tag("li"):
text(paper.source)
with tag("li"):
text(paper.title)
if not newPaper:
for culEntry in matchup.culEntries:
with tag("p"):
with tag("a", href=culEntry.getCulUrl()):
text("Paper @ CiteULike")
else:
with tag("ul"):
url = Papers.getUrlFromPaperList(matchup.papers)
if url:
# Got a url, post it to CiteULike, and link to it.
with tag("li"):
with tag("a", href="http://www.citeulike.org/posturl?url=" + url,
target="citeulike"):
text("Submit to CiteULike")
with tag("li"):
with tag("a", href=url, target="paper"):
text("See paper")
hopkinsUrl = Papers.getHopkinsUrlFromPaperList(matchup.papers)
if hopkinsUrl:
with tag("li"):
with tag("a", href=hopkinsUrl, target="paperhopkins"):
text("See paper @ Hopkins")
# Search for it at Hopkins; Google and pubmed too
with tag("li"):
with tag("a",
href="https://catalyst.library.jhu.edu/?utf8=%E2%9C%93&search_field=title&" +
urllib.parse.urlencode({"q": matchup.title}),
target="jhulib"):
text("Search Hopkins")
with tag("li"):
with tag("a",
href="https://www.google.com/search?q=" + matchup.title,
target="googletitlesearch"):
text("Search Google")
with tag("li"):
with tag("a",
href="https://scholar.google.com/scholar?q=" + matchup.title,
target="googlescholarsearch"):
text("Search Google Scholar")
with tag("li"):
with tag("a",
href="http://www.ncbi.nlm.nih.gov/pubmed/?term=" + matchup.title,
target="pubmedtitlesearch"):
text("Search Pubmed")
reportHtml = yattag.indent(doc.getvalue())
# do some cleanup
# fix a problem with some Google Scholar URLs. Google Scholar does not like & in place of &
reportHtml = reportHtml.replace("&", "&") # potentially risky outside of URLs
return(reportHtml)