-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
242 lines (188 loc) · 7.83 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# -*- coding: utf-8 -*-
from Tkinter import *
import tkMessageBox
from threading import Thread
from common import common
from parser import parser as htmlparse
from crawler import gethtml
from worker import Worker, WorkerConfig
from sentiment_analysis import inference
from simple_crawler import SpiderApplication
AUTH_KEY = 'abc'
class LoginView(Frame):
def __init__(self, app, master=None,):
Frame.__init__(self, master, width=300, height=200)
self.app = app
self.pack()
self.center_view = Frame(self, width=100, height=100)
self.left_frame = Frame(self, width=130, height=100)
self.right_frame = Frame(self, width=140, height=100)
self.server_label = Label(self.left_frame, text="服务器:",)
self.port = Label(self.left_frame, text="端口:",)
self.server_entry = Entry(self.right_frame, width=14)
self.port_entry = Entry(self.right_frame, width=14)
self.distribute_button = Button(self.center_view, text="分布式", command=self.distribute, height=2, width=13)
self.single_button = Button(self.center_view, text="单机", command=self.single, height=2, width=13)
self.left_frame.propagate(0)
self.right_frame.propagate(0)
self.left_frame.grid(padx=2, pady=2, row=0, column=0)
self.right_frame.grid(padx=0, pady=2,row=0, column=1)
self.center_view.grid(padx=50, row=1, columnspan=2)
self.server_label.grid(row=0)
self.server_entry.grid(row=0)
self.port.grid(row=1, pady=7)
self.port_entry.grid(row=1, pady=7)
self.distribute_button.grid(row=0, pady=4)
self.single_button.grid(row=1, pady=0)
def distribute(self):
if not isinstance(self.app, Application):
exit(-1)
server_ip = self.server_entry.get()
port = self.port_entry.get()
self.app.login(True, server_ip, int(port))
def single(self):
self.app.login(False)
class MainView(Frame):
def __init__(self, app, master=None):
Frame.__init__(self, master, width=900, height=500, bg='gray')
self.app = app
self.pack()
self.sb = Scrollbar(self, orient=VERTICAL, width=5, bg='white')
self.logger = Text(self, width=62)
self.logger.config(yscrollcommand=self.sb.set)
self.logger.tag_config('green', foreground='#008B00')
self.logger.tag_config('red', foreground='#E74C3C')
self.logger.tag_config('orange', foreground='#F1C40F')
self.sb.config(command=self.logger.yview)
self.sb.pack(side=RIGHT, fill=Y)
self.counter_text = Text(self, width=60, height=20)
self.input_text = Text(self, width=60, height=10)
self.button = Button(self, text='情感分析', command=self.sentiment_button)
self.logger.pack(side=LEFT, fill=BOTH)
self.counter_text.pack(padx=5)
self.input_text.pack(padx=5, pady=5)
self.button.pack(fill=X)
self.logger.configure(state='disabled')
self.counter_text.configure(state='disabled')
def write_log(self, log_type, message):
self.logger.configure(state='normal')
if log_type == 'warn':
color = 'orange'
elif log_type == 'info':
color = 'green'
else:
color = 'red'
self.logger.insert(END, message + '\n', color)
self.logger.see(END)
self.logger.configure(state='disabled')
def update_info(self, info):
"""
info: {user:n, question:m, topic:q, answers:p}
:param info:
:return:
"""
self.counter_text.configure(state='normal')
self.counter_text.delete(0.0, END)
self.counter_text.insert(END, '爬虫状态:\n', 'green')
self.counter_text.insert(END, '用户数:%s\n' % info['user'], 'black')
self.counter_text.insert(END, '问题数:%s\n' % info['question'], 'black')
self.counter_text.insert(END, '话题数:%s\n' % info['topic'], 'black')
self.counter_text.configure(state='disabled')
def sentiment_button(self):
content = self.input_text.get("1.0", END)
senti, prob = inference.inference(content)
if senti:
tkMessageBox.showinfo('情感分析', '情感分析结果:正面,置信度:%s' % prob, parent=self.app.root)
else:
if prob != 0:
tkMessageBox.showinfo('情感分析', '情感分析结果:负面,置信度:%s' % prob, parent=self.app.root)
else:
tkMessageBox.showinfo('情感分析', '情感分析结果:中性', parent=self.app.root)
self.input_text.delete(0.0, END)
def crawl_func(s):
return gethtml.get_html(s)
def parse_func(content_type, content):
return htmlparse.parse_html(content_type, content)
class Logger(object):
def __init__(self, handle):
self.handle = handle
def get_timestr(self):
timestamp = common.get_timestamp()
date_str = common.time_to_str(timestamp)
return date_str
def warn(self, message):
self.handle.write_log('warn', self.get_timestr()+'[WARN]'+message)
def info(self, message):
self.handle.write_log('info', self.get_timestr()+'[INFO]'+message)
def error(self, message):
self.handle.write_log('error', self.get_timestr()+'[ERROR]'+message)
class Application(object):
def __init__(self):
self.root = Tk()
self.root.wm_attributes('-topmost', 1)
self.current_view = None
self.worker = None
self.logger = None
self.worker_thread = None
self.state_dict = dict()
self.state_dict['user'] = 0
self.state_dict['question'] = 0
self.state_dict['topic'] = 0
self.root.protocol('WM_DELETE_WINDOW', self.on_closing)
def _resize_window(self, width, height):
screenwidth = self.root.winfo_screenwidth()
screenheight = self.root.winfo_screenheight()
size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
self.root.geometry(size)
def start(self):
self.current_view = LoginView(self, self.root)
self._resize_window(300, 200)
self.root.mainloop()
def update_func(self, content_type):
if content_type == 'people':
self.state_dict['user'] += 1
elif content_type == 'question':
self.state_dict['question'] += 1
else:
self.state_dict['topic'] += 1
self.current_view.update_info(self.state_dict)
def login(self, is_distributed, ip="", port=0):
self.current_view.destroy()
self._resize_window(900, 500)
self.current_view = MainView(self)
self.logger = Logger(self.current_view)
if is_distributed:
config = WorkerConfig(
name='worker',
task_batchsize=2,
crawler_threads=8,
parser_threads=2,
authkey=AUTH_KEY,
address=(ip, port)
)
self.worker = Worker(
config=config,
crawler_func=crawl_func,
parser_func=parse_func,
logger=self.logger,
update_callback=self.update_func
)
else:
self.worker = SpiderApplication(
parser=parse_func,
cralwer=crawl_func,
logger=self.logger,
update_callback=self.update_func
)
# self.root.mainloop()
self.worker_thread = Thread(target=self.worker.run)
self.worker_thread.daemon = True
self.worker_thread.start()
def on_closing(self):
if tkMessageBox.askyesno("Quit", "Do you want to quit?", parent=self.root):
self.worker.exit_app()
self.root.destroy()
exit(0)
if __name__ == '__main__':
app = Application()
app.start()