From 390df8abd875197c04bfee864deb046007b40ad8 Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Sun, 9 Jan 2022 17:26:07 +0100 Subject: [PATCH] Clear up what various indexes were --- src/Channel/SVT/__init__.py | 47 +++++++------- src/Channel/YouTube/__init__.py | 2 - src/Channel/__init__.py | 27 ++++---- src/ChannelProvider/__init__.py | 2 +- src/Utils/__init__.py | 6 +- src/main.py | 112 ++++++++++++++++---------------- 6 files changed, 100 insertions(+), 96 deletions(-) diff --git a/src/Channel/SVT/__init__.py b/src/Channel/SVT/__init__.py index ef5fc9a..f2cec77 100644 --- a/src/Channel/SVT/__init__.py +++ b/src/Channel/SVT/__init__.py @@ -18,16 +18,14 @@ default_rss_url = 'http://www.svtplay.se/rss.xml' class SVT(Channel): - def __init__(self, svt_id: str) -> None: - chan_dict = resolve_svt_channel(svt_id) + def __init__(self, channel_id: str) -> None: + chan_dict = resolve_svt_channel(channel_id) logo = chan_dict['thumbnail'] name = chan_dict['name'] - super().__init__(svt_id, 'SVT', default_rss_url, logo, name) - - self.refresh() + super().__init__(channel_id, 'SVT', default_rss_url, logo, name) def parse_feed(self) -> None: - self.m_items: list[Item] = list() + #self.m_items: list[Item] = list() resolved_link = str() description = str() title = str() @@ -36,13 +34,14 @@ class SVT(Channel): published_parsed: datetime = datetime.now() video_id = str() - if self.m_id == 'feed': + if self.m_id == 'feed' : feed = feedparser.parse(self.get_feed()) entries = feed['entries'] for entry in entries: video_id = hash_string(entry['id']) - if video_exists(video_id, self.m_id): - break + svt_id = str() + if video_exists(video_id, 'feed'): + continue for link in entry['links']: if str(link['type']).startswith('image/'): thumbnail_link = str(link['href']) @@ -56,24 +55,27 @@ class SVT(Channel): datart = element.get('data-rt') if datart == 'top-area-play-button': - self.m_id = href.split('=')[1].split('&')[0] + svt_id = href.split('=')[1].split('&')[0] - resolved_link = self.resolve_link() + resolved_link = self.resolve_link(svt_id) description = str(entry['description']) published_parsed = entry['published_parsed'] title = str(entry['title']) - if resolved_link and thumbnail_link: - thumbnail = make_bitmap_from_url( - thumbnail_link, wx.Size(self.m_screen_width, 150)) - item = Item(description, resolved_link, self.m_provider_name, - published_parsed, thumbnail, title) - self.m_items.append(item) - add_video(video_id, self.m_id, self.m_provider_name, description, - resolved_link, published_parsed, thumbnail, title, 0) + print('pre') + if not resolved_link: + continue + print('post') + thumbnail = make_bitmap_from_url( + thumbnail_link, wx.Size(self.m_screen_width, 150)) + item = Item(description, resolved_link, self.m_provider_name, + published_parsed, thumbnail, title) + self.m_items.append(item) + add_video(video_id, self.m_id, self.m_provider_name, description, + resolved_link, published_parsed, thumbnail, title, 0) else: chan_dict = resolve_svt_channel(self.m_id) - resolved_link = self.resolve_link() + resolved_link = self.resolve_link(self.m_id) video_id = hash_string(resolved_link) title = chan_dict['name'] description = "Live channel stream" @@ -85,9 +87,9 @@ class SVT(Channel): self.m_items.append(item) - def resolve_link(self) -> str: + def resolve_link(self,svt_id) -> str: api = json.loads( - requests.get('https://api.svt.se/video/{}'.format(self.m_id)).text) + requests.get('https://api.svt.se/video/{}'.format(svt_id)).text) resolved_link = '' try: @@ -97,3 +99,4 @@ class SVT(Channel): except KeyError: pass return resolved_link + diff --git a/src/Channel/YouTube/__init__.py b/src/Channel/YouTube/__init__.py index 12ac60b..1c77b03 100644 --- a/src/Channel/YouTube/__init__.py +++ b/src/Channel/YouTube/__init__.py @@ -20,8 +20,6 @@ class YouTube(Channel): self.m_items: list[Item] = list() super().__init__(channel_id, 'YouTube', rss_url, self.m_logo, name) - - def parse_feed(self) -> None: feed = feedparser.parse(self.get_feed()) ydl_opts = { diff --git a/src/Channel/__init__.py b/src/Channel/__init__.py index 15b061d..5af8b6f 100644 --- a/src/Channel/__init__.py +++ b/src/Channel/__init__.py @@ -22,7 +22,9 @@ class Channel: self.m_name = name self.m_feed = feed self.m_items: list[Item] = get_videos(channel_id) - self.m_screen_width = 720/2 + self.m_screen_width = (720/2) + if len(self.m_items) < 1: + self.refresh() def get_logo_as_bitmap(self) -> wx.Bitmap: return self.m_logo @@ -33,6 +35,12 @@ class Channel: def get_items(self) -> Union[list[Item], None]: return self.m_items + def get_id(self) -> str: + return self.m_id + + def get_latest_item(self) -> Item: + return self.m_items[0] # type: ignore + def get_name(self) -> str: return self.m_name @@ -40,7 +48,7 @@ class Channel: return self.m_provider_name def parse_feed(self) -> Union[list[Item], None]: - pass + return None def refresh(self) -> None: if self.wait(): @@ -50,6 +58,12 @@ class Channel: kwargs={}) self.m_thr.start() + def set_items(self, items: list[Item]) -> None: + self.m_items = items + + def set_logo(self, logo: wx.Bitmap) -> None: + self.m_logo = logo + def wait(self) -> bool: result = False try: @@ -58,12 +72,3 @@ class Channel: pass return result - def get_latest_item(self) -> Item: - return self.m_items[0] # type: ignore - - def set_items(self, items: list[Item]) -> None: - self.m_items = items - - def set_logo(self, logo: wx.Bitmap) -> None: - self.m_logo = logo - diff --git a/src/ChannelProvider/__init__.py b/src/ChannelProvider/__init__.py index 878b6b0..15fce37 100644 --- a/src/ChannelProvider/__init__.py +++ b/src/ChannelProvider/__init__.py @@ -12,7 +12,7 @@ MYPATH = path.dirname(path.abspath(__file__)) class ChannelProvider: - def __init__(self, providerid: str, channels=list()): + def __init__(self, providerid: str, channels:list[Channel]): self.m_id = providerid self.m_logo: wx.Bitmap = get_default_logo(providerid) self.m_channels: list[Channel] = channels diff --git a/src/Utils/__init__.py b/src/Utils/__init__.py index c5dd7a5..bd6fdfd 100644 --- a/src/Utils/__init__.py +++ b/src/Utils/__init__.py @@ -54,7 +54,6 @@ def add_video(video_id: str, watchtime: int, basepath: str = BASEPATH, filename: str = DB_FILE_NAME) -> None: - try: timestamp = published.timestamp() #type: ignore except AttributeError: @@ -66,12 +65,13 @@ def add_video(video_id: str, fullpath = path.join(basepath, filename) if not path.isdir(thumbpath): makedirs(thumbpath) - bitmap.SaveFile(thumbnail, wx.BITMAP_TYPE_PNG) + if not path.isfile(thumbnail): + bitmap.SaveFile(thumbnail, wx.BITMAP_TYPE_PNG) con = sqlite3.connect(fullpath) cur = con.cursor() create_query: str = '''CREATE TABLE IF NOT EXISTS {} (video_id TEXT PRIMARY KEY, channel_id TEXT, provider_id TEXT, - title TEXT, link text, description TEXT, thumbnail TEXT, + title TEXT, link TEXT, description TEXT, thumbnail TEXT, published DATETIME, watchtime NUMBER)'''.format(VIDEO_TABLE) cur.execute(create_query) con.commit() diff --git a/src/main.py b/src/main.py index 56d14aa..e1156b0 100644 --- a/src/main.py +++ b/src/main.py @@ -34,7 +34,6 @@ class Cast(wx.Frame): super().__init__(*args, **kw) self.m_selected_chromecast = None self.SetSizeHints(WIDTH, HEIGHT, maxW=WIDTH) - self.m_index = 0 self.m_chromecast_thr = threading.Thread(target=self.get_chromecasts, args=(), kwargs={}) @@ -46,8 +45,8 @@ class Cast(wx.Frame): self.m_panel.SetupScrolling(rate_y=SCROLL_RATE, scrollToTop=True) self.m_panel.SetSizer(self.m_sizer) self.m_providers: list[ChannelProvider] = self.get_providers() - self.m_selected_channel = None - self.m_selected_provider_index = None +# self.m_selected_channel = None +# self.m_selected_provider_index = None self.show_provider_list(None) def add_back_button(self, callback: Callable) -> None: @@ -131,8 +130,8 @@ class Cast(wx.Frame): for provider in self.m_providers: bitmap = provider.get_logo_as_bitmap() - callback = lambda event, index=provider_index: self.show_channel_list( - event, index) + callback = lambda event, pindex=provider_index: self.show_channel_list( + event, pindex) btn_sizer: wx.BoxSizer = make_sized_button(self.m_panel, bitmap, provider.get_name(), callback) @@ -162,8 +161,8 @@ class Cast(wx.Frame): for channel in self.m_selected_provider.get_channels(): bitmap = channel.get_logo_as_bitmap() - callback = lambda event, index=channel_index: self.show_video_list( - event, index) + callback = lambda event, cindex=channel_index: self.show_video_list( + event, cindex) btn_sizer: wx.BoxSizer = make_sized_button(self.m_panel, bitmap, channel.get_name(), callback) @@ -175,47 +174,48 @@ class Cast(wx.Frame): self.m_sizer.Fit(self) self.m_sizer.Layout() - def show_video_list(self, _, index=0) -> None: + def show_video_list(self, _,channel_index) -> None: + self.m_selected_channel = self.m_selected_provider.get_channel_by_index(channel_index) self.m_sizer.Clear(delete_windows=True) self.m_sizer = wx.BoxSizer(wx.VERTICAL) # self.m_sizer.AddSpacer(SPACER_HEIGHT * 4) - channel = self.m_selected_provider.get_channel_by_index(index) - def refresh_callback(event): - if self.m_selected_provider_index == 1 and index == 0: - with wx.BusyInfo("Please wait, working..."): - for chan in self.m_selected_provider.get_channels(): - chan.refresh() - wait = 1 - while wait > 0: - wait = 0 + back_callback = lambda event: self.show_channel_list( + event, self.m_selected_provider_index) + self.add_back_button(back_callback) + if self.m_selected_provider.get_name() == 'YouTube' or (self.m_selected_provider.get_name() == 'SVT' and self.m_selected_channel.get_id() == 'feed'): + def refresh_callback(event): + if self.m_selected_provider.get_name() == 'YouTube' and channel_index == 0: + with wx.BusyInfo("Please wait, working..."): for chan in self.m_selected_provider.get_channels(): - if chan.wait(): - wait += 1 - time.sleep(1) - wx.GetApp().Yield() - else: - channel.refresh() - self.show_video_list(event,index ) - - refreshbtn = wx.Button(self.m_panel, - -1, - label="Refresh", - size=(WIDTH, BTN_HEIGHT)) - refreshbtn.Bind(wx.EVT_BUTTON, refresh_callback) - self.m_sizer.Add(refreshbtn) - - if channel.wait(): + chan.refresh() + wait = 1 + while wait > 0: + wait = 0 + for chan in self.m_selected_provider.get_channels(): + if chan.wait(): + wait += 1 + time.sleep(1) + wx.GetApp().Yield() + else: + self.m_selected_channel.refresh() + self.show_video_list(event,channel_index) + + refreshbtn = wx.Button(self.m_panel, + -1, + label="Refresh", + size=(WIDTH, BTN_HEIGHT)) + refreshbtn.Bind(wx.EVT_BUTTON, refresh_callback) + self.m_sizer.Add(refreshbtn) + + if self.m_selected_channel.wait(): with wx.BusyInfo("Please wait, working..."): - while channel.wait(): + while self.m_selected_channel.wait(): time.sleep(1) wx.GetApp().Yield() - back_callback = lambda event: self.show_channel_list( - event, self.m_selected_provider_index) - self.add_back_button(back_callback) - - for item in channel.get_items(): # type: ignore + btnindex = 0 + for item in self.m_selected_channel.get_items(): # type: ignore inner_sizer = wx.BoxSizer(wx.VERTICAL) pane_sizer = wx.BoxSizer(wx.VERTICAL) title = wx.StaticText(self.m_panel, -1) @@ -224,13 +224,13 @@ class Cast(wx.Frame): bitmap = item["thumbnail"] btn = wx.BitmapButton(self.m_panel, - id=self.m_index, + id=btnindex, bitmap=bitmap, style=BM_BTN_STYLE) btn.Bind( wx.EVT_BUTTON, - lambda event, link=item["link"], provider_index=index: self. - show_player(event, link, provider_index), + lambda event, link=item["link"], cindex=channel_index: self. + show_player(event, link, cindex), ) inner_sizer.Add(title) inner_sizer.Add(btn) @@ -256,13 +256,13 @@ class Cast(wx.Frame): #inner_sizer.Add(description) self.m_sizer.Add(inner_sizer) self.m_sizer.AddSpacer(SPACER_HEIGHT) - self.m_index = self.m_index + 1 + btnindex = btnindex + 1 self.m_panel.SetupScrolling(rate_y=SCROLL_RATE, scrollToTop=True) self.m_panel.SetSizer(self.m_sizer) self.m_sizer.Fit(self) self.m_sizer.Layout() - def show_player(self, _, uri, provider_index: int): + def show_player(self, _, uri, channel_index: int): """ Show the video player @@ -286,8 +286,7 @@ class Cast(wx.Frame): back_button = wx.Button(self.m_panel, -1, "Back") back_button.Bind( wx.EVT_BUTTON, - lambda event, index=provider_index: self.show_video_list( - event, index), + lambda event, cindex=channel_index: self.show_video_list(event, cindex), ) inner_sizer.Add(self.m_control, (0, 0)) @@ -301,8 +300,7 @@ class Cast(wx.Frame): chromecast_button = wx.Button(self.m_panel, -1, "Cast") chromecast_button.Bind( wx.EVT_BUTTON, - lambda event, muri=uri, index=provider_index: self. - select_chromecast(event, muri, index), + lambda event, muri=uri, cindex=channel_index: self.select_chromecast(event, muri, cindex), ) inner_sizer.Add(chromecast_button, (2, 2)) self.m_sizer.Add(inner_sizer) @@ -319,7 +317,7 @@ class Cast(wx.Frame): play_button.Bind(wx.EVT_BUTTON, self.play) pause_button.Bind(wx.EVT_BUTTON, self.pause) - self.Bind(wx.media.EVT_MEDIA_FINISHED, self.show_video_list) + self.Bind(wx.media.EVT_MEDIA_FINISHED, lambda event: self.show_video_list(event,0)) self.load_uri(uri) self.m_panel.SetupScrolling(rate_y=SCROLL_RATE, scrollToTop=True) self.m_panel.SetSizer(self.m_sizer) @@ -327,7 +325,7 @@ class Cast(wx.Frame): self.m_sizer.Layout() self.m_panel.ScrollChildIntoView(self.m_control) - def select_chromecast(self, _, uri, provider_index): + def select_chromecast(self, _, uri, channel_index): self.m_sizer.Clear(delete_windows=True) self.m_sizer = wx.BoxSizer(wx.VERTICAL) # self.m_sizer.AddSpacer(SPACER_HEIGHT * 4) @@ -339,8 +337,8 @@ class Cast(wx.Frame): style=wx.BU_EXACTFIT) cancel_btn.Bind( wx.EVT_BUTTON, - lambda event, index=provider_index: self.show_video_list( - event, index), + lambda event, cindex=channel_index: self.show_video_list( + event, cindex), ) self.m_sizer.Add(cancel_btn) #, wx.ALIGN_CENTER_VERTICAL) @@ -352,8 +350,8 @@ class Cast(wx.Frame): size=(WIDTH, BTN_HEIGHT)) btn.Bind( wx.EVT_BUTTON, - lambda event, chromecast=cast, muri=uri, index=provider_index: - self.set_chromecast(event, chromecast, muri, index), + lambda event, chromecast=cast, muri=uri, cindex=channel_index: + self.set_chromecast(event, chromecast, muri, cindex), ) self.m_sizer.Add(btn) #, wx.ALIGN_CENTER_VERTICAL) self.m_panel.SetupScrolling(rate_y=SCROLL_RATE, scrollToTop=True) @@ -361,9 +359,9 @@ class Cast(wx.Frame): self.m_sizer.Fit(self) self.m_sizer.Layout() - def set_chromecast(self, event, chromecast, uri, provider_index): + def set_chromecast(self, event, chromecast, uri, channel_index): self.m_selected_chromecast = chromecast - self.show_player(event, uri, provider_index) + self.show_player(event, uri, channel_index) def cast(self, _, uri): mimetype = "video/mp4" @@ -377,7 +375,7 @@ class Cast(wx.Frame): while True: if player_state != cast.media_controller.status.player_state: player_state = cast.media_controller.status.player_state - print("Player state:", player_state) + # print("Player state:", player_state) if player_state == "PLAYING": has_played = True