From 50f220afb43992ac6201b38fd0d53f03bcc14ab6 Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Sun, 9 Jan 2022 13:24:02 +0100 Subject: [PATCH] Make refresh significantly faster, and also centralize threading --- src/Channel/SVT/__init__.py | 12 +----------- src/Channel/YouTube/__init__.py | 28 +++++++++------------------- src/Channel/__init__.py | 19 ++++++++++++++++--- 3 files changed, 26 insertions(+), 33 deletions(-) diff --git a/src/Channel/SVT/__init__.py b/src/Channel/SVT/__init__.py index 081ccab..9e276ad 100644 --- a/src/Channel/SVT/__init__.py +++ b/src/Channel/SVT/__init__.py @@ -24,19 +24,9 @@ class SVT(Channel): name = chan_dict['name'] super().__init__(svt_id, 'SVT', default_rss_url, logo, name) - self.m_thr = threading.Thread(target=self.parse_feed, - args=[svt_id], - kwargs={}) self.refresh() - def refresh(self) -> None: - self.m_thr.start() - - def wait(self) -> bool: - return self.m_thr.is_alive() - - def parse_feed(self, *args, **kwargs) -> None: - svt_id = args[0] + def parse_feed(self) -> None: self.m_items: list[Item] = list() resolved_link = str() description = str() diff --git a/src/Channel/YouTube/__init__.py b/src/Channel/YouTube/__init__.py index 2f2a9db..12ac60b 100644 --- a/src/Channel/YouTube/__init__.py +++ b/src/Channel/YouTube/__init__.py @@ -2,54 +2,44 @@ import threading import feedparser import wx -from youtube_dl import YoutubeDL as yt -from youtube_dl.utils import DownloadError, ExtractorError +import time +import youtube_dl from Channel import Channel from Items import Item -from Utils import (add_video, get_default_logo, hash_string, +from Utils import (add_video, get_default_logo, get_latest_video_timestamp, hash_string, make_bitmap_from_url, video_exists) class YouTube(Channel): def __init__(self, channel_id: str, name: str) -> None: - self.m_channel_id = channel_id self.m_name = name rss_url = 'https://www.youtube.com/feeds/videos.xml?channel_id={}'.format( channel_id) self.m_logo = get_default_logo('YouTube') + self.m_items: list[Item] = list() super().__init__(channel_id, 'YouTube', rss_url, self.m_logo, name) - self.m_thr = threading.Thread(target=self.parse_feed, - args=(), - kwargs={}) - - def refresh(self) -> None: - self.m_thr.start() - def wait(self) -> bool: - return self.m_thr.is_alive() def parse_feed(self) -> None: feed = feedparser.parse(self.get_feed()) - ydl_opts = { 'format': 'worstvideo[ext=mp4]+worstaudio[ext=m4a]/worstvideo+worstaudio', 'container': 'webm_dash', } entries = feed['entries'] - self.m_items: list[Item] = list() for entry in entries: video_id = hash_string(entry['id']) - if video_exists(video_id, self.m_channel_id): - pass + if video_exists(video_id, self.m_id): + continue title = str(entry['title']) thumbnail_link = str(entry['media_thumbnail'][0]['url']) description = str(entry['description']) link = '' - with yt(ydl_opts) as ydl: + with youtube_dl.YoutubeDL(ydl_opts) as ydl: try: video = ydl.extract_info(entry['link'], download=False) @@ -58,7 +48,7 @@ class YouTube(Channel): if form['height'] < 480 and form[ 'acodec'] != 'none': link = form['url'] - except ExtractorError or DownloadError: + except youtube_dl.utils.ExtractorError and youtube_dl.utils.DownloadError: pass resolved_link = link @@ -72,6 +62,6 @@ class YouTube(Channel): item = Item(description, resolved_link, self.m_provider_name, published_parsed, thumbnail, title) self.m_items.append(item) - add_video(video_id, self.m_channel_id, self.m_provider_name, + add_video(video_id, self.m_id, self.m_provider_name, description, resolved_link, published_parsed, thumbnail, title, 0) diff --git a/src/Channel/__init__.py b/src/Channel/__init__.py index 0988394..15b061d 100644 --- a/src/Channel/__init__.py +++ b/src/Channel/__init__.py @@ -3,6 +3,7 @@ from typing import Union import wx +import threading from Items import Item from Utils import get_videos @@ -15,6 +16,7 @@ class Channel: feed: str, logo: wx.Bitmap, name: str) -> None: + self.m_id = channel_id self.m_logo = logo self.m_provider_name = provider_name self.m_name = name @@ -40,8 +42,21 @@ class Channel: def parse_feed(self) -> Union[list[Item], None]: pass + def refresh(self) -> None: + if self.wait(): + return + self.m_thr = threading.Thread(target=self.parse_feed, + args=(), + kwargs={}) + self.m_thr.start() + def wait(self) -> bool: - return False + result = False + try: + result = self.m_thr.is_alive() + except AttributeError: + pass + return result def get_latest_item(self) -> Item: return self.m_items[0] # type: ignore @@ -52,5 +67,3 @@ class Channel: def set_logo(self, logo: wx.Bitmap) -> None: self.m_logo = logo - def refresh(self) -> None: - pass