-
Notifications
You must be signed in to change notification settings - Fork 0
/
mine-pubs
executable file
·177 lines (148 loc) · 5.64 KB
/
mine-pubs
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
#!/usr/bin/env python
import codecs
import locale
import sys
import urllib
import logging
import textwrap
from argparse import ArgumentParser
from infoscience import InfoscienceEntry, InfoscienceParser
from string import Template
html_standalone_template = Template("""\
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>$title</title>
<link href="dslab-infoscience-common.css" rel="stylesheet" type="text/css"/>
<link href="dslab-standalone.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<h1>$title</h1>
<div class="iscollection" id="paperlist">
$content
</div>
</body>
</html>
""")
html_embedded_template = Template("""\
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>$title</title>
<link href="dslab-infoscience-common.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="iscollection" id="paperlist">
$content
</div>
</body>
</html>
""")
html_year_header = Template("""\
<h4 class="isyear">$year</h4>
""")
html_record_url = Template("""\
<div class="isrecord">
<span class="istitle"><a href="$url">$title</a></span>.
<span class="isauthors">$authors</span>
</div>
""")
html_record_plain = Template("""\
<div class="isrecord">
<span class="istitle">$title</span>.
<span class="isauthors">$authors</span>
</div>
""")
base_infoscience_url = "http://infoscience.epfl.ch/search"
search_cc = "Infoscience/Research/IC/IIF/DSLAB"
search_p = ""
search_of = "xm"
def _formatAuthors(authors):
aexp = [a[1] + " " + a[0] for a in authors]
if len(aexp) > 2:
astr = ", ".join(aexp[0:-1]) + ", and " + aexp[-1]
elif len(aexp) == 2:
astr = " and ".join(aexp)
else:
astr = aexp[0]
return astr
def printText(collection):
years = set([entry.year for entry in collection.itervalues()])
for year in sorted(years, reverse=True):
print "%d: " % year
entries = filter(lambda entry: entry.year == year, collection.itervalues())
for entry in sorted(entries, key=lambda entry: entry.id, reverse=True):
print "* '%s', %s" % (entry.title, _formatAuthors(entry.authors))
def printHTML(collection, genbody=True, standalone=False, minyear=0):
years = set([entry.year for entry in collection.itervalues()])
for year in sorted(years, reverse=True):
if year <= minyear:
header = html_year_header.substitute(year="Pre-%d" % minyear)
entries = filter(lambda entry: entry.year <= year, collection.itervalues())
else:
header = html_year_header.substitute(year=minyear)
entries = filter(lambda entry: entry.year == year, collection.itervalues())
for entry in sorted(entries, key=lambda entry: (entry.year, entry.id), reverse=True):
if entry.paperurl:
record = html_record_url.substitute(url=entry.paperurl, title=entry.title,
authors=_formatAuthors(entry.authors))
else:
record = html_record_plain.substitute(title=entry.title,
authors=_formatAuthors(entry.authors))
def printHTML(collection, genbody=True, standalone=False):
years = set([entry.year for entry in collection.itervalues()])
if genbody:
print textwrap.dedent("""\
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Infoscience</title>
</head>
<body>""")
print """<div class="iscollection" id="paperlist">"""
for year in sorted(years, reverse=True):
print """<h4 class="isyear">%s</h4>""" % year
entries = filter(lambda entry: entry.year == year, collection.itervalues())
for entry in sorted(entries, key=lambda entry: entry.id, reverse=True):
print textwrap.dedent("""\
<div class="isrecord">
<span class="istitle">%s</span>.
<span class="isauthors">%s</span>
</div>""" % ("""<a href="%s">%s</a>""" % (entry.paperurl, entry.title)
if entry.paperurl else entry.title,
_formatAuthors(entry.authors)))
print """</div>"""
if genbody:
print textwrap.dedent("""\
</body>
</html>""")
def main():
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
parser = ArgumentParser(description="InfoScience publication parser.")
parser.add_argument("-f", "--file", help="Parse from external file.")
parser.add_argument("--bare", action="store_true", default=False,
help="Output bare HTML (without body).")
parser.add_argument("--stand-alone", action="store_true", default=False,
help="Output stand-alone HTML document.")
args = parser.parse_args()
collection = { }
parser = InfoscienceParser(collection)
if args.file:
with open(args.file, "r") as f:
data = f.read()
parser.parseXML(data)
else:
offset = 1
while True:
url = "%s?cc=%s&of=%s&jrec=%d" % (base_infoscience_url, search_cc,
search_of, offset)
logging.info("Crawling %s" % url)
c = urllib.urlopen(url)
data = c.read()
total = parser.parseXML(data)
if not total:
break
offset += total
printHTML(collection, genbody=not args.bare, standalone=args.stand_alone)
if __name__ == "__main__":
sys.exit(main())