Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing url to png or jpg conversion bugs #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/tts_tools/libtts.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,19 @@ def get_fs_path(path, url):
elif is_image(path, url):
# TTS appears to perform some weird heuristics when determining
# the file suffix. ._.
png_filename = recoded_name + ".png"
png_fullPath = os.path.join(IMGPATH, png_filename)
jpg_filename = recoded_name + ".jpg"
jpg_fullPath = os.path.join(IMGPATH, jpg_filename)
if os.path.isfile(png_fullPath):
return png_fullPath
elif os.path.isfile(jpg_fullPath):
return jpg_fullPath

if url.find(".png") > 0:
file_suffix = ".png"
return png_fullPath
else:
file_suffix = ".jpg"
filename = recoded_name + file_suffix
return os.path.join(IMGPATH, filename)

return jpg_fullPath
else:
errstr = (
"Do not know how to generate path for "
Expand Down
17 changes: 17 additions & 0 deletions src/tts_tools/prefetch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,18 @@ def content_expected(mime):
done.add(url)
continue

if outfile_name.endswith('.png'):
jpg_path = os.path.splitext(outfile_name)[0] + '.jpg'
if os.path.isfile(jpg_path) and not refetch:
done.add(url)
continue

if outfile_name.endswith('.jpg'):
jpg_path = os.path.splitext(outfile_name)[0] + '.png'
if os.path.isfile(jpg_path) and not refetch:
done.add(url)
continue

print("{} ".format(url), end="", flush=True)

if dry_run:
Expand Down Expand Up @@ -187,6 +199,11 @@ def content_expected(mime):
print(size_msg, end="", flush=True)

content_type = response.getheader("Content-Type", "").strip()
if content_type == "image/jpeg" or content_type == "image/jpg":
outfile_name = os.path.splitext(outfile_name)[0] + '.jpg'
if content_type == "image/png":
outfile_name = os.path.splitext(outfile_name)[0] + '.png'

is_expected = not content_type or content_expected(content_type)
if not (is_expected or ignore_content_type):
print_err(
Expand Down