forked from apavlo/pvldb-announce
/
pvldb-announce.py
executable file
·373 lines (316 loc) · 12.3 KB
/
pvldb-announce.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import re
import urllib
import logging
import pytz
import time
import argparse
import sqlite3
import twitter
from datetime import datetime
from datetime import tzinfo
from pprint import pprint
from feedgen.feed import FeedGenerator
from bs4 import BeautifulSoup
## ==============================================
## LOGGING
## ==============================================
LOG = logging.getLogger(__name__)
LOG_handler = logging.StreamHandler()
LOG_formatter = logging.Formatter(fmt='%(asctime)s [%(funcName)s:%(lineno)03d] %(levelname)-5s: %(message)s',
datefmt='%m-%d-%Y %H:%M:%S')
LOG_handler.setFormatter(LOG_formatter)
LOG.addHandler(LOG_handler)
LOG.setLevel(logging.INFO)
## ==============================================
## CONFIGURATION
## ==============================================
RSS_TITLE = 'PVLDB Paper Announcements'
RSS_AUTHOR = {'name':'Andy Pavlo','email':'pavlo@cs.cmu.edu'}
RSS_SUBTITLE = 'Generated by the Carnegie Mellon Database Group'
RSS_FILE = "pvldb-rss.xml"
RSS_URL = "http://db.cs.cmu.edu/files/" + RSS_FILE
BASE_URL = "http://www.vldb.org/pvldb/"
DB_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "pvldb.db")
TWITTER_SLEEP_TIME = 1200 # seconds
dateFormat = "%B %Y"
dateRe = re.compile("Volume ([\d]+), No\.[\s]?([\d]+), ([A-Z][a-z]+ [\d]{4})")
SKIP = set([ "vol%d.html" % x for x in xrange(1, 5) ])
VOLUME_LABELS = { }
## ==============================================
## getVolumeUrls
## ==============================================
def getVolumeUrls(url):
volumes = [ ]
r = urllib.urlopen(url).read()
soup = BeautifulSoup(r, "lxml")
regex = re.compile("vol[\d]+\.html")
for a in soup.find_all('a'):
m = regex.match(a["href"])
if m and not a["href"] in volumes:
if a["href"] in SKIP:
logging.warn("Skipping '%s'" % a["href"])
continue
volumes.append(a["href"])
## FOR
return [ BASE_URL + x for x in volumes ]
## DEF
## ==============================================
## getPapers
## ==============================================
def getPapers(vol_url):
LOG.debug("Retreiving papers for %s" % vol_url)
r = urllib.urlopen(vol_url).read()
soup = BeautifulSoup(r, "lxml")
papers = { }
for s in soup.find_all('h2'):
sectionDate = None
m = dateRe.match(s.text)
LOG.debug("Processing header '%s'" % s.text)
if m:
sectionDate = datetime.strptime(m.groups()[2], dateFormat)
volume = int(m.groups()[0])
number = int(m.groups()[1])
VOLUME_LABELS[sectionDate] = (volume, number)
assert not sectionDate is None
if not sectionDate in papers:
papers[sectionDate] = [ ]
ul_search = s.find_next('ul')
# VOL6 Fix
if vol_url.find("vol6.html") != -1:
ul_search = ul_search.find_next('ul')
for u in ul_search:
skip = False
try:
for p in u.parent.find_all('li'):
if p.text.find("Editors-in-Chief") != -1:
skip = True
if skip: continue
url = None
title = None
authors = None
link = p.find("a")
if link and not p.next_element is None:
if link.text.find("Front Matter") != -1:
continue
authors = p.next_element
try:
authors = authors.strip()[:-1]
except:
LOG.error("authors=" + str(type(authors)))
LOG.error("authors.text=" + str(authors.text))
raise
url = link["href"]
if not url.startswith("http://"):
url = BASE_URL + url
title = link.__dict__["contents"][0].replace("\n", " ").strip()
#print pprint(dir(link))
#print
#print
if url is None: continue
papers[sectionDate].append({
"authors": authors,
"title": title,
"volume": volume,
"number": number,
"link": url,
"published": sectionDate.replace(tzinfo=pytz.utc)
})
## FOR
break
except:
LOG.error("Unexpected error for section '" + s.text + "'")
LOG.error("link=" + str(link))
LOG.error("p=" + str(p))
#LOG.error("authors=" + str(authors))
raise
## FOR
## FOR
return (papers)
## DEF
## ==============================================
## writeRSS
## ==============================================
def writeRSS(papers, output):
fg = FeedGenerator()
fg.id(RSS_URL)
fg.title(RSS_TITLE)
fg.subtitle(RSS_SUBTITLE)
fg.author(RSS_AUTHOR)
fg.link( href='http://www.vldb.org/pvldb/', rel='alternate' )
fg.language('en')
for p in papers:
summary = "%(title)s\nAuthors: %(authors)s\nPVLDB Volume %(volume)d, Number %(number)d" % p
fe = fg.add_entry()
fe.author(name=p["authors"])
fe.title(p["title"])
fe.link(href=p["link"])
fe.id(p["link"])
fe.published(published=p["published"])
fe.description(description=summary, isSummary=True)
## FOR
atomfeed = fg.atom_str(pretty=True) # Get the ATOM feed as string
atom_file = os.path.join(output, 'pvldb-atom.xml')
fg.atom_file(atom_file) # Write the ATOM feed to a file
LOG.info("Created ATOM '%s'" % atom_file)
rssfeed = fg.rss_str(pretty=True) # Get the RSS feed as string
rss_file = os.path.join(output, RSS_FILE)
fg.rss_file(rss_file) # Write the RSS feed to a file
LOG.info("Created RSS '%s'" % rss_file)
## DEF
## ==============================================
## postTwitter
## ==============================================
def postTwitter(args, db, paper):
LOG.info("Posting paper '%s' to twitter!" % paper["title"])
api = twitter.Api(consumer_key=args["twitter_consumer_key"],
consumer_secret=args["twitter_consumer_secret"],
access_token_key=args["twitter_access_token"],
access_token_secret=args["twitter_access_secret"])
#paper["separator"] = u"→".encode('unicode-escape')
tweet = u"Vol:%(volume)d No:%(number)d → %(title)s" % paper
if len(tweet)+24 > 140:
remaining = 140 - (len(tweet)+24)
tweet = tweet[:remaining-3] + u"..."
tweet += " " + paper["link"]
LOG.debug("%s [Length=%d]" % (tweet, len(tweet)))
status = api.PostUpdate(tweet)
LOG.info("Posted tweet [status=%s]", str(status))
cur = db.cursor()
sql = "UPDATE papers SET twitter = 1 WHERE link = ?"
cur.execute(sql, (paper["link"], ))
db.commit()
## DEF
## ==============================================
## createDatabase
## ==============================================
def createDatabase():
db = sqlite3.connect(DB_PATH)
cur = db.cursor()
sql = """
CREATE TABLE papers (
link VARCHAR(255) PRIMARY KEY,
title TEXT NOT NULL,
authors TEXT NOT NULL,
volume INT NOT NULL,
number INT NOT NULL,
published DATE NOT NULL,
twitter INT NOT NULL DEFAULT 0
);"""
cur.execute(sql)
db.commit()
db.close()
## FOR
## ==============================================
## main
## ==============================================
if __name__ == '__main__':
aparser = argparse.ArgumentParser(description='PVLDB Announcements Script')
aparser.add_argument('dbpath', help='Database Path')
aparser.add_argument("--debug", action='store_true')
## Collection Parameters
agroup = aparser.add_argument_group('Collection Parameters')
agroup.add_argument('--collect', action='store_true', help='Collect results from PVLDB website')
## RSS Parameters
agroup = aparser.add_argument_group('RSS Parameters')
agroup.add_argument('--rss', action='store_true', help='Genereate RSS/Atom file')
agroup.add_argument('--rss-path', type=str, help='RSS output directory')
## Twitter Parameters
agroup = aparser.add_argument_group('RSS Parameters')
agroup.add_argument('--twitter', action='store_true', help='Post announcements on Twitter')
agroup.add_argument('--twitter-consumer-key', type=str, help='Twitter Consumer Key')
agroup.add_argument('--twitter-consumer-secret', type=str, help='Twitter Consumer Secret')
agroup.add_argument('--twitter-access-token', type=str, help='Twitter Access Token Key')
agroup.add_argument('--twitter-access-secret', type=str, help='Twitter Access Token Secret')
args = vars(aparser.parse_args())
## ----------------------------------------------
if args['debug']:
LOG.setLevel(logging.DEBUG)
# If they want to post to twitter, make sure they give us all the info
# that we need to do this
if args["twitter"]:
LOG.debug("Checking twitter input arguments")
for k in args.keys():
if k.startswith("twitter") and args[k] is None:
LOG.error("Missing '%s' input parameter for Twitter" % k)
sys.exit(1)
## FOR
## IF
## ----------------------------------------------
# Create the database if we don't have it
if not os.path.exists(DB_PATH):
createDatabase()
db = sqlite3.connect(DB_PATH)
cur = db.cursor()
# Get the volume URLs
if args["collect"]:
volumes = getVolumeUrls(BASE_URL)
papers = { }
for v in volumes:
try:
p = getPapers(v)
papers.update(p)
except:
LOG.error("Unexpected error for " + v)
raise
# Figure out what papers are new
for d in reversed(sorted(papers.keys())):
for p in papers[d]:
sql = "SELECT * FROM papers WHERE link = ?"
cur.execute(sql, (p["link"],))
row = cur.fetchone()
if row is None:
LOG.debug("Adding %s" % p["link"])
sql = """INSERT INTO papers (
link, title, authors, volume, number, published
) VALUES (
?, ?, ?, ?, ?, ?)"""
cur.execute(sql, (p["link"], p["title"], p["authors"], p["volume"], p["number"], p["published"],))
## FOR
## FOR
db.commit()
## IF
## Post new papers to Twitter
if args["twitter"]:
sql = "SELECT * FROM papers WHERE twitter = 0 ORDER BY link"
new_papers = [ ]
for row in cur.execute(sql):
paper = {
"link": row[0],
"title": row[1],
"authors": row[2],
"volume": row[3],
"number": row[4],
"published":row[5],
}
new_papers.append(paper)
## FOR
for paper in new_papers:
postTwitter(args, db, paper)
LOG.warn("Sleeping for %d seconds..." % TWITTER_SLEEP_TIME)
time.sleep(TWITTER_SLEEP_TIME)
## FOR
## IF
# Always create the RSS files from scratch
if args["rss"]:
assert args["rss_path"]
sql = "SELECT * FROM papers ORDER BY volume DESC, number DESC, link"
papers = [ ]
for row in cur.execute(sql):
paper = {
"link": row[0],
"title": row[1],
"authors": row[2],
"volume": row[3],
"number": row[4],
"published":row[5],
}
papers.append(paper)
## FOR
writeRSS(papers, args["rss_path"])
## IF
db.close()
## MAIN