-
Notifications
You must be signed in to change notification settings - Fork 23
Sourcery Starbot ⭐ refactored yanyongyu/CXmoocSearchTool #5
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,40 +33,33 @@ async def cxmooc_tool(sess: requests.Session, | |
|
||
# 接口参数 | ||
index = yield | ||
data = {} | ||
for i in range(len(args)): | ||
data['topic[%d]' % i] = args[i] | ||
|
||
data = {'topic[%d]' % i: args[i] for i in range(len(args))} | ||
# post请求 | ||
logging.info("Post to cxmooc_tool api.") | ||
try: | ||
res = sess.post(url, data=data, verify=False, timeout=5) | ||
res.raise_for_status() | ||
except requests.exceptions.RequestException as e: | ||
logging.info("Request Exception appeared: %s" % e) | ||
logging.info(f"Request Exception appeared: {e}") | ||
answer = [{'topic': str(e), 'correct': ''}] | ||
for each in args: | ||
answer = [] | ||
answer.append({'topic': str(e), | ||
'correct': ''}) | ||
yield answer | ||
raise StopIteration | ||
|
||
# 处理结果 | ||
logging.info("Processing result") | ||
result = [[] for i in range(len(args))] | ||
result = [[] for _ in range(len(args))] | ||
for each in res.json(): | ||
for answ in each['result']: | ||
temp = {} | ||
temp['topic'] = answ['topic'] | ||
temp['correct'] = '' | ||
temp = {'topic': answ['topic'], 'correct': ''} | ||
for option in answ['correct']: | ||
temp['correct'] = temp['correct'] + str(option['option']) | ||
temp['correct'] += str(option['option']) | ||
result[each['index']].append(temp) | ||
|
||
for i in range(len(result)): | ||
if index and i < index: | ||
continue | ||
logging.info("Yield question %s: %s" % (i+1, result[i])) | ||
logging.info(f"Yield question {i + 1}: {result[i]}") | ||
index = yield result[i] | ||
raise StopIteration | ||
|
||
|
@@ -99,23 +92,19 @@ async def forestpolice(sess: requests.Session, | |
res = sess.post(url + args[i], data=data, verify=False, timeout=5) | ||
res.raise_for_status() | ||
except requests.exceptions.RequestException as e: | ||
logging.info("Request Exception appeared: %s" % e) | ||
answer = [] | ||
answer.append({'topic': str(e), | ||
'correct': ''}) | ||
logging.info(f"Request Exception appeared: {e}") | ||
answer = [{'topic': str(e), 'correct': ''}] | ||
index = yield answer | ||
continue | ||
|
||
# 处理结果 | ||
logging.info("Processing result") | ||
answer = [] | ||
temp = {} | ||
temp['topic'] = args[i] | ||
temp['correct'] = res.json()['data'] | ||
temp = {'topic': args[i], 'correct': res.json()['data']} | ||
if temp['correct'] != '未找到答案': | ||
answer.append(temp) | ||
|
||
logging.info("Yield question %s: %s" % (i+1, answer)) | ||
logging.info(f"Yield question {i + 1}: {answer}") | ||
Comment on lines
-102
to
+107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
index = yield answer | ||
|
||
await asyncio.sleep(0.5) | ||
|
@@ -150,21 +139,20 @@ async def jiuaidaikan(sess: requests.Session, | |
eventvalidation = selector.xpath( | ||
'//*[@id="__EVENTVALIDATION"]/@value') | ||
except requests.exceptions.RequestException as e: | ||
logging.info("Request Exception appeared: %s" % e) | ||
logging.info(f"Request Exception appeared: {e}") | ||
index = yield | ||
for i in range(len(args)): | ||
if index and i < index: | ||
continue | ||
answer = [] | ||
answer.append({'topic': str(e), | ||
'correct': ''}) | ||
yield answer | ||
yield [{'topic': str(e), 'correct': ''}] | ||
raise StopIteration | ||
|
||
data = {} | ||
data['__VIEWSTATE'] = viewstate | ||
data['__EVENTVALIDATION'] = eventvalidation | ||
data['ctl00$ContentPlaceHolder1$gen'] = '查询' | ||
data = { | ||
'__VIEWSTATE': viewstate, | ||
'__EVENTVALIDATION': eventvalidation, | ||
'ctl00$ContentPlaceHolder1$gen': '查询', | ||
} | ||
|
||
Comment on lines
-153
to
+155
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
for i in range(len(args)): | ||
if index and i < index: | ||
continue | ||
|
@@ -176,24 +164,24 @@ async def jiuaidaikan(sess: requests.Session, | |
res = sess.post(url, data=data, verify=False, timeout=5) | ||
res.raise_for_status() | ||
except requests.exceptions.RequestException as e: | ||
logging.info("Request Exception appeared: %s" % e) | ||
answer = [] | ||
answer.append({'topic': str(e), | ||
'correct': ''}) | ||
logging.info(f"Request Exception appeared: {e}") | ||
answer = [{'topic': str(e), 'correct': ''}] | ||
index = yield answer | ||
continue | ||
|
||
# 处理结果 | ||
logging.info("Processing result") | ||
answer = [] | ||
selector = etree.HTML(res.text) | ||
temp = {} | ||
temp['topic'] = args[i] | ||
temp['correct'] = selector.xpath('//*[@id="daan"]/text()')[0] | ||
temp = { | ||
'topic': args[i], | ||
'correct': selector.xpath('//*[@id="daan"]/text()')[0], | ||
} | ||
|
||
if temp['correct'] != '未找到答案': | ||
answer.append(temp) | ||
|
||
logging.info("Yield question %s: %s" % (i+1, answer)) | ||
logging.info(f"Yield question {i + 1}: {answer}") | ||
index = yield answer | ||
|
||
await asyncio.sleep(0.5) | ||
|
@@ -212,9 +200,7 @@ async def xuanxiu365(sess: requests.Session, | |
|
||
# 接口 | ||
url = "http://tiku.xuanxiu365.com/admin/admin/api.html" | ||
header = {} | ||
header['X-Requested-With'] = "XMLHttpRequest" | ||
|
||
header = {'X-Requested-With': "XMLHttpRequest"} | ||
Comment on lines
-215
to
+203
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
# 接口参数 | ||
index = yield | ||
payload = {} | ||
|
@@ -230,10 +216,8 @@ async def xuanxiu365(sess: requests.Session, | |
verify=False, timeout=5) | ||
res.raise_for_status() | ||
except requests.exceptions.RequestException as e: | ||
logging.info("Request Exception appeared: %s" % e) | ||
answer = [] | ||
answer.append({'topic': str(e), | ||
'correct': ''}) | ||
logging.info(f"Request Exception appeared: {e}") | ||
answer = [{'topic': str(e), 'correct': ''}] | ||
index = yield answer | ||
continue | ||
|
||
|
@@ -242,11 +226,9 @@ async def xuanxiu365(sess: requests.Session, | |
res = res.json() | ||
answer = [] | ||
if res['data']: | ||
temp = {} | ||
temp['topic'] = res['data']['title'] | ||
temp['correct'] = res['data']['content'] | ||
temp = {'topic': res['data']['title'], 'correct': res['data']['content']} | ||
answer.append(temp) | ||
logging.info("Yield question %s: %s" % (i+1, answer)) | ||
logging.info(f"Yield question {i + 1}: {answer}") | ||
index = yield answer | ||
|
||
await asyncio.sleep(0.5) | ||
|
@@ -284,10 +266,8 @@ async def www150s(sess: requests.Session, | |
verify=False, timeout=5) | ||
res.raise_for_status() | ||
except requests.exceptions.RequestException as e: | ||
logging.info("Request Exception appeared: %s" % e) | ||
answer = [] | ||
answer.append({'topic': str(e), | ||
'correct': ''}) | ||
logging.info(f"Request Exception appeared: {e}") | ||
answer = [{'topic': str(e), 'correct': ''}] | ||
Comment on lines
-287
to
+270
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
index = yield answer | ||
continue | ||
|
||
|
@@ -302,7 +282,7 @@ async def www150s(sess: requests.Session, | |
if temp['topic'] == "查无此题,请您换一道题查询!": | ||
break | ||
answer.append(temp) | ||
logging.info("Yield question %s: %s" % (i+1, answer)) | ||
logging.info(f"Yield question {i + 1}: {answer}") | ||
index = yield answer | ||
|
||
await asyncio.sleep(0.5) | ||
|
@@ -336,11 +316,9 @@ async def wangkebang(sess: requests.Session, | |
res = sess.post(url, data=data, verify=False, timeout=5) | ||
res.raise_for_status() | ||
except requests.exceptions.RequestException as e: | ||
logging.info("Request Exception appeared: %s" % e) | ||
logging.info(f"Request Exception appeared: {e}") | ||
for each in args: | ||
answer = [] | ||
answer.append({'topic': str(e), | ||
'correct': ''}) | ||
answer = [{'topic': str(e), 'correct': ''}] | ||
Comment on lines
-339
to
+321
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
yield answer | ||
raise StopIteration | ||
|
||
|
@@ -349,17 +327,19 @@ async def wangkebang(sess: requests.Session, | |
res.encoding = 'utf-8' | ||
answer = [] | ||
selector = etree.HTML(res.text) | ||
temp = {} | ||
temp['topic'] = selector.xpath( | ||
'/html/body/div[3]/div[1]/div/span[1]/strong/text()' | ||
)[0].lstrip(" 题目:\n") | ||
temp = { | ||
'topic': selector.xpath( | ||
'/html/body/div[3]/div[1]/div/span[1]/strong/text()' | ||
)[0].lstrip(" 题目:\n") | ||
} | ||
|
||
temp['correct'] = selector.xpath( | ||
'/html/body/div[3]/div[1]/div/span[2]/strong/text()' | ||
)[0].lstrip(" 答案:\n") | ||
if temp['topic'] != '啊哦暂无该题': | ||
answer.append(temp) | ||
|
||
logging.info("Yield question %s: %s" % (i+1, answer)) | ||
logging.info(f"Yield question {i + 1}: {answer}") | ||
index = yield answer | ||
|
||
await asyncio.sleep(0.5) | ||
|
@@ -597,21 +577,19 @@ async def wangkebang(sess: requests.Session, | |
async def cmd(): | ||
# 获取所有api | ||
api_list = {} | ||
for each in globals().keys(): | ||
for each, fn in globals().items(): | ||
if each.startswith('_'): | ||
continue | ||
fn = globals()[each] | ||
if callable(fn): | ||
if getattr(fn, '__annotations__', None): | ||
api_list[each] = fn | ||
if callable(fn) and getattr(fn, '__annotations__', None): | ||
api_list[each] = fn | ||
Comment on lines
-600
to
+584
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
args = sys.argv[1:] | ||
if not args or "-h" in args: | ||
print("超星查题助手\n\tpython api.py [-json] [-api=] -text=\nusage:") | ||
print("\t-h\tPrint help") | ||
print("\t-json\tReturn json data at last") | ||
print("\t-api\tUsing the specified api\n\t\tapi list:") | ||
for each in api_list.keys(): | ||
for each in api_list: | ||
print("\t\t\t%s" % each) | ||
print("\t-text\tquestion(-text can be used more than one time)") | ||
else: | ||
|
@@ -632,15 +610,15 @@ async def cmd(): | |
elif each.startswith("-text="): | ||
text.append(each[6:]) | ||
else: | ||
ValueError("Unknow option %s. Use -h for help" | ||
% each.split("=")[0]) | ||
ValueError(f'Unknow option {each.split("=")[0]}. Use -h for help') | ||
|
||
# 获取答案 | ||
answer = [[] for i in range(len(text))] | ||
answer = [[] for _ in range(len(text))] | ||
if not search: | ||
for each in api_list.keys(): | ||
remain = [text[i] for i in range(len(text)) if not answer[i]] | ||
if remain: | ||
for each in api_list: | ||
if remain := [ | ||
text[i] for i in range(len(text)) if not answer[i] | ||
]: | ||
generator = api_list[each](*remain) | ||
await generator.asend(None) | ||
for i in range(len(text)): | ||
|
@@ -649,16 +627,13 @@ async def cmd(): | |
continue | ||
else: | ||
answer[i] = result | ||
else: | ||
if text: | ||
generator = search(*text) | ||
await generator.asend(None) | ||
for i in range(len(text)): | ||
result = await generator.asend(None) | ||
if not result or not result[0]['correct']: | ||
continue | ||
else: | ||
answer[i] = result | ||
elif text: | ||
generator = search(*text) | ||
await generator.asend(None) | ||
for i in range(len(text)): | ||
result = await generator.asend(None) | ||
if result and result[0]['correct']: | ||
answer[i] = result | ||
|
||
if not JSON: | ||
print(answer) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -277,7 +277,7 @@ def _configure_frame(event): | |
if len(self.text) <= 3: | ||
self.root.geometry('%dx%d' % (600, 35 + 70*len(self.text))) | ||
canvas.config(height=frame_in.winfo_reqheight()) | ||
elif len(self.text) > 3: | ||
else: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
self.root.geometry('%dx%d' % (600, 35 + 70*3)) | ||
canvas.config(height=210) | ||
|
||
|
@@ -439,9 +439,11 @@ def scan_cx(self): | |
|
||
print(question) | ||
|
||
raw_question = [each[each.index("】") + 1:] | ||
for each in question if each.find("】") != -1] | ||
return raw_question | ||
return [ | ||
each[each.index("】") + 1 :] | ||
for each in question | ||
if each.find("】") != -1 | ||
] | ||
Comment on lines
-442
to
+446
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def scan_zhs(self): | ||
clipboard_text = self.root.clipboard_get().strip() | ||
|
@@ -454,11 +456,12 @@ def scan_zhs(self): | |
|
||
question = html or zhs_text | ||
|
||
raw_question = [question[i + 1] | ||
for i in range(len(question) - 1) | ||
if question[i].find("】") != -1 | ||
if question[i].find(r")") != -1] | ||
return raw_question | ||
return [ | ||
question[i + 1] | ||
for i in range(len(question) - 1) | ||
if question[i].find("】") != -1 | ||
if question[i].find(r")") != -1 | ||
] | ||
Comment on lines
-457
to
+464
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def start_search(self): | ||
"开始搜索,显示答案窗口" | ||
|
@@ -532,14 +535,14 @@ async def search(self, frame_list): | |
if self.api_on[api].get(): | ||
generator_list[api] = self.api_list[api](self.sess, *text) | ||
# 启动迭代器 | ||
logging.info("Active generator %s" % api) | ||
logging.info(f"Active generator {api}") | ||
await generator_list[api].asend(None) | ||
|
||
# 查询答案 | ||
for i in range(len(text)): | ||
label = frame_list[i].children['!text'] | ||
label.configure(state="normal") | ||
for generator in generator_list.keys(): | ||
for generator in generator_list: | ||
Comment on lines
-535
to
+545
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
label.insert(END, f"查询中。。。使用源{generator}\n") | ||
label.configure(state="disable") | ||
try: | ||
|
@@ -661,7 +664,7 @@ async def scan_release(self, silence): | |
res = self.sess.get(URL) | ||
res.raise_for_status() | ||
except requests.exceptions.RequestException as e: | ||
logging.info("Request Exception appeared: %s" % e) | ||
logging.info(f"Request Exception appeared: {e}") | ||
Comment on lines
-664
to
+667
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
showinfo(title="查题助手", | ||
message="检查更新失败了!") | ||
else: | ||
|
@@ -679,9 +682,9 @@ async def scan_release(self, silence): | |
now.append('0') | ||
for i in range(len(now)): | ||
if latest[i] > now[i]: | ||
if askyesno(title="查题助手", | ||
message="发现新版本%s!是否前去更新?" | ||
% info['tag_name']): | ||
if askyesno( | ||
title="查题助手", message=f"发现新版本{info['tag_name']}!是否前去更新?" | ||
): | ||
webbrowser.open(info['html_url']) | ||
if info['assets'][0]['name'].endswith('.exe'): | ||
webbrowser.open( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
cxmooc_tool
refactored with the following changes:dict-comprehension
)replace-interpolation-with-fstring
)hoist-statement-from-loop
)merge-list-append
)for-index-underscore
)merge-dict-assign
)aug-assign
)