This repository has been archived by the owner on Sep 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconsole_run.py
121 lines (95 loc) · 3.35 KB
/
console_run.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import argparse
import os
import warnings
from LatiaoAid import LatiaoAid
from Logger import Logger
def parse_args() -> argparse.Namespace:
"""Parse the command line arguments for the `LatiaoAid` binary.
:return: Namespace with parsed arguments.
"""
parser = argparse.ArgumentParser(
prog="python3 console_run.py",
description="Collects 辣条/亲密度 on live.bilibili.com using selenium webdriver",
)
parser.add_argument(
"-r", "--room", type=int, help="default room", default=22198526,
)
# runtime behaviors
parser.add_argument(
"--headless", help="do not show the browser", action="store_true",
)
parser.add_argument(
"-d", "--disable-image", help="do not show the images", action="store_true",
)
# log settings
parser.add_argument(
"--silent", help="do not print log to console", action="store_true",
)
parser.add_argument(
"-l", "--log", help="save log to the log file", action="store_true",
)
# arg check
parser.add_argument(
"--skip-check", help="skip the arg check", action="store_true",
)
# timer
parser.add_argument(
"-s", "--second", type=int, help="planned running time in seconds", default=0,
)
parser.add_argument(
"-m", "--minute", type=int, help="planned running time in minutes", default=0,
)
# paths
parser.add_argument(
"--log-path", type=str, help="path of the log file", default="./log.txt",
)
parser.add_argument(
"--driver-path",
type=str,
help="path of the geckodriver. If it's not install, "
"see https://github.com/mozilla/geckodriver/releases for more information",
default="/usr/local/bin/geckodriver",
)
return parser.parse_args()
def check_args(args: argparse.Namespace):
"""Check whether the arguments is valid.
:note: Only checks the existence of the file, not its validity
"""
if not os.path.isfile(args.driver_path):
raise FileNotFoundError("Unable to find the geckodriver")
if not os.path.isfile(args.log_path) and args.log:
warnings.warn(f"Unable to find the log:{args.log_path}, create it now")
with open(args.log_path, "w") as f:
f.write("")
if 0 < args.second + args.minute * 60 < 60:
warnings.warn(
f"设置程序倒计时停止时间为{args.second + args.minute * 60}秒,在如此短的时间内可能无法获取足够的辣条."
)
def run_LatiaoAid_main(args: argparse.Namespace):
"""
Run LatiaoAid.main with given args
:param args: Namespace with parsed arguments.
:return: None
"""
return LatiaoAid(
geckodriver_path=args.driver_path,
headless=args.headless,
disable_image=args.disable_image,
seconds_before_exit=args.second + args.minute * 60,
room=args.room,
logger=Logger(
print_to_console=not args.silent,
print_to_log=args.log,
log_path=args.log_path,
),
).main()
if __name__ == "__main__":
# parse the input args
args = parse_args()
# check the args unless skip it
if not args.skip_check:
check_args(args)
# set the base_tab_link
LatiaoAid.base_tab_link = f"https://live.bilibili.com/{args.room}"
# run the LatiaoAid.main
run_LatiaoAid_main(args)