75 lines
2.3 KiB
Python
75 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
import re
|
|
import sys
|
|
from collections import OrderedDict
|
|
from pathlib import Path
|
|
|
|
pat = re.compile(r"(https://live\.douyin\.com/\d+),主播:\s*(.+)")
|
|
key_pat = re.fullmatch
|
|
|
|
|
|
def parse(text: str):
|
|
channels = OrderedDict()
|
|
current = None
|
|
current_key = None
|
|
for line in text.splitlines():
|
|
line = line.strip().lstrip("\ufeff")
|
|
if not line:
|
|
continue
|
|
m = pat.match(line)
|
|
if m:
|
|
if current:
|
|
channels[current]["urls"].append(m.groups())
|
|
elif re.fullmatch(r"[a-z0-9]{4}(-[a-z0-9]{4}){4}", line):
|
|
current_key = line
|
|
else:
|
|
current = line
|
|
channels[current] = {"key": current_key or "", "urls": []}
|
|
return channels
|
|
|
|
|
|
def main() -> int:
|
|
text = Path(sys.argv[1]).read_text(encoding="utf-8") if len(sys.argv) > 1 else sys.stdin.read()
|
|
channels = parse(text)
|
|
all_entries = []
|
|
dupes_cross = {}
|
|
seen_global = {}
|
|
for ch, info in channels.items():
|
|
seen = set()
|
|
deduped = []
|
|
for url, name in info["urls"]:
|
|
all_entries.append((url, name, ch))
|
|
if url in seen:
|
|
continue
|
|
seen.add(url)
|
|
deduped.append((url, name))
|
|
if url in seen_global:
|
|
dupes_cross.setdefault(url, set()).update([seen_global[url], ch])
|
|
else:
|
|
seen_global[url] = ch
|
|
info["deduped"] = deduped
|
|
|
|
unique = OrderedDict()
|
|
for url, name, _ in all_entries:
|
|
unique.setdefault(url, name)
|
|
|
|
total = sum(len(v["urls"]) for v in channels.values())
|
|
print(f"原始条目: {total}")
|
|
print(f"去重后唯一 URL: {len(unique)}")
|
|
print(f"重复条目: {total - len(unique)}")
|
|
print("\n=== 跨频道重复 ===")
|
|
for url in sorted(dupes_cross, key=lambda u: u.rsplit("/", 1)[-1]):
|
|
name = unique[url]
|
|
print(f"{url},主播: {name}")
|
|
print(f" -> {', '.join(sorted(dupes_cross[url]))}")
|
|
print("\n=== 各频道去重后 ===")
|
|
for ch, info in channels.items():
|
|
print(f"\n## {ch} | key={info['key']} | {len(info['deduped'])} 条")
|
|
for url, name in info["deduped"]:
|
|
print(f"{url},主播: {name}")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|