diff --git a/tubearchivist/home/config.json b/tubearchivist/home/config.json index ce0867b..502ceda 100644 --- a/tubearchivist/home/config.json +++ b/tubearchivist/home/config.json @@ -22,6 +22,7 @@ "add_thumbnail": false }, "application": { + "app_root": "/app", "cache_dir": "/cache", "videos": "/youtube", "file_template": "%(id)s_%(title)s.mp4", diff --git a/tubearchivist/home/src/thumbnails.py b/tubearchivist/home/src/thumbnails.py index e1b6fe6..e46130a 100644 --- a/tubearchivist/home/src/thumbnails.py +++ b/tubearchivist/home/src/thumbnails.py @@ -88,6 +88,34 @@ class ThumbManager: return missing_channels + def get_raw_img(self, img_url, thumb_type): + """get raw image from youtube and handle 404""" + app_root = self.CONFIG["application"]["app_root"] + default_map = { + "video": os.path.join( + app_root, "static/img/default-video-thumb.jpg" + ), + "icon": os.path.join( + app_root, "static/img/default-channel-icon.jpg" + ), + "banner": os.path.join( + app_root, "static/img/default-channel-banner.jpg" + ), + } + if img_url: + response = requests.get(img_url, stream=True) + else: + response = False + if not response or response.status_code == 404: + # use default + img_raw = Image.open(default_map[thumb_type]) + else: + # use response + img_obj = response.raw + img_raw = Image.open(img_obj) + + return img_raw + def download_vid(self, missing_thumbs): """download all missing thumbnails from list""" print(f"downloading {len(missing_thumbs)} thumbnails") @@ -99,16 +127,15 @@ class ThumbManager: thumb_path = os.path.join(self.CACHE_DIR, thumb_path_part) os.makedirs(folder_path, exist_ok=True) - img_raw = requests.get(thumb_url, stream=True).raw - img = Image.open(img_raw) + img_raw = self.get_raw_img(thumb_url, "video") - width, height = img.size + width, height = img_raw.size if not width / height == 16 / 9: new_height = width / 16 * 9 offset = (height - new_height) / 2 - img = img.crop((0, offset, width, height - offset)) + img_raw = img_raw.crop((0, offset, width, height - offset)) - img.convert("RGB").save(thumb_path) + img_raw.convert("RGB").save(thumb_path) mess_dict = { "status": "pending", @@ -122,23 +149,19 @@ class ThumbManager: """download needed artwork for channels""" print(f"downloading {len(missing_channels)} channel artwork") for channel in missing_channels: - print(channel) channel_id, channel_thumb, channel_banner = channel thumb_path = os.path.join( self.CHANNEL_DIR, channel_id + "_thumb.jpg" ) - img_raw = requests.get(channel_thumb, stream=True).content - with open(thumb_path, "wb") as f: - f.write(img_raw) + img_raw = self.get_raw_img(channel_thumb, "icon") + img_raw.convert("RGB").save(thumb_path) - if channel_banner: - banner_path = os.path.join( - self.CHANNEL_DIR, channel_id + "_banner.jpg" - ) - img_raw = requests.get(channel_banner, stream=True).content - with open(banner_path, "wb") as f: - f.write(img_raw) + banner_path = os.path.join( + self.CHANNEL_DIR, channel_id + "_banner.jpg" + ) + img_raw = self.get_raw_img(channel_banner, "banner") + img_raw.convert("RGB").save(banner_path) mess_dict = { "status": "pending", diff --git a/tubearchivist/home/templates/home/channel.html b/tubearchivist/home/templates/home/channel.html index 1b5f32d..4e1102b 100644 --- a/tubearchivist/home/templates/home/channel.html +++ b/tubearchivist/home/templates/home/channel.html @@ -54,13 +54,11 @@ {% if channels %} {% for channel in channels %}
- {% if channel.source.channel_banner_url %} -
- - {{ channel.source.channel_id }}-banner - -
- {% endif %} +
+ + {{ channel.source.channel_id }}-banner + +
diff --git a/tubearchivist/home/templates/home/channel_id.html b/tubearchivist/home/templates/home/channel_id.html index a1052e0..c6f72bf 100644 --- a/tubearchivist/home/templates/home/channel_id.html +++ b/tubearchivist/home/templates/home/channel_id.html @@ -6,9 +6,7 @@

Channel: {{ channel_info.channel_name }}

- {% if channel_info.channel_banner_url %} - channel_banner - {% endif %} + channel_banner
diff --git a/tubearchivist/static/img/default-channel-banner.jpg b/tubearchivist/static/img/default-channel-banner.jpg new file mode 100644 index 0000000..578db6b Binary files /dev/null and b/tubearchivist/static/img/default-channel-banner.jpg differ diff --git a/tubearchivist/static/img/default-channel-icon.jpg b/tubearchivist/static/img/default-channel-icon.jpg new file mode 100644 index 0000000..33577a2 Binary files /dev/null and b/tubearchivist/static/img/default-channel-icon.jpg differ diff --git a/tubearchivist/static/img/default-video-thumb.jpg b/tubearchivist/static/img/default-video-thumb.jpg new file mode 100644 index 0000000..2325bc9 Binary files /dev/null and b/tubearchivist/static/img/default-video-thumb.jpg differ