Skip to content

Commit

Permalink
更新数据源
Browse files Browse the repository at this point in the history
  • Loading branch information
shaonianzhentan committed May 16, 2023
1 parent e7a8c4a commit 7581092
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 57 deletions.
22 changes: 6 additions & 16 deletions custom_components/xiaomi_tv/browse_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@

from urllib.parse import urlparse, parse_qs, parse_qsl, quote
from .manifest import manifest
from .tv_source import TVSource
from .iptv import IPTV

tv = IPTV()

protocol = 'xiaomi://'
tv_protocol = 'xiaomi://tv/'
Expand All @@ -74,21 +76,10 @@ def parse_query(url_query):
data[item[0]] = item[1]
return data


# 初始化直播源
async def async_Init_TVSource(hass):
tv = hass.data.get(manifest.domain)
if tv is None:
tv = TVSource()
hass.data.setdefault(manifest.domain, tv)
# 更新数据
await tv.update()
return tv

async def async_browse_media(media_player, media_content_type, media_content_id):
hass = media_player.hass
# 初始化直播源
tv = await async_Init_TVSource(hass)
await tv.get_list()

# 主界面
if media_content_id in [None, XiaomiRouter.tv_home]:
Expand Down Expand Up @@ -150,9 +141,8 @@ 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

hass = media_player.hass
# 初始化直播源
tv = await async_Init_TVSource(hass)
await tv.get_list()

# 协议转换
url = urlparse(media_content_id)
Expand All @@ -161,4 +151,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_channel(kv)
return await tv.search_url(kv)
Original file line number Diff line number Diff line change
@@ -1,41 +1,5 @@
import sys, re, os, aiohttp, time
import os, aiohttp, time

class TVSource():

def __init__(self) -> None:
self.playlist = []
self.groups = []
self.update_time = None

# 更新TV源
async def update(self):
# 缓存一小时
if self.update_time is not None and (time.time() - self.update_time) < 1800:
return

m3ufile = 'xiaomi_tv.m3u'
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)))

self.update_time = time.time()

async def search_channel(self, name):
await self.update()
# 频道搜索
arr = list(filter(lambda x: name.lower() in x.title.lower(), self.playlist))
# print(arr)
if len(arr) > 0:
return arr[0].path

class track():
def __init__(self, group, title, path):
Expand Down Expand Up @@ -68,9 +32,14 @@ def parseM3U(infile):
if line.startswith('#EXTINF:'):
# pull length and title from #EXTINF line
info,title=line.split('#EXTINF:')[1].split(',',1)
matchObj = re.match(r'(.*)status="online"', info)
if matchObj is None:
#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'
Expand Down Expand Up @@ -105,4 +74,59 @@ def parseM3U(infile):

infile.close()

return playlist
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
2 changes: 1 addition & 1 deletion custom_components/xiaomi_tv/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "xiaomi_tv",
"name": "\u5C0F\u7C73\u7535\u89C6",
"version": "2023.5.8",
"version": "2023.5.16",
"config_flow": true,
"documentation": "https://github.com/shaonianzhentan/xiaomi_tv",
"requirements": [
Expand Down

0 comments on commit 7581092

Please sign in to comment.