-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathacmdownload.py
253 lines (214 loc) · 7.26 KB
/
acmdownload.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
from html.parser import HTMLParser
import json
import requests
import sqlite3
FILE_NAME = 'docs.json'
DB_NAME = 'docs.db'
def load_docs_file():
try:
with open(FILE_NAME, 'r') as f:
return json.load(f)
except:
return {}
def save_docs_file(docs):
with open(FILE_NAME, 'w') as f:
json.dump(docs, f)
def copy_file_to_db():
docs = load_docs_file()
con = sqlite3.connect(DB_NAME)
cur = con.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS docs (uid TEXT NOT NULL PRIMARY KEY, doc TEXT NOT NULL)')
con.commit()
for uid in docs:
doc = docs[uid]
cur.execute('INSERT INTO docs (uid, doc) VALUES (?, ?)', (uid, json.dumps(doc, ensure_ascii = False)))
con.commit()
con.close()
def load_docs():
docs = {}
con = sqlite3.connect(DB_NAME)
cur = con.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS docs (uid TEXT NOT NULL PRIMARY KEY, doc TEXT NOT NULL)')
con.commit()
cur.execute('SELECT uid, doc FROM docs')
for row in cur.fetchall():
docs[row[0]] = json.loads(row[1])
con.close()
return docs
def save_doc(uid, doc):
con = sqlite3.connect(DB_NAME)
cur = con.cursor()
cur.execute('INSERT INTO docs (uid, doc) VALUES (?, ?)', (uid, json.dumps(doc, ensure_ascii = False)))
con.commit()
con.close()
class PageParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.in_title = False
self.in_references_item = False
self.refs = []
self.cbu = None
self.title = None
def handle_starttag(self, tag, attrs):
d = dict(attrs)
if tag == 'h1' and 'citation__title' in d['class']:
self.in_title = True
elif tag == 'a' and 'data-ajaxurl' in d:
cited_by_url = d['data-ajaxurl']
if cited_by_url.startswith('/action/ajaxShowCitedBy'):
self.cbu = cited_by_url
elif self.in_references_item:
if tag == 'a':
link = d['href']
if link.startswith('https://dl.acm.org/doi/'):
link = link[23:]
self.refs.append(link)
else:
if tag == 'li' and 'class' in d and 'references__item' in d['class']:
self.in_references_item = True
def handle_endtag(self, tag):
if self.in_title:
self.in_title = False
elif self.in_references_item:
if tag == 'li':
self.in_references_item = False
def handle_data(self, data):
if self.in_title:
self.title = data
class CitationParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.links = []
def handle_starttag(self, tag, attrs):
d = dict(attrs)
if tag == 'a':
link = d['href']
if link.startswith('https://doi.org/'):
link = link[16:]
self.links.append(link)
def download_doc(doi):
r = requests.get('https://dl.acm.org/doi/' + doi)
page_parser = PageParser()
page_parser.feed(r.text)
doc = {'references': page_parser.refs}
if page_parser.title:
doc['title'] = page_parser.title
if page_parser.cbu:
r = requests.get('https://dl.acm.org' + page_parser.cbu)
citation_parser = CitationParser()
citation_parser.feed(r.text)
doc['citedby'] = citation_parser.links
else:
doc['citedby'] = []
r = requests.post('https://dl.acm.org/action/exportCiteProcCitation', data={
'dois': doi,
'targetFile': 'custom-bibtex',
'format': 'bibTex'
})
j = json.loads(r.text)
kvs = list(j['items'][0].items())[0][1]
if 'title' not in doc and 'title' in kvs:
doc['title'] = kvs['title']
for k in ['original-date', 'issued']:
if k in kvs:
date = kvs[k]
ds = map(str, date['date-parts'][0])
doc['date'] = '/'.join(ds)
break
if 'author' in kvs:
l = []
for a in kvs['author']:
name = ''
if 'given' in a:
name += a['given']
if 'family' in a:
if name:
name += ' '
name += a['family']
l.append(name)
doc['authors'] = ', '.join(l)
return doc
def get_top_ranked(docs, missing):
r = {uid:0 for uid in missing}
c = {uid:0 for uid in missing}
for doc in docs.values():
for uid in doc['references']:
if uid in r:
r[uid] = r[uid] + 1
for uid in doc['citedby']:
if uid in c:
c[uid] = c[uid] + 1
queue = set()
queue.add(max(r.items(), key = lambda x: x[1])[0])
queue.add(max(c.items(), key = lambda x: x[1])[0])
return queue
def download(orig, num_docs):
all_docs = load_docs()
docs = {}
uids = set([orig])
queue = set([orig])
while len(docs) < num_docs:
if len(queue) == 0:
missing = uids - set(docs.keys())
if len(missing) == 0:
print('No documents are missing, stopping.')
exit()
queue |= get_top_ranked(docs, missing)
uid = queue.pop()
if uid in all_docs:
print('%s: Taking %s from store' % (len(docs) + 1, uid))
doc = all_docs[uid]
else:
print('%s: Downloading %s...' % (len(docs) + 1, uid))
doc = download_doc(uid)
all_docs[uid] = doc
save_doc(uid, doc)
#save_docs(all_docs)
docs[uid] = doc
doc_uids = set(doc['references']) | set(doc['citedby'])
uids |= doc_uids
if uid == orig:
queue |= doc_uids - set([orig]) # Possibly unnecessary, to guard against paper that references itself.
return docs
def mostreferenced(docs, orig):
rank = {uid:0 for uid in docs}
for doc in docs.values():
for uid in set(doc['references']):
if uid in rank:
rank[uid] = rank[uid] + 1
rank = sorted(rank.items(), key = lambda x: -x[1])
for i in range(min(100, len(docs))):
uid, cnt = rank[i]
doc = docs[uid]
print('%3d, %8s, %3d (%3d), %s. %s: %s' % (i + 1, uid, cnt, len(doc['citedby']), doc['date'] if 'date' in doc else '??/??/????', doc['authors'] if 'authors' in doc else '???', doc['title'] if 'title' in doc else '???'))
def info(doc):
for (key, value) in doc.items():
if key not in ['references', 'citedby']:
print('%s: %s' % (key, value))
print('references: %s' % len(doc['references']))
print('cited by: %s' % len(doc['citedby']))
def remove_uid_file(uid):
docs = load_docs_file()
if uid in docs:
del docs[uid]
save_docs_file(docs)
def remove_uid(uid):
con = sqlite3.connect(DB_NAME)
cur = con.cursor()
cur.execute('DELETE FROM docs WHERE uid=?', (uid,))
con.commit()
con.close()
def remove_missing_meta():
docs = load_docs()
uids = list(docs.keys())
for uid in uids:
doc = docs[uid]
if len(set(doc.keys()) - set(['references', 'citedby'])) == 0:
remove_uid(uid)
del docs[uid]
#save_docs(docs)
doi = '10.5555/2387880.2387905'
documents_to_download = 300
docs = download(doi, documents_to_download)
info(docs[doi])
mostreferenced(docs, doi)