-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathharvest.py
More file actions
58 lines (49 loc) · 2.48 KB
/
Copy pathharvest.py
File metadata and controls
58 lines (49 loc) · 2.48 KB
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
import datetime
import csv
from oaipmh.client import Client
from oaipmh.metadata import MetadataRegistry, oai_dc_reader
def main():
URL = 'http://oai.narcis.nl/oai'
# from_date = '2022-07-13T22:00:00Z'
# from_date = datetime.datetime.strptime(from_date, "%Y-%m-%dT%H:%M:%SZ")
until_date = datetime.datetime.now()
from_date = until_date - datetime.timedelta(hours=6)
registry = MetadataRegistry()
registry.registerReader('oai_dc', oai_dc_reader)
client = Client(URL, registry)
with open('harvest.csv', 'w', newline='') as f:
fieldnames = ['identifiers', 'date', 'source', 'rights', 'partof', 'creators', 'title']
writer = csv.DictWriter(f, fieldnames=fieldnames, delimiter=' ', quotechar='"')
writer.writeheader()
records = client.listRecords(metadataPrefix='oai_dc', from_=from_date, until=until_date, set='publication')
# to make it more robust, we would have to check if the list isn't empty
for num, record in enumerate(records):
print('%0.6d %s' % (num, record[0].identifier()))
# check if item has been deleted
# deleted items have 'header status=deleted' and no metadata
# if not deleted, get metadata: title, creator, date, type, source, identifier
# field 'ispartof' is not included in the module, so we hacked it in
if record[0].isDeleted():
print("item deleted")
elif record[1] is not None:
fields = record[1].getMap()
pubtype = fields['type']
# continue if pubtype is not present or if it's an article
if not pubtype or pubtype[0] == 'info:eu-repo/semantics/article':
identifiers = fields['identifier']
itemdate = fields['date']
source = fields['source']
rights = fields['rights']
partof = fields['ispartof']
creator = fields['creator']
title = fields['title']
print(identifiers)
print(" by ")
print(creator)
print(" is part of")
print(partof)
writer.writerow({'identifiers': identifiers, 'date': itemdate, 'source': source,
'rights': rights, 'partof': partof, 'creators': creator,
'title': title})
if __name__ == '__main__':
main()