File size: 2,163 Bytes
0166771
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3

import traceback
import time
import sys
import sickle
from lxml import etree

# monkey patch sickle.response to handle large XML responses
# by enabling the huge_tree option (aka XML_PARSE_HUGE)

sickle.response.XMLParser = etree.XMLParser(remove_blank_text=True,
                                            recover=True,
                                            resolve_entities=False,
                                            huge_tree=True)

headers = {
    'User-Agent': 'Annif-Finna-collect'
}

sickle = sickle.Sickle('https://api.finna.fi/OAI/Server', headers=headers)

if len(sys.argv) > 1:
    current_token = sys.argv[1]
else:
    current_token = None

while True:
    params = { 	'metadataPrefix': 'oai_vufind_json',
                'set': 'non_dedup',
                'timeout': 30 }

    if current_token is not None:
        params['resumptionToken'] = current_token

    try:
        print("Performing call with params {}".format(params), file=sys.stderr)
        records = sickle.ListRecords(**params)

        for idx, rec in enumerate(records):
            if records.resumption_token:
                token = records.resumption_token.token
                if token != current_token:
                    print("Resumption token: {} expires: {}".format(token, records.resumption_token.expiration_date), file=sys.stderr)
                    current_token = token
            else:
                print("No resumption token in response", file=sys.stderr)
                print(records.oai_response.raw, file=sys.stderr)
                current_token = None
            if 'metadata' in rec.metadata and len(rec.metadata['metadata']) > 0:
                json_data = rec.metadata['metadata'][0]
                print(json_data)
        
        if current_token is None:
            # all records received, no token - we have probably reached the end
            print("No further records, all done.", file=sys.stderr)
            break

    except Exception as e:
        print(traceback.format_exc(), file=sys.stderr)
        print("Waiting for 10 seconds before retrying...", file=sys.stderr)
        time.sleep(10)