Skip to content

Commit

Permalink
Merge pull request #12 from djdembeck/develop
Browse files Browse the repository at this point in the history
Audible covers
  • Loading branch information
djdembeck authored Jul 11, 2021
2 parents d24ab8d + 1817c6b commit 09970b7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name='m4b-merge',
version='0.3.4.2',
version='0.3.5',
url='https://github.com/djdembeck/m4b-merge',
description=(
"A tool to standardize audiobook files"
Expand Down
4 changes: 3 additions & 1 deletion src/m4b_merge/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@


def run_all(inputs):
logging.info(f"Working on: {inputs}")
print('-' * 50)
print(f"Working on: {inputs}")
print('-' * 50)
# Validate path, check if it's a directory or a file
# This will also run find_extension to determine relevant filetype
input_data = helpers.get_directory(inputs)
Expand Down
15 changes: 12 additions & 3 deletions src/m4b_merge/audible_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ def get_chapters(self):
chap_start = self.ms_to_timestamp(chapter['start_offset_ms'])
# Starting chapter title data
original_title = chapter['title']
stripped_title = original_title.rstrip('.')
# Check if chapter title is purely numbers
if original_title.rstrip('.').isnumeric():
if stripped_title.isnumeric() and len(stripped_title) < 3:
# Remove trailing period in some cases
strip_period = original_title.rstrip('.')
strip_period = stripped_title
# Convert to int to normalize numbers
int_title = int(strip_period)
# Convert back to string for file use
Expand Down Expand Up @@ -146,7 +147,8 @@ def parser(self):
"contributors,"
"product_desc,"
"product_extended_attrs,"
"product_attrs"),
"product_attrs,"
"media"),
"asins": self.asin
}
)
Expand Down Expand Up @@ -281,5 +283,12 @@ def parser(self):
metadata_dict['format_type'] = (
aud_json['product']['format_type'])

# Cover image
if 'product_images' in aud_json['product']:
metadata_dict['cover_image'] = (
aud_json['product']['product_images']['500']
.replace('_SL500_', '')
)

# return all data
return metadata_dict
27 changes: 26 additions & 1 deletion src/m4b_merge/m4b_helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import math
import os
import requests
import shutil
from pathlib import Path
from pathvalidate import sanitize_filename
Expand All @@ -17,6 +18,22 @@ def __init__(self, input_data, metadata, chapters=None):
self.metadata = metadata
self.chapters = chapters

def download_cover(self):
if 'cover_image' in self.metadata:
# Request to image URL
cover_request = requests.get(self.metadata['cover_image'])
# Verify image exists
if cover_request.status_code == 200:
# Path to write image to
self.cover_path = f"{self.input_path}_cover.jpg"
# Write image
with open(self.cover_path, 'wb') as f:
f.write(cover_request.content)
else:
logging.error("Couldn't download Audible cover")
else:
logging.warning("No cover image available from Audible")

def prepare_data(self):
# Metadata variables
# Only use subtitle in case of metadata, not file name
Expand Down Expand Up @@ -48,6 +65,9 @@ def prepare_data(self):
f"{sanitize_filename(path_title)}"
)
self.file_title = sanitize_filename(title)

# Download cover image
self.download_cover()
##

# Make necessary directories
Expand Down Expand Up @@ -78,6 +98,9 @@ def prepare_data(self):
if series:
self.metadata_args.append(f"--series=\"{series}\"")

if self.cover_path:
self.metadata_args.append(f"--cover=\"{self.cover_path}\"")

# args for merge process
self.processing_args = [
'--force',
Expand Down Expand Up @@ -255,7 +278,6 @@ def run_merge(self):
f"--output-file=\"{self.book_output}/{self.file_title}.m4b\"",
f"--audio-bitrate=\"{target_bitrate}\"",
f"--audio-samplerate=\"{target_samplerate}\"",
'--skip-cover'
]
# Add in main metadata and merge args
args.extend(self.metadata_args)
Expand Down Expand Up @@ -329,6 +351,9 @@ def fix_chapters(self):
os.system(m4b_chap_cmd)

def move_completed_input(self):
# Cleanup cover file
if self.cover_path:
os.remove(self.cover_path)
# Move obsolete input to processed folder
if Path(self.input_path.parent, 'done') == config.junk_dir:
logging.debug("Junk dir is direct parent")
Expand Down
2 changes: 2 additions & 0 deletions tests/test_audible.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,7 @@ def test_returned_data(self):
# Check language
if metadata['language'] != "english":
errors.append("Error with language")
if not metadata['cover_image']:
errors.append("No cover image found")
# Assert no errors come back
assert not errors, "Errors occured:\n{}".format("\n".join(errors))

0 comments on commit 09970b7

Please sign in to comment.