Skip to content

Commit cc782f6

Browse files
committed
Merge branch 'release/0.0.6'
2 parents 93d18f5 + a351b6c commit cc782f6

File tree

3 files changed

+57
-25
lines changed

3 files changed

+57
-25
lines changed

src/db.py

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,54 @@
66
from typing import Dict, List
77

88

9-
class Crossref():
10-
"""Crossref compatibilty layer"""
11-
def __init__(self, doi: str):
12-
self.works = Works()
9+
class Db():
10+
"""Base Db class to derive specialised database classes from"""
11+
def __init__(self, doi: str) -> None:
12+
self.db = self.init_db()
1313
self.doi = urljoin('https://doi.org/', doi)
1414

15+
def init_db(self):
16+
"""Init database object"""
17+
raise NotImplementedError
18+
19+
def get_book(self):
20+
"""Return book data"""
21+
raise NotImplementedError
22+
23+
def get_chapters(self):
24+
"""Return chapters data"""
25+
raise NotImplementedError
26+
27+
28+
class Crossref(Db):
29+
"""Crossref compatibility layer"""
30+
def init_db(self):
31+
"""Init database object"""
32+
return Works()
33+
1534
def get_book(self) -> Dict:
16-
"""Return the book data associated to the supplied ISBN"""
17-
query = self.works.doi(self.doi)
35+
"""Return book data"""
36+
query = self.db.doi(self.doi)
37+
38+
if not query:
39+
raise ValueError(f"No book data associated to the DOI {self.doi}"
40+
"found on the database Crossref")
41+
1842
data = {"title": query.get("title")[0],
1943
"doi": query.get("DOI")}
2044
return data
2145

2246
def get_chapters(self, book: Dict) -> List:
23-
"""Returns a chapter data related to the book"""
24-
query = self.works.filter(container_title=book.get("title"),
25-
type='book-chapter') \
26-
.select('DOI', 'license', 'author',
27-
'title', 'type', 'page',
28-
'publisher', 'abstract')
29-
30-
# Assert that at least one DOI have been discovered
47+
"""Return chapters data"""
48+
query = self.db.filter(container_title=book.get("title"),
49+
type='book-chapter') \
50+
.select('DOI', 'license', 'author',
51+
'title', 'type', 'page',
52+
'publisher', 'abstract')
53+
3154
if not query:
32-
raise AssertionError('Couldn\'t find any chapter-level DOIs'
33-
+ ' for the supplied --isbn value')
55+
raise ValueError("No chapter data associated to the DOI"
56+
f"{self.doi} found on the database Crossref")
3457

3558
chapters = []
3659
for chapter in query:
@@ -57,21 +80,23 @@ def join_author_names(self, chapter_data: Dict) -> str:
5780
return '; '.join(author_list)
5881

5982

60-
class Thoth():
61-
"""Thoth compatibilty layer"""
62-
def __init__(self, doi: str):
63-
self.thoth = ThothClient()
64-
self.doi_url = urljoin('https://doi.org/', doi)
83+
class Thoth(Db):
84+
"""Thoth compatibility layer"""
85+
def init_db(self):
86+
"""Init database object"""
87+
return ThothClient()
6588

6689
def get_book(self) -> Dict:
67-
work = self.thoth.work_by_doi(doi=self.doi_url, raw=True)
90+
"""Return book data"""
91+
work = self.db.work_by_doi(doi=self.doi, raw=True)
6892
work_dict = json.loads(work)['data']['workByDoi']
6993

7094
data = {"title": work_dict.get("fullTitle"),
71-
"doi": self.doi_url}
95+
"doi": self.doi}
7296
return data
7397

7498
def get_chapters(self, book: Dict) -> List:
99+
"""Return chapters data"""
75100
# TODO replace this with a Thoth library method when available
76101
url = 'https://api.thoth.pub/graphql'
77102
query = {"query": """{ workByDoi (doi: "%s") {

src/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pdf import Pdf
88
from metadata import Metadata
99
from shutil import copy2
10+
import re
1011

1112
app = typer.Typer()
1213

@@ -28,7 +29,7 @@ def run(input_file: Path = typer.Option("./file.pdf",
2829

2930
# Iterate over chapters metadata
3031
for chapter in metadata.get_chapters():
31-
page_range = chapter.get("pages").split('-')
32+
page_range = re.split('-|–', chapter.get("pages"))
3233
output_file_name = chapter.get("doi").split('/')[-1] + '.pdf'
3334

3435
# Merge PDFs

src/metadata.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@ class Metadata:
5252
def __init__(self, database="thoth", doi=None):
5353
if database == "thoth":
5454
self.db = Thoth(doi)
55-
if database == "crossref":
55+
elif database == "crossref":
5656
self.db = Crossref(doi)
57+
else:
58+
raise ValueError(f"Database '{database}' misspelled or not "
59+
"implemented yet.")
5760

5861
self.book = self.fetch_book_data()
5962
self.chapters = self.fetch_chapter_data()
@@ -69,6 +72,9 @@ def fetch_chapter_data(self) -> List[Chapter]:
6972

7073
def get_chapters(self) -> List[Dict]:
7174
"""Return a list of Chapters (dictionaries)"""
75+
if not self.chapters:
76+
raise ValueError("No chapter data retrieved from the database.")
77+
7278
return [chapter.to_dict() for chapter in self.chapters]
7379

7480
@staticmethod

0 commit comments

Comments
 (0)