-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.py
209 lines (189 loc) · 8.36 KB
/
main.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import json
from datetime import datetime
from time import sleep
from traceback import format_exc
from threading import Lock
import requests
from EsportsHelper.Config import config
from EsportsHelper.GUIThread import GUIThread
from EsportsHelper.I18n import i18n
from EsportsHelper.LiveDataProvider import fetchWatchRegions
from EsportsHelper.Logger import log
from EsportsHelper.LoginHandler import LoginHandler
from EsportsHelper.Match import Match
from EsportsHelper.Stats import stats
from EsportsHelper.Utils import getLolesportsWeb, sysQuit, formatExc, acceptCookies, info, debugScreen
from EsportsHelper.Webdriver import createWebdriver
from rich import print
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait
global driver
_ = i18n.getText
_log = i18n.getLog
def initWebdriver():
"""
Initialize the program by creating a webdriver, setting the window size, opening the Lolesports webpage, and switching the language to English.
"""
global driver
# Generate webdriver
try:
driver = createWebdriver()
except requests.exceptions.ConnectionError:
driver = None
log.error(formatExc(format_exc()))
print(_("网络连接失败!请检查网络", color="red"))
input(_log("按回车键退出"))
sysQuit(driver)
except FileNotFoundError:
driver = None
log.error(formatExc(format_exc()))
print(_("该目录下文件查找失败!请检查路径", color="red"))
input(_log("按回车键退出"))
sysQuit(driver)
except json.decoder.JSONDecodeError:
driver = None
log.error(formatExc(format_exc()))
print(_("出现异常 请尝试手动打开Chrome并关闭后再次重试脚本", color="red"))
input(_log("按回车键退出"))
sysQuit(driver)
except TypeError:
driver = None
log.error(formatExc(format_exc()))
print(_("无法找到最新版谷歌浏览器!如没有下载或不是最新版请检查好再次尝试", color="red"))
print(_("或可以尝试用管理员方式打开", color="red"))
print(_("如果还不行请尝试重装谷歌浏览器", color="red"))
print(_("或者在配置文件中指定Chrome路径或是使用便携版Chrome并指定路径", color="red"))
input(_log("按回车键退出"))
sysQuit(driver)
except MemoryError:
driver = None
log.error(formatExc(format_exc()))
print(_("内存不足!请关闭一些程序后再次尝试", color="red"))
input(_log("按回车键退出"))
sysQuit(driver)
except WebDriverException:
driver = None
log.error(formatExc(format_exc()))
print(_("是否有谷歌浏览器?", color="red"))
print(_("是否打开着谷歌浏览器?请关闭后再次尝试", color="red"))
print(_("如果还不行请尝试重装谷歌浏览器", color="red"))
input(_log("按回车键退出"))
sysQuit(driver)
except Exception:
driver = None
log.error(formatExc(format_exc()))
print(_("是否有谷歌浏览器?", color="red"))
print(_("是不是网络问题?请检查VPN节点是否可用", color="red"))
print(_("如果还不行请尝试重装谷歌浏览器", color="red"))
input(_log("按回车键退出"))
sysQuit(driver)
# Set the window size
driver.set_window_size(960, 768)
driver.set_window_position(0, 0)
def switchLanguage():
# Open lolesports page
try:
watchRegion = fetchWatchRegions()
if watchRegion != "ERROR":
stats.watchRegion = watchRegion
else:
stats.watchRegion = _log("未知")
log.info(f"{_log('观看属地')} {stats.watchRegion}")
getLolesportsWeb(driver)
except Exception:
log.error(
_log("无法打开Lolesports网页,网络问题,将于3秒后退出..."))
log.error(formatExc(format_exc()))
stats.info.append(f"{datetime.now().strftime('%H:%M:%S')} "
f"{_('无法打开Lolesports网页,网络问题,将于3秒后退出...', color='red')}")
debugScreen(driver, "initWeb")
sysQuit(driver, _log("无法打开Lolesports网页,网络问题,将于3秒后退出..."))
# Switch web language to English
try:
wait = WebDriverWait(driver, 30)
languageButton = wait.until(ec.presence_of_element_located(
(By.CSS_SELECTOR, "[data-testid='riotbar:localeswitcher:button-toggleLocaleMenu']")))
languageButton.click()
enUSButton = wait.until(ec.presence_of_element_located(
(By.CSS_SELECTOR, "[data-testid='riotbar:localeswitcher:dropdown'] > li:nth-child(1) > a")))
enUSButton.click()
log.info(_log("切换网页语言成功"))
except Exception:
debugScreen(driver, "language")
log.error(_log("切换网页语言失败"))
stats.info.append(f"{datetime.now().strftime('%H:%M:%S')} "
f"{_('切换网页语言失败', color='red')}")
log.error(formatExc(format_exc()))
sysQuit(driver, _log("切换网页语言失败"))
def login(locks):
"""
The login function, which logs in with the given configuration information and outputs the login result.
"""
loginHandler = LoginHandler(driver=driver, locks=locks)
if config.userDataDir == "":
tryLoginTimes = 4
while not driver.find_elements(by=By.CSS_SELECTOR, value="div.riotbar-summoner-name") and tryLoginTimes > 0:
try:
if loginHandler.automaticLogIn(config.username, config.password):
pass
else:
tryLoginTimes = tryLoginTimes - 1
if tryLoginTimes <= 0:
stats.info.append(f"{datetime.now().strftime('%H:%M:%S')} "
f"{_('无法登录,账号密码可能错误或者网络出现问题', color='red')}")
stats.status = _("离线", color="red")
sysQuit(driver, _log("无法登录,账号密码可能错误或者网络出现问题"))
else:
log.error(_log("5秒后开始重试"))
stats.status = _("5秒后开始重试", color="red")
sleep(5)
except Exception:
debugScreen(driver, "login")
log.error(formatExc(format_exc()))
stats.info.append(f"{datetime.now().strftime('%H:%M:%S')} {_('出现异常,登录失败', color='red')}")
sysQuit(driver, _log("出现异常,登录失败"))
stats.status = _("在线", color="bold green")
log.info(_log("好嘞 登录成功"))
stats.info.append(f"{datetime.now().strftime('%H:%M:%S')} {_('好嘞 登录成功', color='green')}")
else:
loginHandler.userDataLogin()
log.info(_log("使用浏览器缓存 自动登录成功"))
stats.info.append(f"{datetime.now().strftime('%H:%M:%S')} {_('使用浏览器缓存 自动登录成功', color='green')}")
stats.status = _("在线", color="bold green")
def watch():
Match(driver=driver).watchMatches()
def main():
"""
Main function to run the EsportsHelper program.
Parses command line arguments, initializes the configuration and utility objects,
initializes the webdriver, logs in, watches the match and prints the completion message.
"""
global driver
# Print the banner information
info()
openDatetime = datetime.now()
formattedOpenDatetime = openDatetime.strftime("%Y-%m-%d %H:%M:%S")
stats.banner.append(f"{_('开始时间: ', color='green')}" + formattedOpenDatetime)
initWebdriver()
sleep(3)
refreshLock = Lock()
locks = {"refreshLock": refreshLock}
guiThread = GUIThread(locks)
guiThread.daemon = True
guiThread.start()
switchLanguage()
acceptCookies(driver=driver)
login(locks)
sleep(1)
watch()
stats.info.append(_("观看结束", color="green"))
log.info(_log("观看结束"))
if __name__ == '__main__':
try:
main()
except (KeyboardInterrupt, SystemExit):
sysQuit(driver, _log("程序被终止"))
except Exception:
sysQuit(driver, format_exc())