forked from Drrickk/BiliExp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BiliExp.py
169 lines (148 loc) · 7.08 KB
/
BiliExp.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- coding: utf-8 -*-
import asyncio, time, logging, sys, io, os
from importlib import import_module
from collections import OrderedDict
from getopt import getopt
from BiliClient import asyncbili
import tasks
try:
from json5 import loads
except:
from json import loads
main_version = (1, 2, 0)
main_version_str = '.'.join(map(str, main_version))
def version_compare(version: str):
ver = tuple(map(int, version.strip().split('.')))
for ii in range(len(main_version)):
if ver[ii] < main_version[ii]:
return False
return True
def initlog(log_file: str, log_console: bool, msg_raw: bool = False):
'''初始化日志参数'''
logger_raw = logging.getLogger()
logger_raw.setLevel(logging.INFO)
formatter1 = logging.Formatter("[%(levelname)s]: %(message)s")
if log_file:
try:
file_handler = logging.FileHandler(log_file, encoding='utf-8')#输出到日志文件
file_handler.setFormatter(formatter1)
logger_raw.addHandler(file_handler)
except:
...
if log_console:
console_handler = logging.StreamHandler(stream=sys.stdout) #输出到控制台
console_handler.setFormatter(formatter1)
logger_raw.addHandler(console_handler)
formatter2 = logging.Formatter("%(message)s")
if msg_raw:
log_raw = io.StringIO() #用于记录完整日志
strio_handler = logging.StreamHandler(stream=log_raw) #输出到log_raw用于消息推送
strio_handler.setFormatter(formatter2)
logger_raw.addHandler(strio_handler)
return log_raw
return None
def init_message(configData: dict):
'''初始化消息推送'''
if 'webhook' in configData and 'variable' in configData["webhook"]:
tasks.webhook.set(configData["webhook"])
if 'msg_raw' in configData["webhook"]["variable"]:
log_raw = initlog(configData["log_file"], configData["log_console"], True)
tasks.webhook.addMsgStream('msg_raw', log_raw)
else:
initlog(configData["log_file"], configData["log_console"])
if 'msg_simple' in configData["webhook"]["variable"]:
tasks.webhook.addMsgStream('msg_simple')
else:
initlog(configData["log_file"], configData["log_console"])
def load_config(path: str) -> OrderedDict:
'''加载配置文件'''
if path:
with open(path,'r',encoding='utf-8') as fp:
return loads(fp.read(), object_pairs_hook=OrderedDict)
else:
for path in ('./config/config.json', './config.json', '/etc/BiliExp/config.json'):
if os.path.exists(path):
with open(path,'r',encoding='utf-8') as fp:
return loads(fp.read(), object_pairs_hook=OrderedDict)
raise RuntimeError('未找到配置文件')
async def start(configData: dict):
'''开始任务'''
config_version = configData.get('version', '1.0.0')
if version_compare(config_version):
logging.info(f'当前程序版本为v{main_version_str},配置文件版本为v{config_version}')
else:
logging.warning(f'当前程序版本为v{main_version_str},配置文件版本为v{config_version},可更新配置文件')
tasks.webhook.addMsg('msg_simple', '有新版本配置文件可供使用\n')
await asyncio.wait([run_user_tasks(user, configData["default"]) for user in configData["users"]]) #执行任务
await tasks.webhook.send() #推送消息
async def run_user_tasks(user: dict, #用户配置
default: dict #默认配置
) -> None:
async with asyncbili() as biliapi:
try:
if not await biliapi.login_by_cookie(user["cookieDatas"]):
logging.warning(f'id为{user["cookieDatas"]["DedeUserID"]}的账户cookie失效,跳过此账户后续操作')
tasks.webhook.addMsg('msg_simple', f'id为{user["cookieDatas"]["DedeUserID"]}的账户cookie失效\n')
return
except Exception as e:
logging.warning(f'登录验证id为{user["cookieDatas"]["DedeUserID"]}的账户失败,原因为{str(e)},跳过此账户后续操作')
return
show_name = user.get("show_name", "")
if show_name:
biliapi.name = show_name
logging.info(f'{biliapi.name}: 等级{biliapi.level},经验{biliapi.myexp},剩余硬币{biliapi.mycoin}')
tasks.webhook.addMsg('msg_simple', f'{biliapi.name}: 等级{biliapi.level},经验{biliapi.myexp},剩余硬币{biliapi.mycoin}\n')
task_array = [] #存放本账户所有任务
for task in default: #遍历任务列表,把需要运行的任务添加到task_array
try:
task_module = import_module(f'tasks.{task}') #加载任务模块
except ModuleNotFoundError:
logging.error(f'{biliapi.name}: 未找到任务模块{task}')
continue
task_function = getattr(task_module, task, None)
if not task_function:
logging.error(f'{biliapi.name}: 未找到任务{task}的入口函数')
continue
if task in user["tasks"]:
if isinstance(user["tasks"][task], bool):
if user["tasks"][task]:
task_array.append(task_function(biliapi))
elif isinstance(user["tasks"][task], dict):
if 'enable' in user["tasks"][task] and user["tasks"][task]["enable"]:
task_array.append(task_function(biliapi, user["tasks"][task]))
else:
if isinstance(default[task], bool):
if default[task]:
task_array.append(task_function(biliapi))
elif isinstance(default[task], dict):
if 'enable' in default[task] and default[task]["enable"]:
task_array.append(task_function(biliapi, default[task]))
if task_array:
await asyncio.wait(task_array) #异步等待所有任务完成
def main(*args, **kwargs):
try:
configData = load_config(kwargs.get("config", None))
except Exception as e:
print(f'配置加载异常,原因为{str(e)},退出程序')
sys.exit(6)
if 'log' in kwargs:
configData["log_file"] = kwargs["log"]
init_message(configData) #初始化消息推送
#启动任务
loop = asyncio.get_event_loop()
loop.run_until_complete(start(configData))
if __name__=="__main__":
kwargs = {}
opts, args = getopt(sys.argv[1:], "hvc:l:",["configfile=","logfile="])
for opt, arg in opts:
if opt in ('-c','--configfile'):
kwargs["config"] = arg
elif opt in ('-l','--logfile'):
kwargs["log"] = arg
elif opt == '-h':
print('BliExp -c <configfile> -l <logfile>')
sys.exit()
elif opt == '-v':
print(f'BiliExp v{main_version_str}')
sys.exit()
main(**kwargs)