Make refresh significantly faster, and also centralize threading

main
Micke Nordin 3 years ago
parent f6fe4f143b
commit 50f220afb4
Signed by: micke
GPG Key ID: 014B273D614BE877

@ -24,19 +24,9 @@ class SVT(Channel):
name = chan_dict['name'] name = chan_dict['name']
super().__init__(svt_id, 'SVT', default_rss_url, logo, 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() self.refresh()
def refresh(self) -> None: def parse_feed(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]
self.m_items: list[Item] = list() self.m_items: list[Item] = list()
resolved_link = str() resolved_link = str()
description = str() description = str()

@ -2,54 +2,44 @@ import threading
import feedparser import feedparser
import wx import wx
from youtube_dl import YoutubeDL as yt import time
from youtube_dl.utils import DownloadError, ExtractorError import youtube_dl
from Channel import Channel from Channel import Channel
from Items import Item 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) make_bitmap_from_url, video_exists)
class YouTube(Channel): class YouTube(Channel):
def __init__(self, channel_id: str, name: str) -> None: def __init__(self, channel_id: str, name: str) -> None:
self.m_channel_id = channel_id
self.m_name = name self.m_name = name
rss_url = 'https://www.youtube.com/feeds/videos.xml?channel_id={}'.format( rss_url = 'https://www.youtube.com/feeds/videos.xml?channel_id={}'.format(
channel_id) channel_id)
self.m_logo = get_default_logo('YouTube') self.m_logo = get_default_logo('YouTube')
self.m_items: list[Item] = list()
super().__init__(channel_id, 'YouTube', rss_url, self.m_logo, name) 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: def parse_feed(self) -> None:
feed = feedparser.parse(self.get_feed()) feed = feedparser.parse(self.get_feed())
ydl_opts = { ydl_opts = {
'format': 'format':
'worstvideo[ext=mp4]+worstaudio[ext=m4a]/worstvideo+worstaudio', 'worstvideo[ext=mp4]+worstaudio[ext=m4a]/worstvideo+worstaudio',
'container': 'webm_dash', 'container': 'webm_dash',
} }
entries = feed['entries'] entries = feed['entries']
self.m_items: list[Item] = list()
for entry in entries: for entry in entries:
video_id = hash_string(entry['id']) video_id = hash_string(entry['id'])
if video_exists(video_id, self.m_channel_id): if video_exists(video_id, self.m_id):
pass continue
title = str(entry['title']) title = str(entry['title'])
thumbnail_link = str(entry['media_thumbnail'][0]['url']) thumbnail_link = str(entry['media_thumbnail'][0]['url'])
description = str(entry['description']) description = str(entry['description'])
link = '' link = ''
with yt(ydl_opts) as ydl: with youtube_dl.YoutubeDL(ydl_opts) as ydl:
try: try:
video = ydl.extract_info(entry['link'], download=False) video = ydl.extract_info(entry['link'], download=False)
@ -58,7 +48,7 @@ class YouTube(Channel):
if form['height'] < 480 and form[ if form['height'] < 480 and form[
'acodec'] != 'none': 'acodec'] != 'none':
link = form['url'] link = form['url']
except ExtractorError or DownloadError: except youtube_dl.utils.ExtractorError and youtube_dl.utils.DownloadError:
pass pass
resolved_link = link resolved_link = link
@ -72,6 +62,6 @@ class YouTube(Channel):
item = Item(description, resolved_link, self.m_provider_name, item = Item(description, resolved_link, self.m_provider_name,
published_parsed, thumbnail, title) published_parsed, thumbnail, title)
self.m_items.append(item) 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, description, resolved_link, published_parsed, thumbnail,
title, 0) title, 0)

@ -3,6 +3,7 @@
from typing import Union from typing import Union
import wx import wx
import threading
from Items import Item from Items import Item
from Utils import get_videos from Utils import get_videos
@ -15,6 +16,7 @@ class Channel:
feed: str, feed: str,
logo: wx.Bitmap, logo: wx.Bitmap,
name: str) -> None: name: str) -> None:
self.m_id = channel_id
self.m_logo = logo self.m_logo = logo
self.m_provider_name = provider_name self.m_provider_name = provider_name
self.m_name = name self.m_name = name
@ -40,8 +42,21 @@ class Channel:
def parse_feed(self) -> Union[list[Item], None]: def parse_feed(self) -> Union[list[Item], None]:
pass 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: 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: def get_latest_item(self) -> Item:
return self.m_items[0] # type: ignore return self.m_items[0] # type: ignore
@ -52,5 +67,3 @@ class Channel:
def set_logo(self, logo: wx.Bitmap) -> None: def set_logo(self, logo: wx.Bitmap) -> None:
self.m_logo = logo self.m_logo = logo
def refresh(self) -> None:
pass

Loading…
Cancel
Save