Skip to content

Commit 7dc21a6

Browse files
committed
百万英雄答题辅助系统
1 parent ff5661f commit 7dc21a6

File tree

5 files changed

+462
-0
lines changed

5 files changed

+462
-0
lines changed

baiwan/app.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
var http = require('http');
2+
var fs = require('fs');
3+
var schedule = require("node-schedule");
4+
var message = {};
5+
var count = 0;
6+
var server = http.createServer(function (req,res){
7+
fs.readFile('./index.html',function(error,data){
8+
res.writeHead(200,{'Content-Type':'text/html'});
9+
res.end(data,'utf-8');
10+
});
11+
}).listen(80);
12+
console.log('Server running!');
13+
var lineReader = require('line-reader');
14+
function messageGet(){
15+
lineReader.eachLine('file.txt', function(line, last) {
16+
count++;
17+
var name = 'line' + count;
18+
console.log(name);
19+
console.log(line);
20+
message[name] = line;
21+
});
22+
if(count == 25){
23+
count = 0;
24+
}
25+
else{
26+
for(var i = count+1; i <= 25; i++){
27+
var name = 'line' + i;
28+
message[name] = 'f';
29+
}
30+
count = 0;
31+
}
32+
}
33+
var io = require('socket.io').listen(server);
34+
var rule = new schedule.RecurrenceRule();
35+
var times = [];
36+
for(var i=1; i<1800; i++){
37+
times.push(i);
38+
}
39+
rule.second = times;
40+
schedule.scheduleJob(rule, function(){
41+
messageGet();
42+
});
43+
io.sockets.on('connection',function(socket){
44+
// console.log('User connected' + count + 'user(s) present');
45+
socket.emit('users',message);
46+
socket.broadcast.emit('users',message);
47+
48+
socket.on('disconnect',function(){
49+
console.log('User disconnected');
50+
//socket.broadcast.emit('users',message);
51+
});
52+
});

baiwan/baiwan.py

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
# -*-coding:utf-8 -*-
2+
import requests
3+
from lxml import etree
4+
from bs4 import BeautifulSoup
5+
import urllib
6+
import time, re, types, os
7+
8+
9+
"""
10+
代码写的匆忙,本来想再重构下,完善好注释再发,但是比较忙,想想算了,所以自行完善吧!
11+
12+
作者: Jack Cui
13+
Website:http://cuijiahua.com
14+
注: 本软件仅用于学习交流,请勿用于任何商业用途!
15+
"""
16+
17+
class BaiWan():
18+
def __init__(self):
19+
# 百度知道搜索接口
20+
self.baidu = 'http://zhidao.baidu.com/search?'
21+
# 百万英雄及接口,每个人的接口都不一样,里面包含的手机信息,因此不公布,请自行抓包,有疑问欢迎留言:http://cuijiahua.com/liuyan.html
22+
self.api = 'https://api-spe-ttl.ixigua.com/xxxxxxx={}'.format(int(time.time()*1000))
23+
24+
# 获取答案并解析问题
25+
def get_question(self):
26+
to = True
27+
while to:
28+
list_dir = os.listdir('./')
29+
if 'question.txt' not in list_dir:
30+
fw = open('question.txt', 'w')
31+
fw.write('百万英雄尚未出题请稍后!')
32+
fw.close()
33+
go = True
34+
while go:
35+
req = requests.get(self.api, verify=False)
36+
req.encoding = 'utf-8'
37+
html = req.text
38+
39+
print(html)
40+
if '*' in html:
41+
question_start = html.index('*')
42+
try:
43+
44+
question_end = html.index('?')
45+
except:
46+
question_end = html.index('?')
47+
question = html[question_start:question_end][2:]
48+
if question != None:
49+
fr = open('question.txt', 'r')
50+
text = fr.readline()
51+
fr.close()
52+
if text != question:
53+
print(question)
54+
go = False
55+
with open('question.txt', 'w') as f:
56+
f.write(question)
57+
else:
58+
time.sleep(1)
59+
else:
60+
to = False
61+
else:
62+
to = False
63+
64+
temp = re.findall(r'[\u4e00-\u9fa5a-zA-Z0-9\+\-\*/]', html[question_end+1:])
65+
b_index = []
66+
print(temp)
67+
68+
for index, each in enumerate(temp):
69+
if each == 'B':
70+
b_index.append(index)
71+
elif each == 'P' and (len(temp) - index) <= 3 :
72+
b_index.append(index)
73+
break
74+
75+
if len(b_index) == 4:
76+
a = ''.join(temp[b_index[0] + 1:b_index[1]])
77+
b = ''.join(temp[b_index[1] + 1:b_index[2]])
78+
c = ''.join(temp[b_index[2] + 1:b_index[3]])
79+
alternative_answers = [a,b,c]
80+
81+
if '下列' in question:
82+
question = a + ' ' + b + ' ' + c + ' ' + question.replace('下列', '')
83+
elif '以下' in question:
84+
question = a + ' ' + b + ' ' + c + ' ' + question.replace('以下', '')
85+
else:
86+
alternative_answers = []
87+
# 根据问题和备选答案搜索答案
88+
self.search(question, alternative_answers)
89+
time.sleep(1)
90+
91+
def search(self, question, alternative_answers):
92+
print(question)
93+
print(alternative_answers)
94+
infos = {"word":question}
95+
# 调用百度接口
96+
url = self.baidu + 'lm=0&rn=10&pn=0&fr=search&ie=gbk&' + urllib.parse.urlencode(infos, encoding='GB2312')
97+
print(url)
98+
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36',
99+
}
100+
sess = requests.Session()
101+
req = sess.get(url = url, headers=headers, verify=False)
102+
req.encoding = 'gbk'
103+
# print(req.text)
104+
bf = BeautifulSoup(req.text, 'lxml')
105+
answers = bf.find_all('dd',class_='dd answer')
106+
for answer in answers:
107+
print(answer.text)
108+
109+
# 推荐答案
110+
recommend = ''
111+
if alternative_answers != []:
112+
best = []
113+
print('\n')
114+
for answer in answers:
115+
# print(answer.text)
116+
for each_answer in alternative_answers:
117+
if each_answer in answer.text:
118+
best.append(each_answer)
119+
print(each_answer,end=' ')
120+
# print(answer.text)
121+
print('\n')
122+
break
123+
statistics = {}
124+
for each in best:
125+
if each not in statistics.keys():
126+
statistics[each] = 1
127+
else:
128+
statistics[each] += 1
129+
errors = ['没有', '不是', '不对', '不正确','错误','不包括','不包含','不在','错']
130+
error_list = list(map(lambda x: x in question, errors))
131+
print(error_list)
132+
if sum(error_list) >= 1:
133+
for each_answer in alternative_answers:
134+
if each_answer not in statistics.items():
135+
recommend = each_answer
136+
print('推荐答案:', recommend)
137+
break
138+
elif statistics != {}:
139+
recommend = sorted(statistics.items(), key=lambda e:e[1], reverse=True)[0][0]
140+
print('推荐答案:', recommend)
141+
142+
# 写入文件
143+
with open('file.txt', 'w') as f:
144+
f.write('问题:' + question)
145+
f.write('\n')
146+
f.write('*' * 50)
147+
f.write('\n')
148+
if alternative_answers != []:
149+
f.write('选项:')
150+
for i in range(len(alternative_answers)):
151+
f.write(alternative_answers[i])
152+
f.write(' ')
153+
f.write('\n')
154+
f.write('*' * 50)
155+
f.write('\n')
156+
f.write('参考答案:\n')
157+
for answer in answers:
158+
f.write(answer.text)
159+
f.write('\n')
160+
f.write('*' * 50)
161+
f.write('\n')
162+
if recommend != '':
163+
f.write('最终答案请自行斟酌!\t')
164+
f.write('推荐答案:' + sorted(statistics.items(), key=lambda e:e[1], reverse=True)[0][0])
165+
166+
167+
if __name__ == '__main__':
168+
bw = BaiWan()
169+
bw.get_question()

baiwan/file.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
���⣺�������Ǽ��¼���
2+
**************************************************
3+
ѡ�7��23�� 8��1�� 10��1��
4+
**************************************************
5+
�ο��𰸣�
6+
7+
�Ƽ���
8+
��������8��1�� ÿ��İ���һ�����й������ž����������գ����Ҳ�С���һ�������ڡ� August 1, anniversary of the founding of the Chinese People's Liberation Army��֪�������Ⱦ�������뵽���http://baike.baidu.com/view/23211.htm
9+
[��ϸ]
10+
11+
����ã��й������ž��Ľ�������ÿ��İ���һ�գ���ư�һ�����ڣ�����İ�һ�պ�����������
12+
�𣺽�������8��1�գ���������7��1�ա� ÿ���8��1�����й������ž����������գ��׳ơ���һ�������ڡ�1927��8��1�գ��й����������챱�����������ˣ����ܶ��� ������ ��Ҷͦ ����� �������е��쵼�£��ڽ����ϲ�������װ���壬���췴�Թ��񵳷�����...
13+
����7��30��
14+
�𣺰���һ���ǽ����ڣ�ǰһ�첻��7��31��ô
15+
��1927��8��1������һ���ϲ�����,�������й���������װ�������񵳷����ɵĵ�һǹ,��־���й��������й������������쵼��װ��������ʱ��,��־���й����͵�������ӵĵ�����ÿ��İ���һ�����й������ž�����������
16+
������Դ���й����������������ʱ���й����������쵼���ϲ����塣1927��8��1�յ��ϲ����壬�������й���������װ�������񵳷����ɵĵ�һǹ����־���й��������й������������쵼��װ��������ʱ�ڣ���־���й����͵�������ӵĵ����� 1933��7�£�...
17+
��Ԫ��1��1��������8��1�������10��1��
18+
�𣺰���һ�Ž�����
19+
�������� 2015��8��1�� ũ�� ����ʮ�� ������ 2016��8��1�� ũ�� ����إ�� ������ ÿ��İ���һ�����й������ž����������գ����Ҳ�С���һ�������ڡ�1933��7��11�գ��л���ά�����͹���ʱ�����������������������ίԱ��6��30�յĽ��飬����8��1...
20+
**************************************************
21+
���մ����������ã� �Ƽ��𰸣�8��1��

0 commit comments

Comments
 (0)