-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrss_fetcher.py
72 lines (63 loc) · 2.34 KB
/
rss_fetcher.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
from bs4 import BeautifulSoup
import urllib.request
import json
import sys
from configparser import ConfigParser
''' This script checks the IACR eprint RSS (actually the Atom feed, because this is easier to parse)
feed to check for new papers and stores new papers as an unlabelled record.
The script has functionality to scrape the year, serial number, title, authors list, abstract and
keywords from the feed, which will then be stored as the unlabelled record for the paper. '''
def scrape_eprint_record(e):
eprintId = e.id.string.replace('https://eprint.iacr.org/','')
year = int(eprintId[:4])
serial = int(eprintId[5:])
title = e.title.string
authors = [a.findAll('name')[0].string for a in e.findAll('author')]
abstract = e.content.string
keywords = [c['term'] for c in e.findAll('category')]
record = {
'year': year,
'serial': serial,
'title': title,
'authors': authors,
'abstract': abstract,
'kw': keywords
}
return record
def is_newer_than(last_record, record):
newer = record['year'] > last_record['year']
newer = newer or (record['year'] == last_record['year']
and record['serial'] > last_record['serial'])
return newer
## Read path to unlabelled papers from config
config = ConfigParser()
config.read('config.cfg')
unlabelled_path = config.get('Data', 'unlabelled')
## Fetch rss (atom) feed
with urllib.request.urlopen('https://eprint.iacr.org/rss/atom.xml') as url:
rss = url.read()
## Read all entries
soup = BeautifulSoup(rss, 'xml')
entries = soup.findAll('entry')
## Read in unlabelled papers
records = []
with open(unlabelled_path, 'r') as json_file:
records = json.load(json_file)
## Find any papers appearing in the rss feed not already stored as a unlabelled paper
new_records = []
for e in entries:
r = scrape_eprint_record(e)
if len(records) > 0 :
last_record = records[-1]
if (is_newer_than(last_record, r)):
new_records.append(r)
else:
new_records.append(r)
## Print the amout of new papers
print(str(len(new_records)))
## If there are new papers add these to the file of unlabelled papers
if len(new_records) > 0:
records = records + new_records
records = sorted(records, key=lambda k: k['year'] * 10000 + k['serial'])
with open(unlabelled_path, 'w') as json_file:
json.dump(records, json_file, separators=(',', ':'), indent=0, sort_keys=True)