-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
55 lines (43 loc) · 1.53 KB
/
Copy pathapp.py
File metadata and controls
55 lines (43 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import os
import subprocess
import sys
from datetime import datetime
def start_http_server(project_root: str, port: int) -> subprocess.Popen:
os.chdir(project_root)
http_cmd = [
sys.executable,
'-m',
'http.server',
str(port),
]
print(f"$ {' '.join(http_cmd)} (cwd={os.getcwd()})")
return subprocess.Popen(http_cmd)
def print_current_counts(project_root: str) -> None:
# 动态导入 tools.stats.collect_stats
try:
from tools.stats import collect_stats
except ImportError:
sys.path.insert(0, project_root)
from tools.stats import collect_stats # type: ignore
data_dir = os.path.join(project_root, 'data')
stats = collect_stats(data_dir)
print(f"当前电视台(频道)总数: {stats.get('total_channels', 0)}")
print(f"当前节目总数: {stats.get('total_videos', 0)}")
def main():
parser = argparse.ArgumentParser(description='先更新节目,再启动静态服务器')
parser.add_argument('port', type=int, nargs='?', default=8000, help='HTTP 服务端口,默认 8000')
args = parser.parse_args()
project_root = os.path.dirname(os.path.abspath(__file__))
# 仅打印当前统计 -> 启动服务
print_current_counts(project_root)
server = start_http_server(project_root, args.port)
# 前台等待,转发信号由操作系统处理
try:
server.wait()
except KeyboardInterrupt:
pass
if __name__ == '__main__':
main()