Skip to content

Commit

Permalink
code refactored
Browse files Browse the repository at this point in the history
Signed-off-by: Deepak Raj <[email protected]>
  • Loading branch information
codeperfectplus committed Apr 25, 2023
1 parent bed44ca commit 2369b93
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
28 changes: 7 additions & 21 deletions audiobook/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
write_json_file,
rtf_to_json
)
from audiobook.utils import get_json_metadata

logger = logging.getLogger("PyPDF2")
logger.setLevel(logging.INFO)
Expand Down Expand Up @@ -60,9 +61,8 @@ def get_library(self):
def create_json_book(self, input_book_path, password=None, load_from_library=False):
"""method to create json book from input file
it calls respective method based on file format"""
json_filename = (
os.path.basename(input_book_path).split(".")[0] + ".json"
)
json_filename = (os.path.basename(input_book_path).split(".")[0] + ".json")
output_file_path = os.path.join(BOOK_DIR, json_filename)

if load_from_library:
print("Loading book from library")
Expand All @@ -73,26 +73,12 @@ def create_json_book(self, input_book_path, password=None, load_from_library=Fal
metadata["pages"] = len(json_book)
return json_book, metadata

elif input_book_path.endswith(".odt"):
json_book, metadata = odt_to_json(input_book_path)
elif input_book_path.endswith(".pdf"):
json_book, metadata = pdf_to_json(input_book_path, password)
elif input_book_path.endswith(".txt"):
json_book, metadata = txt_to_json(input_book_path)
elif input_book_path.endswith(".epub"):
json_book, metadata = epub_to_json(input_book_path)
elif input_book_path.endswith(".mobi"):
json_book, metadata = mobi_to_json(input_book_path)
elif input_book_path.startswith(("http", "https")):
json_book, metadata = html_to_json(input_book_path)
elif input_book_path.endswith((".docx", ".doc")):
json_book, metadata = docs_to_json(input_book_path)
elif input_book_path.endswith(".rtf"):
json_book, metadata = rtf_to_json(input_book_path)
if json_book:
json_book, metadata = get_json_metadata(input_book_path=input_book_path, password=password)
else:
raise NotImplementedError("Only PDF, TXT, EPUB, MOBI, ODT, HTTP, DOCX and DOC files are supported")
raise NotImplementedError("Only PDF, TXT, EPUB, MOBI, ODT, HTTP, RTF, DOCX and DOC files are supported")

write_json_file(json_book, os.path.join(BOOK_DIR, json_filename))
write_json_file(json_book, output_file_path)

return json_book, metadata

Expand Down
27 changes: 27 additions & 0 deletions audiobook/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,30 @@ def html_to_json(url):
metadata["pages"] = len(json_book)
metadata["book_name"] = book_name
return json_book, metadata


def get_json_metadata(input_book_path, password):
""" helper function to call the function based on the file type """
# get the file extension
json_book = {}
metadata = {}
file_extension = input_book_path.split(".")[-1]

if file_extension == "odt":
json_book, metadata = odt_to_json(input_book_path)
elif file_extension == "pdf":
json_book, metadata = pdf_to_json(input_book_path, password)
elif file_extension == "txt":
json_book, metadata = txt_to_json(input_book_path)
elif file_extension == "epub":
json_book, metadata = epub_to_json(input_book_path)
elif file_extension == "mobi":
json_book, metadata = mobi_to_json(input_book_path)
elif input_book_path.startswith(("http", "https")):
json_book, metadata = html_to_json(input_book_path)
elif input_book_path.endswith((".docx", ".doc")):
json_book, metadata = docs_to_json(input_book_path)
elif file_extension == "rtf":
json_book, metadata = rtf_to_json(input_book_path)

return json_book, metadata

0 comments on commit 2369b93

Please sign in to comment.