-
Notifications
You must be signed in to change notification settings - Fork 1
/
harvest-arxiv.py
executable file
·148 lines (122 loc) · 5.5 KB
/
harvest-arxiv.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#------------------------------------------------------------------------------
# arXiv harvester - Fetches metadata from arXiv for article metadata.
#------------------------------------------------------------------------------
# Copyright (c) 2015 University of Helsinki
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#------------------------------------------------------------------------------
import urllib.request
import urllib.parse
import xml.etree.ElementTree as ET
import argparse
import time
import sys
import os
#------------------------------------------------------------------------------
# Harvest from given set, e.g. cs=computer science
# see http://export.arxiv.org/oai2?verb=ListSets
harvest_set = "cs"
# Harvest result format, see http://arxiv.org/help/oa/index
# and http://export.arxiv.org/oai2?verb=ListMetadataFormats
harvest_format = "oai_dc"
# Base API url
harvest_base_url = "http://export.arxiv.org/oai2"
# API request for getting records, see documentation at
# http://www.openarchives.org/OAI/2.0/openarchivesprotocol.htm#ListRecords
harvest_url = harvest_base_url + "?verb=ListRecords" \
"&set={0}&metadataPrefix={1}". \
format(harvest_set, harvest_format)
harvest_url_continue = harvest_base_url + "?verb=ListRecords&resumptionToken={0}"
sleep_time = 20
#------------------------------------------------------------------------------
def fetch(resumptionToken = "", part=0, from_date="", outdir=""):
# Set to starting url, or use the resumptionToken if that is set
get_url = harvest_url
# Set from date if given
if from_date:
get_url += "&from=" + from_date
# If a resumption token is given, we use that only
if resumptionToken:
get_url = harvest_url_continue.format(urllib.parse.quote(resumptionToken))
try:
# Request from API
print("GET", get_url)
response = urllib.request.urlopen(get_url)
data = response.read()
# Parse XML
root = ET.fromstring(data)
# Get resumptionToken element from the XML
namespaces = {'oai': 'http://www.openarchives.org/OAI/2.0/'}
rtNode = root.find('./oai:ListRecords/oai:resumptionToken', namespaces)
cursor = 0
resumptionToken = None
if rtNode is not None:
resumptionToken = rtNode.text
cursor = rtNode.attrib['cursor']
# Form filename
if outdir != '' and outdir[-1] != '/':
outdir += '/'
filename = outdir + "arxiv-{0}-{1}-cursor{2}.xml".format(harvest_set,
harvest_format,
cursor)
# Write to file
print("Writing to", filename)
with open(filename, 'wb') as fp:
fp.write(data)
if not resumptionToken:
print("No resumptionToken, stopping ...")
return 1
print("Sleeping {0} seconds ...".format(sleep_time))
time.sleep(sleep_time)
return fetch(resumptionToken, part + 1, "", outdir)
except urllib.error.HTTPError as error:
# If we are fetching too fast the server will give an HTTP 503
# then we just wait the specified time and try again
if error.code == 503:
h = error.headers
if 'retry-after' in h:
ra = int(h['retry-after'])
print("Got HTTP 503, Retry-after: {0}".format(ra))
print("Sleeping {0} seconds ...".format(ra))
time.sleep(ra)
return fetch(resumptionToken, part, from_date)
# Any other errors than 503 are fatal.
print("ERROR: HTTP returned status {0}!".format(rstat))
return 1
#------------------------------------------------------------------------------
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-r', '--resumptionToken', metavar='TOKEN')
parser.add_argument('-d', '--outdir', metavar='DIR',
help='place xml files in this directory')
parser.add_argument('-f', '--from', dest='from_date', metavar='DATE',
help='Fetch records updated since FROM datestamp, ' +
'e.g. "2014-12-01"')
args = parser.parse_args()
if args.resumptionToken:
fetch(args.resumptionToken, outdir=args.outdir)
elif args.from_date:
fetch("", 0, args.from_date, outdir=args.outdir)
else:
fetch(outdir=args.outdir)