cast/src/Channel/SVT/__init__.py

106 lines
3.5 KiB
Python
Raw Normal View History

2021-12-07 09:51:53 +01:00
#!/usr/bin/env python3
import json
2021-12-19 14:19:27 +01:00
import threading
from datetime import datetime
2021-12-07 09:51:53 +01:00
import feedparser
import requests
import wx
2021-12-07 09:51:53 +01:00
from bs4 import BeautifulSoup
from Channel import Channel
2021-12-07 09:51:53 +01:00
from Items import Item
2022-01-08 21:21:34 +01:00
from Utils import (add_video, hash_string, make_bitmap_from_url,
resolve_svt_channel, video_exists)
default_rss_url = 'http://www.svtplay.se/rss.xml'
2021-12-07 09:51:53 +01:00
2021-12-26 15:18:47 +01:00
class SVT(Channel):
def __init__(self, svt_id: str) -> None:
chan_dict = resolve_svt_channel(svt_id)
logo = chan_dict['thumbnail']
name = chan_dict['name']
2022-01-08 21:21:34 +01:00
super().__init__(svt_id, 'SVT', default_rss_url, logo, name)
2021-12-07 09:51:53 +01:00
2021-12-19 14:19:27 +01:00
self.m_thr = threading.Thread(target=self.parse_feed,
args=[svt_id],
2021-12-19 14:19:27 +01:00
kwargs={})
2022-01-08 21:21:34 +01:00
def refresh(self) -> None:
2021-12-19 14:19:27 +01:00
self.m_thr.start()
2021-12-19 14:19:27 +01:00
def wait(self) -> bool:
return self.m_thr.is_alive()
def parse_feed(self, *args, **kwargs) -> None:
svt_id = args[0]
2021-12-07 09:51:53 +01:00
feed = feedparser.parse(self.get_feed())
entries = feed['entries']
2021-12-19 14:19:27 +01:00
self.m_items: list[Item] = list()
2022-01-08 21:21:34 +01:00
resolved_link = str()
description = str()
title = str()
thumbnail_link = str()
thumbnail: wx.Bitmap = wx.Bitmap()
published_parsed: datetime = datetime.now()
video_id = str()
2021-12-07 09:51:53 +01:00
if svt_id == 'feed':
for entry in entries:
2022-01-08 21:21:34 +01:00
video_id = hash_string(entry['id'])
if video_exists(video_id, svt_id):
pass
for link in entry['links']:
if str(link['type']).startswith('image/'):
thumbnail_link = str(link['href'])
break
page = requests.get(str(entry['link']))
soup = BeautifulSoup(page.text, 'html.parser')
for element in soup.find_all('a'):
href = element.get('href')
datart = element.get('data-rt')
if datart == 'top-area-play-button':
svt_id = href.split('=')[1].split('&')[0]
resolved_link = 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))
else:
chan_dict = resolve_svt_channel(svt_id)
resolved_link = resolve_link(svt_id)
2022-01-08 21:21:34 +01:00
video_id = hash_string(resolved_link)
title = chan_dict['name']
description = "Live channel stream"
thumbnail = chan_dict['thumbnail']
2022-01-08 21:21:34 +01:00
if resolved_link:
item = Item(description, resolved_link, self.m_provider_name,
published_parsed, thumbnail, title)
self.m_items.append(item)
add_video(video_id, svt_id, self.m_provider_name, description,
resolved_link, published_parsed, thumbnail, title, 0)
def resolve_link(svt_id: str) -> str:
api = json.loads(
requests.get('https://api.svt.se/video/{}'.format(svt_id)).text)
resolved_link = ''
try:
for reference in api['videoReferences']:
if reference['format'] == "dashhbbtv":
resolved_link = reference['url']
except KeyError:
pass
return resolved_link