59 lines
2.2 KiB
Python
59 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
||
"""米视TV 使用统计解析器 —— 读 stdin 的 [ACCESS] 日志行,argv[1]=时间窗描述"""
|
||
import sys, re, collections
|
||
|
||
since = sys.argv[1] if len(sys.argv) > 1 else '168h'
|
||
rx = re.compile(r'^\[ACCESS\] (\S+) ip=(\S+) (\S+) (\S+) ua="([^"]*)"')
|
||
rows = []
|
||
for line in sys.stdin:
|
||
m = rx.match(line.strip())
|
||
if m:
|
||
rows.append(m.groups())
|
||
if not rows:
|
||
print(f"最近 {since} 没有访问记录([ACCESS] 日志从 v-fixed34 起启用)")
|
||
sys.exit(0)
|
||
|
||
def is_live(p): return 'live=ts' in p or 'live=m3u8' in p or 'live=lines' in p or 'live=ch' in p
|
||
def is_page(p): return p in ('/', '/live', '/login') or (not p.startswith(('/api', '/_next', '/screenshot', '/manifest')))
|
||
|
||
days = collections.defaultdict(set)
|
||
per_ip = collections.Counter()
|
||
live_ips = collections.Counter()
|
||
pages = collections.Counter()
|
||
for ts, ip, method, path, ua in rows:
|
||
days[ts[:10]].add(ip)
|
||
per_ip[ip] += 1
|
||
if is_live(path): live_ips[ip] += 1
|
||
if is_page(path): pages[path] += 1
|
||
|
||
print(f"=== 米视TV 使用统计(最近 {since})===")
|
||
print(f"日志范围: {rows[0][0]} ~ {rows[-1][0]}")
|
||
print(f"总请求数: {len(rows)} 独立 IP 数: {len(set(per_ip))}")
|
||
print(f"看过直播的独立 IP: {len(live_ips)}")
|
||
print("\n按天独立 IP:")
|
||
for d in sorted(days):
|
||
print(f" {d}: {len(days[d])}")
|
||
print("\nTop 访问 IP (IP | 请求 | 其中直播):")
|
||
for ip, c in per_ip.most_common(10):
|
||
print(f" {ip:40s} {c:6d} {live_ips.get(ip, 0)}")
|
||
if pages:
|
||
print("\n页面访问:")
|
||
for p, c in pages.most_common(8):
|
||
print(f" {p:24s} {c}")
|
||
print("\n提示: NAT/共享出口 IP 会把多人算一个;同一人换网络会算多个 —— 数字为近似值。")
|
||
|
||
# ---------- TVBOX 客户端统计 ----------
|
||
tb_req, tb_ips, tb_live_ips, ua_tb = 0, set(), set(), collections.Counter()
|
||
for ts, ip, m, p, ua in rows:
|
||
if p.startswith("/api/tvbox"):
|
||
tb_req += 1
|
||
tb_ips.add(ip)
|
||
ua_tb[ua or "(none)"] += 1
|
||
if is_live(p):
|
||
tb_live_ips.add(ip)
|
||
print(f"\n===== TVBOX =====")
|
||
print(f"配置/接口请求 {tb_req} 次,独立 IP {len(tb_ips)};其中看直播 IP {len(tb_live_ips)}")
|
||
print("Top UA:")
|
||
for ua, c in ua_tb.most_common(6):
|
||
print(f" {ua[:56]:56s} {c}")
|