-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwrite_locations.py
More file actions
executable file
·52 lines (45 loc) · 1.82 KB
/
write_locations.py
File metadata and controls
executable file
·52 lines (45 loc) · 1.82 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
#!/usr/bin/env python3
"""
Write one or more sets of publication location information to Thoth.
Input: path to file containing location information, one location per line,
containing publication ID, location platform, landing page and full text URL,
separated by spaces.
Requires: Thoth personal access token as THOTH_PAT env var.
"""
# Third-party package already included in thoth-dissemination/requirements.txt
from thothlibrary import ThothError
from os import environ
import sys
from thothapi import get_thoth_client
def write_thoth_location(publication_id, location_platform, landing_page,
full_text_url):
thoth = get_thoth_client()
try:
token = environ['THOTH_PAT']
except KeyError as e:
raise KeyError('No Thoth token provided (THOTH_PAT environment variable not set)') from e
thoth.set_token(token)
location = {
'publicationId': publication_id,
'landingPage': landing_page,
'fullTextUrl': full_text_url,
'locationPlatform': location_platform,
'canonical': 'false'
}
try:
location_id = thoth.create_location(location)
except ThothError as e:
raise ValueError('Failed to create location in Thoth: token may be incorrect') from e
print(location_id)
if __name__ == '__main__':
locations_file = sys.argv[1]
with open(locations_file, 'r') as locations:
for location in locations:
parts = location.rstrip().split(' ')
try:
# Handle locations which only have a landing page, no full text URL
if parts[3] == "None":
parts[3] = None
write_thoth_location(parts[0], parts[1], parts[2], parts[3])
except IndexError:
raise ValueError('Not enough data in entry "{}"'.format(location.rstrip()))