35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""从 sources 目录读取直播源"""
|
|
|
|
import os
|
|
|
|
|
|
def load_sources(sources_dir):
|
|
"""读取 sources 目录下所有 .txt 文件,返回 (category, name, url) 列表"""
|
|
channels = []
|
|
if not os.path.isdir(sources_dir):
|
|
return channels
|
|
|
|
for filename in sorted(os.listdir(sources_dir)):
|
|
if not filename.endswith(".txt"):
|
|
continue
|
|
category = filename[:-4]
|
|
filepath = os.path.join(sources_dir, filename)
|
|
with open(filepath, "r", encoding="utf-8") as f:
|
|
for line in f:
|
|
line = line.strip()
|
|
if not line or line.startswith("#"):
|
|
continue
|
|
if "," not in line:
|
|
continue
|
|
name, url = line.split(",", 1)
|
|
name = name.strip()
|
|
url = url.strip()
|
|
if name and url:
|
|
channels.append({
|
|
"category": category,
|
|
"name": name,
|
|
"url": url,
|
|
})
|
|
return channels
|