-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.py
195 lines (151 loc) · 5.9 KB
/
consumer.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
# VTechWorks consumer by Erin & Coral
from __future__ import unicode_literals
import time
from datetime import date, timedelta, datetime
import requests
from lxml import etree
from dateutil.parser import *
from nameparser import HumanName
from scrapi.linter import lint
from scrapi.linter.document import RawDocument, NormalizedDocument
NAME = 'vtechworks'
OAI_DC_BASE = 'http://vtechworks.lib.vt.edu/oai/request?verb=ListRecords'
NAMESPACES = {'dc': 'http://purl.org/dc/elements/1.1/',
'oai_dc': 'http://www.openarchives.org/OAI/2.0/',
'ns0': 'http://www.openarchives.org/OAI/2.0/'}
DEFAULT = datetime(1970, 01, 01)
DEFAULT_ENCODING = 'utf-8'
record_encoding = None
def consume(days_back=5):
start_date = date.today() - timedelta(days_back)
url = '{}&metadataPrefix=oai_dc&from={}'.format(OAI_DC_BASE, str(start_date))
records = get_records(url)
xml_list = []
for record in records:
doc_id = record.xpath('ns0:header/ns0:identifier/node()', namespaces=NAMESPACES)[0]
record = etree.tostring(record, encoding=(record_encoding or DEFAULT_ENCODING))
xml_list.append(RawDocument({
'doc': record,
'source': NAME,
'docID': copy_to_unicode(doc_id),
'filetype': 'xml'
}))
return xml_list
def get_records(url):
data = requests.get(url)
record_encoding = data.encoding
doc = etree.XML(data.content)
records = doc.xpath('//ns0:record', namespaces=NAMESPACES)
token = doc.xpath('//ns0:resumptionToken/node()', namespaces=NAMESPACES)
if len(token) == 1:
time.sleep(0.5)
url ='{}&resumptionToken={}'.format(OAI_DC_BASE, token[0])
records += get_records(url)
return records
def copy_to_unicode(element):
encoding = record_encoding or DEFAULT_ENCODING
element = ''.join(element)
if isinstance(element, unicode):
return element
else:
return unicode(element, encoding=encoding)
def get_contributors(result):
dctype = (result.xpath('//dc:type/node()', namespaces=NAMESPACES) or [''])[0]
contributors = result.xpath('//dc:contributor/node()', namespaces=NAMESPACES)
creators = result.xpath('//dc:creator/node()', namespaces=NAMESPACES)
if 'thesis' not in dctype.lower() and 'dissertation' not in dctype.lower():
all_contributors = contributors + creators
else:
all_contributors = creators
contributor_list = []
for person in all_contributors:
name = HumanName(person)
contributor = {
'prefix': name.title,
'given': name.first,
'middle': name.middle,
'family': name.last,
'suffix': name.suffix,
'email': '',
'ORCID': '',
}
contributor_list.append(contributor)
return contributor_list
def get_tags(result):
tags = result.xpath('//dc:subject/node()', namespaces=NAMESPACES) or []
return [copy_to_unicode(tag.lower()) for tag in tags]
def get_ids(result, doc):
service_id = doc.get('docID')
identifiers = result.xpath('//dc:identifier/node()', namespaces=NAMESPACES)
url = ''
doi = ''
for item in identifiers:
if 'hdl.handle.net' in item:
url = item
if 'doi' in item or 'DOI' in item:
doi = item
doi = doi.replace('doi:', '')
doi = doi.replace('DOI:', '')
doi = doi.replace('http://dx.doi.org/', '').strip()
doi = doi.strip()
if url == '':
raise Exception('Warning: No url provided!')
return {
'serviceID': service_id,
'url': copy_to_unicode(url),
'doi': copy_to_unicode(doi)
}
def get_properties(result):
result_type = (result.xpath('//dc:type/node()', namespaces=NAMESPACES) or [''])[0]
rights = (result.xpath('//dc:rights/node()', namespaces=NAMESPACES) or [''])
rights = ' '.join(rights)
publisher = (result.xpath('//dc:publisher/node()', namespaces=NAMESPACES) or [''])[0]
relation = (result.xpath('//dc:relation/node()', namespaces=NAMESPACES) or [''])[0]
language = (result.xpath('//dc:language/node()', namespaces=NAMESPACES) or [''])[0]
properties = {
'type': copy_to_unicode(result_type),
'language': copy_to_unicode(language),
'relation': copy_to_unicode(relation),
'publisherInfo': {
'publisher': copy_to_unicode(publisher),
},
'permissions': {
'copyrightStatement': copy_to_unicode(rights),
},
}
return properties
def get_date_created(result):
dates = result.xpath('//dc:date/node()', namespaces=NAMESPACES)
date_list = []
for item in dates:
a_date = parse(str(item)[:10], yearfirst=True, default=DEFAULT).isoformat()
date_list.append(a_date)
min_date = min(date_list)
return copy_to_unicode(min_date)
def get_date_updated(result):
dateupdated = result.xpath('//ns0:header/ns0:datestamp/node()', namespaces=NAMESPACES)[0]
date_updated = parse(dateupdated).isoformat()
return copy_to_unicode(date_updated)
def normalize(raw_doc):
result = raw_doc.get('doc')
try:
result = etree.XML(result)
except etree.XMLSyntaxError:
print('Error in namespaces! Skipping this one...')
return None
title = copy_to_unicode((result.xpath('//dc:title/node()', namespaces=NAMESPACES) or [''])[0])
description = copy_to_unicode((result.xpath('//dc:description/node()', namespaces=NAMESPACES) or [''])[0])
payload = {
'title': title,
'contributors': get_contributors(result),
'properties': get_properties(result),
'description': description,
'tags': get_tags(result),
'id': get_ids(result, raw_doc),
'source': NAME,
'dateUpdated': get_date_updated(result),
'dateCreated': get_date_created(result),
}
return NormalizedDocument(payload)
if __name__ == '__main__':
print(lint(consume, normalize))