Skip to content
Open
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
28 changes: 25 additions & 3 deletions usr/lib/hypnotix/hypnotix.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import shutil
import sys
import tempfile
import time
import traceback
import warnings
Expand Down Expand Up @@ -667,15 +668,36 @@ def download_channel_logos(self, logos_to_refresh):
continue
if os.path.isfile(channel.logo_path):
continue
tmp_name = None
try:
response = requests.get(channel.logo, headers=headers, timeout=10, stream=True)
if response.status_code == 200:
response.raw.decode_content = True
with open(channel.logo_path, "wb") as f:
shutil.copyfileobj(response.raw, f)
self.refresh_channel_logo(channel, image)
os.makedirs(os.path.dirname(channel.logo_path), exist_ok=True)
with tempfile.NamedTemporaryFile(dir=os.path.dirname(channel.logo_path),
prefix=".logo_",
suffix=".part",
delete=False) as tmp_file:
tmp_name = tmp_file.name
shutil.copyfileobj(response.raw, tmp_file)

if os.path.getsize(tmp_name) == 0:
raise ValueError("Downloaded logo is empty")

# Verify downloaded content is a valid decodable image before caching
GdkPixbuf.Pixbuf.new_from_file(tmp_name)

os.replace(tmp_name, channel.logo_path)
tmp_name = None
self.refresh_channel_logo(channel, image)
except Exception as e:
print(e)
finally:
if tmp_name and os.path.exists(tmp_name):
try:
os.remove(tmp_name)
except Exception:
pass

@idle_function
def refresh_channel_logo(self, channel, image):
Expand Down