Clear up what various indexes were

main
Micke Nordin 3 years ago
parent 8e9086475d
commit 390df8abd8
Signed by: micke
GPG Key ID: 014B273D614BE877

@ -18,16 +18,14 @@ default_rss_url = 'http://www.svtplay.se/rss.xml'
class SVT(Channel): class SVT(Channel):
def __init__(self, svt_id: str) -> None: def __init__(self, channel_id: str) -> None:
chan_dict = resolve_svt_channel(svt_id) chan_dict = resolve_svt_channel(channel_id)
logo = chan_dict['thumbnail'] logo = chan_dict['thumbnail']
name = chan_dict['name'] name = chan_dict['name']
super().__init__(svt_id, 'SVT', default_rss_url, logo, name) super().__init__(channel_id, 'SVT', default_rss_url, logo, name)
self.refresh()
def parse_feed(self) -> None: def parse_feed(self) -> None:
self.m_items: list[Item] = list() #self.m_items: list[Item] = list()
resolved_link = str() resolved_link = str()
description = str() description = str()
title = str() title = str()
@ -36,13 +34,14 @@ class SVT(Channel):
published_parsed: datetime = datetime.now() published_parsed: datetime = datetime.now()
video_id = str() video_id = str()
if self.m_id == 'feed': if self.m_id == 'feed' :
feed = feedparser.parse(self.get_feed()) feed = feedparser.parse(self.get_feed())
entries = feed['entries'] entries = feed['entries']
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_id): svt_id = str()
break if video_exists(video_id, 'feed'):
continue
for link in entry['links']: for link in entry['links']:
if str(link['type']).startswith('image/'): if str(link['type']).startswith('image/'):
thumbnail_link = str(link['href']) thumbnail_link = str(link['href'])
@ -56,13 +55,16 @@ class SVT(Channel):
datart = element.get('data-rt') datart = element.get('data-rt')
if datart == 'top-area-play-button': 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']) description = str(entry['description'])
published_parsed = entry['published_parsed'] published_parsed = entry['published_parsed']
title = str(entry['title']) title = str(entry['title'])
if resolved_link and thumbnail_link: print('pre')
if not resolved_link:
continue
print('post')
thumbnail = make_bitmap_from_url( thumbnail = make_bitmap_from_url(
thumbnail_link, wx.Size(self.m_screen_width, 150)) thumbnail_link, wx.Size(self.m_screen_width, 150))
item = Item(description, resolved_link, self.m_provider_name, item = Item(description, resolved_link, self.m_provider_name,
@ -73,7 +75,7 @@ class SVT(Channel):
else: else:
chan_dict = resolve_svt_channel(self.m_id) 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) video_id = hash_string(resolved_link)
title = chan_dict['name'] title = chan_dict['name']
description = "Live channel stream" description = "Live channel stream"
@ -85,9 +87,9 @@ class SVT(Channel):
self.m_items.append(item) self.m_items.append(item)
def resolve_link(self) -> str: def resolve_link(self,svt_id) -> str:
api = json.loads( 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 = '' resolved_link = ''
try: try:
@ -97,3 +99,4 @@ class SVT(Channel):
except KeyError: except KeyError:
pass pass
return resolved_link return resolved_link

@ -20,8 +20,6 @@ class YouTube(Channel):
self.m_items: list[Item] = list() 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)
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 = {

@ -22,7 +22,9 @@ class Channel:
self.m_name = name self.m_name = name
self.m_feed = feed self.m_feed = feed
self.m_items: list[Item] = get_videos(channel_id) 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: def get_logo_as_bitmap(self) -> wx.Bitmap:
return self.m_logo return self.m_logo
@ -33,6 +35,12 @@ class Channel:
def get_items(self) -> Union[list[Item], None]: def get_items(self) -> Union[list[Item], None]:
return self.m_items 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: def get_name(self) -> str:
return self.m_name return self.m_name
@ -40,7 +48,7 @@ class Channel:
return self.m_provider_name return self.m_provider_name
def parse_feed(self) -> Union[list[Item], None]: def parse_feed(self) -> Union[list[Item], None]:
pass return None
def refresh(self) -> None: def refresh(self) -> None:
if self.wait(): if self.wait():
@ -50,6 +58,12 @@ class Channel:
kwargs={}) kwargs={})
self.m_thr.start() 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: def wait(self) -> bool:
result = False result = False
try: try:
@ -58,12 +72,3 @@ class Channel:
pass pass
return result 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

@ -12,7 +12,7 @@ MYPATH = path.dirname(path.abspath(__file__))
class ChannelProvider: class ChannelProvider:
def __init__(self, providerid: str, channels=list()): def __init__(self, providerid: str, channels:list[Channel]):
self.m_id = providerid self.m_id = providerid
self.m_logo: wx.Bitmap = get_default_logo(providerid) self.m_logo: wx.Bitmap = get_default_logo(providerid)
self.m_channels: list[Channel] = channels self.m_channels: list[Channel] = channels

@ -54,7 +54,6 @@ def add_video(video_id: str,
watchtime: int, watchtime: int,
basepath: str = BASEPATH, basepath: str = BASEPATH,
filename: str = DB_FILE_NAME) -> None: filename: str = DB_FILE_NAME) -> None:
try: try:
timestamp = published.timestamp() #type: ignore timestamp = published.timestamp() #type: ignore
except AttributeError: except AttributeError:
@ -66,12 +65,13 @@ def add_video(video_id: str,
fullpath = path.join(basepath, filename) fullpath = path.join(basepath, filename)
if not path.isdir(thumbpath): if not path.isdir(thumbpath):
makedirs(thumbpath) makedirs(thumbpath)
if not path.isfile(thumbnail):
bitmap.SaveFile(thumbnail, wx.BITMAP_TYPE_PNG) bitmap.SaveFile(thumbnail, wx.BITMAP_TYPE_PNG)
con = sqlite3.connect(fullpath) con = sqlite3.connect(fullpath)
cur = con.cursor() cur = con.cursor()
create_query: str = '''CREATE TABLE IF NOT EXISTS {} create_query: str = '''CREATE TABLE IF NOT EXISTS {}
(video_id TEXT PRIMARY KEY, channel_id TEXT, provider_id TEXT, (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) published DATETIME, watchtime NUMBER)'''.format(VIDEO_TABLE)
cur.execute(create_query) cur.execute(create_query)
con.commit() con.commit()

@ -34,7 +34,6 @@ class Cast(wx.Frame):
super().__init__(*args, **kw) super().__init__(*args, **kw)
self.m_selected_chromecast = None self.m_selected_chromecast = None
self.SetSizeHints(WIDTH, HEIGHT, maxW=WIDTH) self.SetSizeHints(WIDTH, HEIGHT, maxW=WIDTH)
self.m_index = 0
self.m_chromecast_thr = threading.Thread(target=self.get_chromecasts, self.m_chromecast_thr = threading.Thread(target=self.get_chromecasts,
args=(), args=(),
kwargs={}) kwargs={})
@ -46,8 +45,8 @@ class Cast(wx.Frame):
self.m_panel.SetupScrolling(rate_y=SCROLL_RATE, scrollToTop=True) self.m_panel.SetupScrolling(rate_y=SCROLL_RATE, scrollToTop=True)
self.m_panel.SetSizer(self.m_sizer) self.m_panel.SetSizer(self.m_sizer)
self.m_providers: list[ChannelProvider] = self.get_providers() self.m_providers: list[ChannelProvider] = self.get_providers()
self.m_selected_channel = None # self.m_selected_channel = None
self.m_selected_provider_index = None # self.m_selected_provider_index = None
self.show_provider_list(None) self.show_provider_list(None)
def add_back_button(self, callback: Callable) -> None: def add_back_button(self, callback: Callable) -> None:
@ -131,8 +130,8 @@ class Cast(wx.Frame):
for provider in self.m_providers: for provider in self.m_providers:
bitmap = provider.get_logo_as_bitmap() bitmap = provider.get_logo_as_bitmap()
callback = lambda event, index=provider_index: self.show_channel_list( callback = lambda event, pindex=provider_index: self.show_channel_list(
event, index) event, pindex)
btn_sizer: wx.BoxSizer = make_sized_button(self.m_panel, bitmap, btn_sizer: wx.BoxSizer = make_sized_button(self.m_panel, bitmap,
provider.get_name(), provider.get_name(),
callback) callback)
@ -162,8 +161,8 @@ class Cast(wx.Frame):
for channel in self.m_selected_provider.get_channels(): for channel in self.m_selected_provider.get_channels():
bitmap = channel.get_logo_as_bitmap() bitmap = channel.get_logo_as_bitmap()
callback = lambda event, index=channel_index: self.show_video_list( callback = lambda event, cindex=channel_index: self.show_video_list(
event, index) event, cindex)
btn_sizer: wx.BoxSizer = make_sized_button(self.m_panel, bitmap, btn_sizer: wx.BoxSizer = make_sized_button(self.m_panel, bitmap,
channel.get_name(), channel.get_name(),
callback) callback)
@ -175,13 +174,17 @@ class Cast(wx.Frame):
self.m_sizer.Fit(self) self.m_sizer.Fit(self)
self.m_sizer.Layout() 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.Clear(delete_windows=True)
self.m_sizer = wx.BoxSizer(wx.VERTICAL) self.m_sizer = wx.BoxSizer(wx.VERTICAL)
# self.m_sizer.AddSpacer(SPACER_HEIGHT * 4) # self.m_sizer.AddSpacer(SPACER_HEIGHT * 4)
channel = self.m_selected_provider.get_channel_by_index(index) 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): def refresh_callback(event):
if self.m_selected_provider_index == 1 and index == 0: if self.m_selected_provider.get_name() == 'YouTube' and channel_index == 0:
with wx.BusyInfo("Please wait, working..."): with wx.BusyInfo("Please wait, working..."):
for chan in self.m_selected_provider.get_channels(): for chan in self.m_selected_provider.get_channels():
chan.refresh() chan.refresh()
@ -194,8 +197,8 @@ class Cast(wx.Frame):
time.sleep(1) time.sleep(1)
wx.GetApp().Yield() wx.GetApp().Yield()
else: else:
channel.refresh() self.m_selected_channel.refresh()
self.show_video_list(event,index ) self.show_video_list(event,channel_index)
refreshbtn = wx.Button(self.m_panel, refreshbtn = wx.Button(self.m_panel,
-1, -1,
@ -204,18 +207,15 @@ class Cast(wx.Frame):
refreshbtn.Bind(wx.EVT_BUTTON, refresh_callback) refreshbtn.Bind(wx.EVT_BUTTON, refresh_callback)
self.m_sizer.Add(refreshbtn) self.m_sizer.Add(refreshbtn)
if channel.wait(): if self.m_selected_channel.wait():
with wx.BusyInfo("Please wait, working..."): with wx.BusyInfo("Please wait, working..."):
while channel.wait(): while self.m_selected_channel.wait():
time.sleep(1) time.sleep(1)
wx.GetApp().Yield() 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) inner_sizer = wx.BoxSizer(wx.VERTICAL)
pane_sizer = wx.BoxSizer(wx.VERTICAL) pane_sizer = wx.BoxSizer(wx.VERTICAL)
title = wx.StaticText(self.m_panel, -1) title = wx.StaticText(self.m_panel, -1)
@ -224,13 +224,13 @@ class Cast(wx.Frame):
bitmap = item["thumbnail"] bitmap = item["thumbnail"]
btn = wx.BitmapButton(self.m_panel, btn = wx.BitmapButton(self.m_panel,
id=self.m_index, id=btnindex,
bitmap=bitmap, bitmap=bitmap,
style=BM_BTN_STYLE) style=BM_BTN_STYLE)
btn.Bind( btn.Bind(
wx.EVT_BUTTON, wx.EVT_BUTTON,
lambda event, link=item["link"], provider_index=index: self. lambda event, link=item["link"], cindex=channel_index: self.
show_player(event, link, provider_index), show_player(event, link, cindex),
) )
inner_sizer.Add(title) inner_sizer.Add(title)
inner_sizer.Add(btn) inner_sizer.Add(btn)
@ -256,13 +256,13 @@ class Cast(wx.Frame):
#inner_sizer.Add(description) #inner_sizer.Add(description)
self.m_sizer.Add(inner_sizer) self.m_sizer.Add(inner_sizer)
self.m_sizer.AddSpacer(SPACER_HEIGHT) 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.SetupScrolling(rate_y=SCROLL_RATE, scrollToTop=True)
self.m_panel.SetSizer(self.m_sizer) self.m_panel.SetSizer(self.m_sizer)
self.m_sizer.Fit(self) self.m_sizer.Fit(self)
self.m_sizer.Layout() self.m_sizer.Layout()
def show_player(self, _, uri, provider_index: int): def show_player(self, _, uri, channel_index: int):
""" """
Show the video player Show the video player
@ -286,8 +286,7 @@ class Cast(wx.Frame):
back_button = wx.Button(self.m_panel, -1, "Back") back_button = wx.Button(self.m_panel, -1, "Back")
back_button.Bind( back_button.Bind(
wx.EVT_BUTTON, wx.EVT_BUTTON,
lambda event, index=provider_index: self.show_video_list( lambda event, cindex=channel_index: self.show_video_list(event, cindex),
event, index),
) )
inner_sizer.Add(self.m_control, (0, 0)) 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 = wx.Button(self.m_panel, -1, "Cast")
chromecast_button.Bind( chromecast_button.Bind(
wx.EVT_BUTTON, wx.EVT_BUTTON,
lambda event, muri=uri, index=provider_index: self. lambda event, muri=uri, cindex=channel_index: self.select_chromecast(event, muri, cindex),
select_chromecast(event, muri, index),
) )
inner_sizer.Add(chromecast_button, (2, 2)) inner_sizer.Add(chromecast_button, (2, 2))
self.m_sizer.Add(inner_sizer) self.m_sizer.Add(inner_sizer)
@ -319,7 +317,7 @@ class Cast(wx.Frame):
play_button.Bind(wx.EVT_BUTTON, self.play) play_button.Bind(wx.EVT_BUTTON, self.play)
pause_button.Bind(wx.EVT_BUTTON, self.pause) 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.load_uri(uri)
self.m_panel.SetupScrolling(rate_y=SCROLL_RATE, scrollToTop=True) self.m_panel.SetupScrolling(rate_y=SCROLL_RATE, scrollToTop=True)
self.m_panel.SetSizer(self.m_sizer) self.m_panel.SetSizer(self.m_sizer)
@ -327,7 +325,7 @@ class Cast(wx.Frame):
self.m_sizer.Layout() self.m_sizer.Layout()
self.m_panel.ScrollChildIntoView(self.m_control) 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.Clear(delete_windows=True)
self.m_sizer = wx.BoxSizer(wx.VERTICAL) self.m_sizer = wx.BoxSizer(wx.VERTICAL)
# self.m_sizer.AddSpacer(SPACER_HEIGHT * 4) # self.m_sizer.AddSpacer(SPACER_HEIGHT * 4)
@ -339,8 +337,8 @@ class Cast(wx.Frame):
style=wx.BU_EXACTFIT) style=wx.BU_EXACTFIT)
cancel_btn.Bind( cancel_btn.Bind(
wx.EVT_BUTTON, wx.EVT_BUTTON,
lambda event, index=provider_index: self.show_video_list( lambda event, cindex=channel_index: self.show_video_list(
event, index), event, cindex),
) )
self.m_sizer.Add(cancel_btn) #, wx.ALIGN_CENTER_VERTICAL) self.m_sizer.Add(cancel_btn) #, wx.ALIGN_CENTER_VERTICAL)
@ -352,8 +350,8 @@ class Cast(wx.Frame):
size=(WIDTH, BTN_HEIGHT)) size=(WIDTH, BTN_HEIGHT))
btn.Bind( btn.Bind(
wx.EVT_BUTTON, wx.EVT_BUTTON,
lambda event, chromecast=cast, muri=uri, index=provider_index: lambda event, chromecast=cast, muri=uri, cindex=channel_index:
self.set_chromecast(event, chromecast, muri, index), self.set_chromecast(event, chromecast, muri, cindex),
) )
self.m_sizer.Add(btn) #, wx.ALIGN_CENTER_VERTICAL) self.m_sizer.Add(btn) #, wx.ALIGN_CENTER_VERTICAL)
self.m_panel.SetupScrolling(rate_y=SCROLL_RATE, scrollToTop=True) 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.Fit(self)
self.m_sizer.Layout() 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.m_selected_chromecast = chromecast
self.show_player(event, uri, provider_index) self.show_player(event, uri, channel_index)
def cast(self, _, uri): def cast(self, _, uri):
mimetype = "video/mp4" mimetype = "video/mp4"
@ -377,7 +375,7 @@ class Cast(wx.Frame):
while True: while True:
if player_state != cast.media_controller.status.player_state: if player_state != cast.media_controller.status.player_state:
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": if player_state == "PLAYING":
has_played = True has_played = True

Loading…
Cancel
Save