From 72f9a6128a05b1498dadbf1011289b6ec8346a27 Mon Sep 17 00:00:00 2001 From: shaonianzhentan Date: Thu, 25 May 2023 21:27:16 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=B4=E6=92=AD=E8=B5=84=E6=BA=90=E7=94=B1?= =?UTF-8?q?=E8=AF=AD=E9=9F=B3=E5=8A=A9=E6=89=8B=E6=8F=90=E4=BE=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- custom_components/xiaomi_tv/browse_media.py | 31 +++-- custom_components/xiaomi_tv/iptv.py | 132 -------------------- custom_components/xiaomi_tv/manifest.json | 2 +- 3 files changed, 22 insertions(+), 143 deletions(-) delete mode 100644 custom_components/xiaomi_tv/iptv.py diff --git a/custom_components/xiaomi_tv/browse_media.py b/custom_components/xiaomi_tv/browse_media.py index 1ec1f82..44d6225 100644 --- a/custom_components/xiaomi_tv/browse_media.py +++ b/custom_components/xiaomi_tv/browse_media.py @@ -56,9 +56,6 @@ from urllib.parse import urlparse, parse_qs, parse_qsl, quote from .manifest import manifest -from .iptv import IPTV - -tv = IPTV() protocol = 'xiaomi://' tv_protocol = 'xiaomi://tv/' @@ -78,8 +75,19 @@ def parse_query(url_query): async def async_browse_media(media_player, media_content_type, media_content_id): hass = media_player.hass - # 初始化直播源 - await tv.get_list() + iptv = hass.data.get('conversation_iptv') + if iptv is None: + return BrowseMedia( + media_class=MEDIA_CLASS_DIRECTORY, + media_content_id=media_content_id, + media_content_type=MEDIA_TYPE_CHANNEL, + title="请安装语音小助手", + can_play=False, + can_expand=False, + children=[], + ) + + playlist = await iptv.get_list() # 主界面 if media_content_id in [None, XiaomiRouter.tv_home]: @@ -93,7 +101,7 @@ async def async_browse_media(media_player, media_content_type, media_content_id) children=[], ) # 分组列表 - for item in tv.groups: + for item in iptv.groups: library_info.children.append( BrowseMedia( title=item, @@ -122,8 +130,9 @@ async def async_browse_media(media_player, media_content_type, media_content_id) can_expand=False, children=[], ) - channels = filter(lambda x: x.group == group, tv.playlist) + channels = filter(lambda x: x.group == group, playlist) for item in channels: + print(item.path) library_info.children.append( BrowseMedia( title=item.title, @@ -141,8 +150,10 @@ async def async_play_media(media_player, media_content_type, media_content_id): if media_content_id is None or media_content_id.startswith(protocol) == False: return - # 初始化直播源 - await tv.get_list() + iptv = media_player.hass.data.get('conversation_iptv') + if iptv is None: + return None + await iptv.get_list() # 协议转换 url = urlparse(media_content_id) @@ -151,4 +162,4 @@ async def async_play_media(media_player, media_content_type, media_content_id): if media_content_id.startswith(XiaomiRouter.tv_search): kv = query.get('kv') # 电视搜索 - return await tv.search_url(kv) \ No newline at end of file + return await iptv.search_url(kv) \ No newline at end of file diff --git a/custom_components/xiaomi_tv/iptv.py b/custom_components/xiaomi_tv/iptv.py deleted file mode 100644 index 1e3890b..0000000 --- a/custom_components/xiaomi_tv/iptv.py +++ /dev/null @@ -1,132 +0,0 @@ -import os, aiohttp, time - - -class track(): - def __init__(self, group, title, path): - self.group = group - self.title = title - self.path = path - -def parseM3U(infile): - try: - assert(type(infile) == '_io.TextIOWrapper') - except AssertionError: - infile = open(infile,'r') - - """ - All M3U files start with #EXTM3U. - If the first line doesn't start with this, we're either - not working with an M3U or the file we got is corrupted. - """ - - line = infile.readline() - if not line.startswith('#EXTM3U'): - return - - # initialize playlist variables before reading file - playlist=[] - song=track(None,None,None) - - for line in infile: - line=line.strip() - if line.startswith('#EXTINF:'): - # pull length and title from #EXTINF line - info,title=line.split('#EXTINF:')[1].split(',',1) - #print(info, title) - - # 过滤失效 - if '[Timeout]' in title or '[Geo-blocked]' in title: - #matchObj = re.match(r'(.*)status="online"', info) - #if matchObj is None: - continue - - group = '默认列表' - if 'CCTV' in title: - group = 'CCTV' - elif 'NewTV' in title: - group = 'NewTV' - elif 'SiTV' in title: - group = 'SiTV' - elif '电影' in title or '影视' in title or '影視' in title or '视频' in title: - group = '电影视频' - elif '新闻' in title or '新聞' in title or '资讯' in title: - group = '新闻资讯' - elif '少儿' in title or '卡通' in title: - group = '少儿卡通' - elif '卫视' in title or '衛視' in title: - group = '卫视' - elif '上海' in title: - group = '上海' - elif '浙江' in title: - group = '浙江' - elif '江苏' in title: - group = '江苏' - elif '中国' in title or '中文' in title: - group = '中文频道' - song=track(group,title,None) - elif (len(line) != 0): - # pull song path from all other, non-blank lines - song.path=line - if song.group is not None: - playlist.append(song) - # reset the song variable so it doesn't use the same EXTINF more than once - song=track(None,None,None) - - infile.close() - - return playlist - -class IPTV(): - - def __init__(self) -> None: - self.playlist = [] - self.groups = [] - - async def get_list(self): - m3ufile = 'iptv.m3u' - is_download = False - if os.path.exists(m3ufile) == True: - # 更新时间小于30分钟,播放列表不为空,则停止更新 - if time.time() < os.path.getmtime(m3ufile) + 60000 * 30: - if len(self.playlist) > 0: - print('每隔30分钟更新一次') - return self.playlist - else: - is_download = True - else: - is_download = True - - if is_download: - print('拉取最新资源') - m3u_url = 'https://ghproxy.com/https://raw.githubusercontent.com/iptv-org/iptv/master/streams/cn.m3u' - # 下载文件 - request_timeout = aiohttp.ClientTimeout(total=10) - async with aiohttp.ClientSession(timeout=request_timeout) as session: - async with session.get(m3u_url) as response: - with open(m3ufile,"wb") as fs: - fs.write(await response.read()) - # 解析文件 - playlist = parseM3U(m3ufile) - self.playlist = playlist - # 分组 - self.groups = list(set(map(lambda x: x.group, playlist))) - - return self.playlist - - async def search_list(self, name): - ''' 列表搜索 ''' - playlist = await self.get_list() - return list(filter(lambda x: name.lower() in x.title.lower(), playlist)) - - async def search_item(self, name): - ''' 列表项搜索 ''' - playlist = await self.get_list() - _list = list(filter(lambda x: name.lower() in x.title.lower(), playlist)) - if len(_list) > 0: - return _list[0] - - async def search_url(self, name): - ''' 播放URL搜索 ''' - item = await self.search_item(name) - if item is not None: - return item.path diff --git a/custom_components/xiaomi_tv/manifest.json b/custom_components/xiaomi_tv/manifest.json index 9a51d2a..e933e10 100644 --- a/custom_components/xiaomi_tv/manifest.json +++ b/custom_components/xiaomi_tv/manifest.json @@ -1,7 +1,7 @@ { "domain": "xiaomi_tv", "name": "\u5C0F\u7C73\u7535\u89C6", - "version": "2023.5.16", + "version": "2023.5.25", "config_flow": true, "documentation": "https://github.com/shaonianzhentan/xiaomi_tv", "requirements": [