-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from OpenBookPublishers/feature/write_urls
Write Landing Page and Full Text URLs to Thoth automatically on chapter creation
- Loading branch information
Showing
6 changed files
with
118 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,8 +20,9 @@ The entry point is `.src/main.py`, which can be called with the following argume | |
| `-u` | URL path of this book | | ||
| `-k` | Copyright file | | ||
| `-a` | Donation link | | ||
| `-m` | MathJax support | | ||
| `-p` | Privacy policy URL | | ||
| `-m` | MathJax support | | ||
| `-p` | Privacy policy URL | | ||
| `-w` | Write chapter URLs to Thoth (True/False) | | ||
|
||
|
||
## Thoth Wrapper (Optional) | ||
|
@@ -38,12 +39,15 @@ docker run --rm \ | |
-v /path/to/output:/ebook_automation/output \ | ||
-e MATHJAX=False \ | ||
-e PRIVACYPOLICY_URL=https://example.org \ | ||
-e [email protected] \ | ||
-e THOTH_PWD=password \ | ||
openbookpublishers/epublius \ | ||
thoth_wrapper.py /ebook_automation/epub_file.epub \ | ||
--doi 10.11647/obp.0275 | ||
``` | ||
|
||
The environment variable MATHJAX enables or disable MathJax support | ||
The environment variables THOTH_EMAIL and THOTH_PWD allow use of the `--write-urls` option by providing Thoth credentials | ||
|
||
## Contributing | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from os import getenv | ||
from thothlibrary import ThothClient | ||
import urllib.parse | ||
|
||
|
||
class Thoth: | ||
''' | ||
Module to handle Thoth interactions | ||
''' | ||
|
||
def __init__(self): | ||
self.client = ThothClient() | ||
self.logged_in = self.login() | ||
|
||
def login(self): | ||
username = getenv('THOTH_EMAIL') | ||
password = getenv('THOTH_PWD') | ||
if username is None: | ||
print('[WARNING] No Thoth username provided ' | ||
'(THOTH_EMAIL environment variable not set)') | ||
return False | ||
if password is None: | ||
print('[WARNING] No Thoth password provided ' | ||
'(THOTH_PWD environment variable not set)') | ||
return False | ||
try: | ||
self.client.login(username, password) | ||
return True | ||
except: | ||
return False | ||
|
||
def write_urls(self, metadata, book_doi): | ||
''' | ||
Write chapter Landing Page and Full Text URLs | ||
to Thoth in standard OBP format | ||
''' | ||
chapter_doi_full = metadata.get_chapter_doi() | ||
|
||
# Skip chapters that don't have a DOI | ||
if chapter_doi_full is not None: | ||
work_id = self.client.work_by_doi(chapter_doi_full).workId | ||
chapter_doi = chapter_doi_full.split('doi.org/')[-1].lower() | ||
landing_page_root = ( | ||
'https://www.openbookpublishers.com/books/' | ||
'{book_doi}/chapters/{chapter_doi}') | ||
|
||
publication = {"workId": work_id, | ||
"publicationType": "HTML", | ||
"isbn": None, | ||
"widthMm": None, | ||
"widthIn": None, | ||
"heightMm": None, | ||
"heightIn": None, | ||
"depthMm": None, | ||
"depthIn": None, | ||
"weightG": None, | ||
"weightOz": None} | ||
publication_id = self.client.create_publication(publication) | ||
|
||
location = {"publicationId": publication_id, | ||
"landingPage": landing_page_root.format( | ||
book_doi=book_doi, chapter_doi=chapter_doi), | ||
"fullTextUrl": urllib.parse.unquote_plus( | ||
metadata.get_page_url()), | ||
"locationPlatform": "OTHER", | ||
"canonical": "true"} | ||
self.client.create_location(location) | ||
|
||
print('{}: URLs written to Thoth'.format( | ||
metadata.contents[metadata.index])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters