|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from os import getenv |
| 4 | +from thothlibrary import ThothClient |
| 5 | +import urllib.parse |
| 6 | + |
| 7 | + |
| 8 | +class Thoth: |
| 9 | + ''' |
| 10 | + Module to handle Thoth interactions |
| 11 | + ''' |
| 12 | + |
| 13 | + def __init__(self): |
| 14 | + self.client = ThothClient() |
| 15 | + self.logged_in = self.login() |
| 16 | + |
| 17 | + def login(self): |
| 18 | + username = getenv('THOTH_EMAIL') |
| 19 | + password = getenv('THOTH_PWD') |
| 20 | + if username is None: |
| 21 | + print('[WARNING] No Thoth username provided ' |
| 22 | + '(THOTH_EMAIL environment variable not set)') |
| 23 | + return False |
| 24 | + if password is None: |
| 25 | + print('[WARNING] No Thoth password provided ' |
| 26 | + '(THOTH_PWD environment variable not set)') |
| 27 | + return False |
| 28 | + try: |
| 29 | + self.client.login(username, password) |
| 30 | + return True |
| 31 | + except: |
| 32 | + return False |
| 33 | + |
| 34 | + def write_urls(self, metadata, book_doi): |
| 35 | + ''' |
| 36 | + Write chapter Landing Page and Full Text URLs |
| 37 | + to Thoth in standard OBP format |
| 38 | + ''' |
| 39 | + chapter_doi_full = metadata.get_chapter_doi() |
| 40 | + |
| 41 | + # Skip chapters that don't have a DOI |
| 42 | + if chapter_doi_full is not None: |
| 43 | + work_id = self.client.work_by_doi(chapter_doi_full).workId |
| 44 | + chapter_doi = chapter_doi_full.split('doi.org/')[-1].lower() |
| 45 | + landing_page_root = ( |
| 46 | + 'https://www.openbookpublishers.com/books/' |
| 47 | + '{book_doi}/chapters/{chapter_doi}') |
| 48 | + |
| 49 | + publication = {"workId": work_id, |
| 50 | + "publicationType": "HTML", |
| 51 | + "isbn": None, |
| 52 | + "widthMm": None, |
| 53 | + "widthIn": None, |
| 54 | + "heightMm": None, |
| 55 | + "heightIn": None, |
| 56 | + "depthMm": None, |
| 57 | + "depthIn": None, |
| 58 | + "weightG": None, |
| 59 | + "weightOz": None} |
| 60 | + publication_id = self.client.create_publication(publication) |
| 61 | + |
| 62 | + location = {"publicationId": publication_id, |
| 63 | + "landingPage": landing_page_root.format( |
| 64 | + book_doi=book_doi, chapter_doi=chapter_doi), |
| 65 | + "fullTextUrl": urllib.parse.unquote_plus( |
| 66 | + metadata.get_page_url()), |
| 67 | + "locationPlatform": "OTHER", |
| 68 | + "canonical": "true"} |
| 69 | + self.client.create_location(location) |
| 70 | + |
| 71 | + print('{}: URLs written to Thoth'.format( |
| 72 | + metadata.contents[metadata.index])) |
0 commit comments