forked from smileboywtu/MillionHeroAssistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
196 lines (161 loc) · 5.24 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
# -*- coding:utf-8 -*-
"""
Xi Gua video Million Heroes
"""
import multiprocessing
from multiprocessing import Event
from multiprocessing import Pipe
import time
from argparse import ArgumentParser
from multiprocessing import Value
import operator
from functools import partial
from terminaltables import AsciiTable
from config import api_key
from config import api_version
from config import app_id
from config import app_key
from config import app_secret
from config import data_directory
from config import enable_chrome
from config import image_compress_level
from config import prefer
from config import crop_areas
from core.android import analyze_current_screen_text
from core.android import save_screen
from core.baiduzhidao import baidu_count
from core.check_words import parse_false
from core.chrome_search import run_browser
from core.ocr.baiduocr import get_text_from_image as bai_get_text
from core.ocr.spaceocr import get_text_from_image as ocrspace_get_text
if prefer[0] == "baidu":
get_text_from_image = partial(bai_get_text,
app_id=app_id,
app_key=app_key,
app_secret=app_secret,
api_version=api_version,
timeout=5)
elif prefer[0] == "ocrspace":
get_test_from_image = partial(ocrspace_get_text, api_key=api_key)
def parse_args():
parser = ArgumentParser(description="Million Hero Assistant")
parser.add_argument(
"-t", "--timeout",
type=int,
default=5,
help="default http request timeout"
)
return parser.parse_args()
def parse_question_and_answer(text_list):
question = ""
start = 0
for i, keyword in enumerate(text_list):
question += keyword
if "?" in keyword:
start = i + 1
break
real_question = question.split(".")[-1]
question, true_flag = parse_false(real_question)
return true_flag, real_question, question, text_list[start:]
def pre_process_question(keyword):
"""
strip charactor and strip ?
:param question:
:return:
"""
for char, repl in [("“", ""), ("”", ""), ("?", "")]:
keyword = keyword.replace(char, repl)
keyword = keyword.split(r".")[-1]
keywords = keyword.split(" ")
keyword = "".join([e.strip("\r\n") for e in keywords if e])
return keyword
def main():
args = parse_args()
timeout = args.timeout
if enable_chrome:
closer = Event()
noticer = Event()
closer.clear()
noticer.clear()
reader, writer = Pipe()
browser_daemon = multiprocessing.Process(
target=run_browser, args=(closer, noticer, reader,))
browser_daemon.daemon = True
browser_daemon.start()
def __inner_job():
start = time.time()
text_binary = analyze_current_screen_text(
directory=data_directory,
compress_level=image_compress_level[0],
crop_area = crop_areas[game_type]
)
keywords = get_text_from_image(
image_data=text_binary,
)
if not keywords:
print("text not recognize")
return
true_flag, real_question, question, answers = parse_question_and_answer(keywords)
print('-' * 72)
print(real_question)
print('-' * 72)
print("\n".join(answers))
# notice browser
if enable_chrome:
writer.send(question)
noticer.set()
search_question = pre_process_question(question)
summary = baidu_count(search_question, answers, timeout=timeout)
summary_li = sorted(
summary.items(), key=operator.itemgetter(1), reverse=True)
data = [("选项", "同比")]
for a, w in summary_li:
data.append((a, w))
table = AsciiTable(data)
print(table.table)
print("*" * 72)
if true_flag:
print("肯定回答(**): ", summary_li[0][0])
print("否定回答( ): ", summary_li[-1][0])
else:
print("肯定回答( ): ", summary_li[0][0])
print("否定回答(**): ", summary_li[-1][0])
print("*" * 72)
end = time.time()
print("use {0} 秒".format(end - start))
save_screen(
directory=data_directory
)
print("""
请选择答题节目:
1. 百万英雄
2. 冲顶大会
""")
game_type = input("输入节目序号: ")
if game_type == "1":
game_type = '百万英雄'
elif game_type == "2":
game_type = '冲顶大会'
else:
game_type = '百万英雄'
while True:
print("""
请在答题开始前就运行程序,
答题开始的时候按Enter预测答案
""")
print("当前选择答题游戏: {}\n".format(game_type))
enter = input("按Enter键开始,按ESC键退出...")
if enter == chr(27):
break
try:
__inner_job()
except Exception as e:
print(str(e))
print("欢迎下次使用")
if enable_chrome:
reader.close()
writer.close()
closer.set()
time.sleep(3)
if __name__ == "__main__":
main()