-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobploader.py
341 lines (308 loc) · 13.4 KB
/
obploader.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
"""Load OBP metadata into Thoth"""
from bookloader import BookLoader
class OBPBookLoader(BookLoader):
"""OBP specific logic to ingest metadata from CSV into Thoth"""
single_imprint = True
publisher_name = "Open Book Publishers"
publisher_shortname = "OBP"
publisher_url = "https://www.openbookpublishers.com/"
def run(self):
"""Process CSV and call Thoth to insert its data"""
for row in self.data.index:
work = self.get_work(row, self.imprint_id)
work_id = self.thoth.create_work(work)
print("workId: {}".format(work_id))
self.create_publications(row, work_id, work["landingPage"])
self.create_languages(row, work_id)
self.create_subjects(row, work_id)
self.create_contributors(row, work_id)
self.create_series(row, self.imprint_id, work_id, work["workType"])
# pylint: disable=too-many-locals
def get_work(self, row, imprint_id):
"""Returns a dictionary with all attributes of a 'work'
row: current row number
imprint_id: previously obtained ID of this work's imprint
"""
title = self.sanitise_title(self.data.at[row, 'Title'],
self.data.at[row, 'Subtitle'])
doi = "https://doi.org/{}/{}".format(self.data.at[row, 'DOI prefix'],
self.data.at[row, 'DOI suffix'])
try:
publication_date = "-".join([
str(int(self.data.at[row, "publication year"])),
str(int(self.data.at[row, "publication month"])),
str(int(self.data.at[row, "publication day"]))])
except TypeError:
publication_date = None
copyright_text = ""
for index in range(1, 4):
holder = self.data.at[row, "Copyright holder {}".format(index)]
if holder:
if copyright_text != "":
copyright_text += "; "
copyright_text += str(holder)
oclc = str(int(self.data.at[row, "OCN (OCLC number)"])) \
if self.data.at[row, "OCN (OCLC number)"] else None
image_count = int(self.data.at[row, "no of illustrations"]) \
if self.data.at[row, "no of illustrations"] else None
table_count = int(self.data.at[row, "no of tables"]) \
if self.data.at[row, "no of tables"] else None
audio_count, video_count = self.sanitise_media(
self.data.at[row, "no of audio/video"])
width = int(self.data.at[row, "Width (mm)"]) \
if self.data.at[row, "Width (mm)"] else None
height = int(self.data.at[row, "Height (mm)"]) \
if self.data.at[row, "Height (mm)"] else None
page_count = int(self.data.at[row, "no of pages"]) \
if self.data.at[row, "no of pages"] else None
page_breakdown = self.data.at[row, "pages"] \
if self.data.at[row, "pages"] else None
edition = int(self.data.at[row, "edition number (integers only)"]) \
if self.data.at[row, "no of pages"] else 1
status = self.data.at[row, "Status"] \
if self.data.at[row, "Status"] else "Forthcoming"
license_url = \
self.data.at[row, "License URL (human-readable summary)"] \
if self.data.at[row, "License URL (human-readable summary)"] \
else None
short_abstract = \
self.data.at[row, "Short Blurb (less than 100 words)"] \
if self.data.at[row, "Short Blurb (less than 100 words)"] \
else None
long_abstract = self.data.at[row, "Plain Text Blurb"] \
if self.data.at[row, "Plain Text Blurb"] else None
toc = self.data.at[row, "Table of Content"] \
if self.data.at[row, "Table of Content"] else None
cover = self.data.at[row, "Cover URL"] \
if self.data.at[row, "Cover URL"] else None
landing = self.data.at[row, "Book-page URL"] \
if self.data.at[row, "Book-page URL"] else None
work = {
"workType": self.work_types[
self.data.at[row, "Publication type"]],
"workStatus": self.work_statuses[status],
"fullTitle": title["fullTitle"],
"title": title["title"],
"subtitle": title["subtitle"],
"reference": None,
"edition": edition,
"imprintId": imprint_id,
"doi": doi,
"publicationDate": publication_date,
"place": "Cambridge, UK",
"width": width,
"height": height,
"pageCount": page_count,
"pageBreakdown": page_breakdown,
"imageCount": image_count,
"tableCount": table_count,
"audioCount": audio_count,
"videoCount": video_count,
"license": license_url,
"copyrightHolder": copyright_text,
"landingPage": landing,
"lccn": None,
"oclc": oclc,
"shortAbstract": short_abstract,
"longAbstract": long_abstract,
"generalNote": None,
"toc": toc,
"coverUrl": cover,
"coverCaption": None,
}
return work
def create_publications(self, row, work_id, landing_page):
"""Creates all publications associated with the current work
row: current row number
work_id: previously obtained ID of the current work
landing_page: previously obtained landing page of the current work
"""
currencies = ["GBP", "USD", "EUR", "AUD", "CAD"]
for index in range(1, 6):
publication_type = self.data.at[
row, "Format {}".format(str(index))]
isbn = self.sanitise_isbn(self.data.at[
row, "ISBN {} with dashes".format(str(index))])
url = landing_page
if not publication_type or not isbn or len(isbn) != 17:
continue
publication_type = publication_type.strip().upper()
if publication_type == "PDF":
url = self.data.at[row, "Full-text URL - PDF"]
publication = {
"workId": work_id,
"publicationType": publication_type,
"isbn": isbn,
"publicationUrl": url
}
publication_id = self.thoth.create_publication(publication)
if publication_type == "PDF":
continue
for currency in currencies:
price = {
"publicationId": publication_id,
"currencyCode": currency,
"unitPrice": self.data.at[row, "{} price {}".format(
currency, publication_type.lower())]
}
if not price["unitPrice"]:
continue
self.thoth.create_price(price)
htmlreader = {
"workId": work_id,
"publicationType": "HTML",
"isbn": None,
"publicationUrl": self.data.at[row, "Full-text URL - HTML"]
}
if htmlreader["publicationUrl"]:
self.thoth.create_publication(htmlreader)
def create_languages(self, row, work_id):
"""Creates all languages associated with the current work
row: current row number
work_id: previously obtained ID of the current work
"""
language_code = self.data.at[row, "ONIX Language Code"]
original = self.data.at[row, "Original ONIX Language Code"]
if language_code:
lang = language_code.upper()
original = original.upper() if original else lang
if lang == original:
language = {
"workId": work_id,
"languageCode": lang,
"languageRelation": "ORIGINAL",
"mainLanguage": "true"
}
languages = [language]
else:
languages = [{
"workId": work_id,
"languageCode": lang,
"languageRelation": "TRANSLATED_INTO",
"mainLanguage": "true"
}, {
"workId": work_id,
"languageCode": original,
"languageRelation": "ORIGINAL",
"mainLanguage": "false"
}]
for language in languages:
self.thoth.create_language(language)
def create_subjects(self, row, work_id):
"""Creates all subjects associated with the current work
row: current row number
work_id: previously obtained ID of the current work
"""
for index in range(1, 6):
for stype in ["BIC", "BISAC"]:
code = self.data.at[row, "{} subject code {}".format(
stype, index)]
if not code or not code.strip():
continue
subject = {
"workId": work_id,
"subjectType": stype,
"subjectCode": code.strip(),
"subjectOrdinal": index
}
self.thoth.create_subject(subject)
custom = self.data.at[row, "Academic discipline (OBP)"]
if custom:
subject = {
"workId": work_id,
"subjectType": "CUSTOM",
"subjectCode": custom,
"subjectOrdinal": 1
}
self.thoth.create_subject(subject)
keywords_cell = self.data.at[row, "keywords"]
if keywords_cell:
keywords = keywords_cell.strip().replace(",", ";")
if keywords:
for index, keyword in enumerate(keywords.split(";")):
if not keyword:
continue
subject = {
"workId": work_id,
"subjectType": "KEYWORD",
"subjectCode": keyword.strip(),
"subjectOrdinal": index + 1
}
self.thoth.create_subject(subject)
def create_contributors(self, row, work_id):
"""Creates all contributions associated with the current work
row: current row number
work_id: previously obtained ID of the current work
"""
for index in range(1, 7):
name = self.data.at[
row, "Contributor {} first name".format(index)]
surname = self.data.at[
row, "Contributor {} surname".format(index)]
if not name or not surname:
continue
name = name.strip()
surname = surname.strip()
fullname = "{} {}".format(name, surname)
numeral = ".{}".format(index - 1) if index > 1 else ""
orcid = self.data.at[row, "ORCID ID{}".format(numeral)]
contributor = {
"firstName": name,
"lastName": surname,
"fullName": fullname,
"orcid": orcid,
"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]
numeral = ".{}".format(index - 1) if index > 1 else ""
contribution_type = self.contribution_types[
self.data.at[row, "OBP Role Name{}".format(numeral)]]
main = self.is_main_contribution(contribution_type)
contribution = {
"workId": work_id,
"contributorId": contributor_id,
"contributionType": contribution_type,
"mainContribution": main,
"biography": None,
"institution": None,
"firstName": name,
"lastName": surname,
"fullName": fullname
}
self.thoth.create_contribution(contribution)
def create_series(self, row, imprint_id, work_id, work_type):
"""Creates all series associated with the current work
row: current row number
work_id: previously obtained ID of the current work
work_type: previously obtained type of the current work
"""
series_name = self.data.at[row, "Series Name"]
issn_print = self.data.at[row, "ISSN Print with dashes"]
issn_digital = self.data.at[row, "ISSN Digital with dashes"]
issue_ordinal = self.data.at[row, "No. in the Series"]
series_type = "JOURNAL" if work_type == "JOURNAL_ISSUE" \
else "BOOK_SERIES"
if series_name and issn_print:
series = {
"seriesType": series_type,
"seriesName": series_name,
"issnDigital": issn_digital,
"issnPrint": issn_print,
"seriesUrl": None,
"imprintId": imprint_id
}
if series_name not in self.all_series:
series_id = self.thoth.create_series(series)
self.all_series[series_name] = series_id
else:
series_id = self.all_series[series_name]
issue = {
"seriesId": series_id,
"workId": work_id,
"issueOrdinal": int(issue_ordinal)
}
self.thoth.create_issue(issue)