-
Notifications
You must be signed in to change notification settings - Fork 1
/
lseloader.py
206 lines (179 loc) · 7.26 KB
/
lseloader.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
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python
"""Load LSE Press metadata into Thoth"""
import logging
import requests
from urllib.parse import urlparse
from bookloader import BookLoader
from onix3 import Onix3Record
class LSELoader(BookLoader):
"""LSE Press specific logic to ingest metadata from ONIX into Thoth"""
import_format = "ONIX3"
single_imprint = True
publisher_name = "LSE Press"
publisher_shortname = None
publisher_url = "https://press.lse.ac.uk/"
cache_institutions = False
def run(self):
"""Process ONIX and call Thoth to insert its data"""
for product in self.data.no_product_or_product:
record = Onix3Record(product)
work = self.get_work(record, self.imprint_id)
logging.info(work)
work_id = self.thoth.create_work(work)
logging.info('workId: %s' % work_id)
self.create_publications(record, work_id)
self.create_contributors(record, work_id)
self.create_languages(record, work_id)
self.create_subjects(record, work_id)
@staticmethod
def get_work(record, imprint_id):
"""Returns a dictionary with all attributes of a 'work'
record: current onix record
imprint_id: previously obtained ID of this work's imprint
"""
title = record.title()
doi = record.doi()
# resolve DOI to obtain landing page
landing_page = requests.get(doi).url
work = {
"workType": record.work_type(),
"workStatus": "ACTIVE",
"fullTitle": title["fullTitle"],
"title": title["title"],
"subtitle": title["subtitle"],
"reference": record.reference(),
"edition": 1,
"imprintId": imprint_id,
"doi": doi,
"publicationDate": record.publication_date(),
"place": record.publication_place(),
"pageCount": record.page_count(),
"pageBreakdown": None,
"imageCount": None,
"tableCount": None,
"audioCount": None,
"videoCount": None,
"license": record.license(),
"copyrightHolder": None,
"landingPage": landing_page,
"lccn": None,
"oclc": None,
"shortAbstract": None,
"longAbstract": record.long_abstract().replace("\r", ""),
"generalNote": None,
"bibliographyNote": None,
"toc": None,
"coverUrl": record.cover_url(),
"coverCaption": None,
"firstPage": None,
"lastPage": None,
"pageInterval": None,
}
return work
def create_publications(self, record, work_id):
"""Creates PDF publication and locations associated with the current work
record: current onix record
work_id: previously obtained ID of the current work
"""
def create_location(landing_page, full_text_url, location_platform, canonical):
location = {
"publicationId": publication_id,
"landingPage": landing_page,
"fullTextUrl": full_text_url,
"locationPlatform": location_platform,
"canonical": canonical,
}
self.thoth.create_location(location)
logging.info(location)
publication = {
"workId": work_id,
"publicationType": "PDF",
"isbn": record.isbn(),
"widthMm": None,
"widthIn": None,
"heightMm": None,
"heightIn": None,
"depthMm": None,
"depthIn": None,
"weightG": None,
"weightOz": None,
}
publication_id = self.thoth.create_publication(publication)
logging.info(publication)
# OAPEN location
oapen_full_text_url = record.oapen_url()
oapen_url_path = urlparse(oapen_full_text_url).path.split('/')
oapen_landing_page = f"https://library.oapen.org/handle/{oapen_url_path[2]}/{oapen_url_path[3]}"
create_location(oapen_landing_page, oapen_full_text_url, "OAPEN", "true")
# DOAB location
doab_cover = record.cover_url()
cover_url_path = urlparse(doab_cover).path.split('/')
doab_landing_page = f"https://directory.doabooks.org/handle/{cover_url_path[3]}/{cover_url_path[4]}"
create_location(doab_landing_page, None, "DOAB", "false")
def create_contributors(self, record, work_id):
"""Creates all contributions associated with the current work
record: current onix record
work_id: previously obtained ID of the current work
"""
for contributor in record.contributors():
name = contributor.choice[0].value
surname = contributor.choice[1].value
fullname = f"{name} {surname}"
contribution_type = self.contribution_types[contributor.contributor_role[0].value.value]
contribution_ordinal = int(contributor.sequence_number.value)
contributor = {
"firstName": name,
"lastName": surname,
"fullName": fullname,
"orcid": None,
"website": None
}
if fullname not in self.all_contributors:
contributor_id = self.thoth.create_contributor(contributor)
self.all_contributors[fullname] = contributor_id
else:
contributor_id = self.all_contributors[fullname]
contribution = {
"workId": work_id,
"contributorId": contributor_id,
"contributionType": contribution_type,
"mainContribution": "true",
"contributionOrdinal": contribution_ordinal,
"biography": None,
"firstName": name,
"lastName": surname,
"fullName": fullname,
}
logging.info(contribution)
self.thoth.create_contribution(contribution)
def create_languages(self, record, work_id):
"""Creates language associated with the current work
record: current onix record
work_id: previously obtained ID of the current work
"""
language = {
"workId": work_id,
"languageCode": record.language_code(),
"languageRelation": "ORIGINAL",
"mainLanguage": "true"
}
logging.info(language)
self.thoth.create_language(language)
def create_subjects(self, record, work_id):
"""Creates all subjects associated with the current work
record: current onix record
work_id: previously obtained ID of the current work
"""
def create_subject(subject_type, subject_code, subject_ordinal):
subject = {
"workId": work_id,
"subjectType": subject_type,
"subjectCode": subject_code,
"subjectOrdinal": subject_ordinal
}
self.thoth.create_subject(subject)
logging.info(subject)
for index, code in enumerate(record.bic_codes()):
create_subject("BIC", code, index + 1)
for index, code in enumerate(record.keywords()):
create_subject("KEYWORD", code, index + 1)