コード例 #1
0
 def run(self):
     while True:
         # Get bytes
         conn, addr = self.server.accept()
         data = conn.recv(4096)
         try:
             word = data.decode('utf-8').strip()
         except:
             word = ''
         print('Get:' + str(len(data)) + ' bytes ' + word)
         try:
             word_info = None
             if word:
                 if is_alphabet(word[0]):
                     word_info = self.json_reader.get_word_info(word)
                 else:
                     word_info = self.json_reader.get_zh_word_info(word)
             if word_info is not None:
                 conn.sendall(word_info.encode('utf-8'))
                 print('Send: ' + str(len(word_info)) + ' bytes ')
             else:
                 conn.sendall('None'.encode('utf-8'))
         except KeyError:
             print('No words: ' + word)
         conn.close()
コード例 #2
0
 def run(self):
     while True:
         # Get bytes
         conn, addr = self.server.accept()
         data = conn.recv(256)
         word = data.decode('utf-8').strip()
         print('Get:' + str(len(data)) + ' bytes ' + word)
         # Shutdown
         if word == '---shutdown keyword---':
             self.server.close()
             print('Bye!~~~')
             sys.exit(0)
         # Get word
         try:
             word_info = None
             if word:
                 if is_alphabet(word[0]):
                     word_info = self.json_reader.get_word_info(word)
                 else:
                     word_info = self.json_reader.get_zh_word_info(word)
             if word_info is not None:
                 conn.sendall(word_info.encode('utf-8'))
                 print('Send: ' + str(len(word_info)) + ' bytes ')
             else:
                 conn.sendall('None'.encode('utf-8'))
         except KeyError:
             print('No words: ' + word)
         conn.close()
コード例 #3
0
ファイル: mainwindow.py プロジェクト: ChestnutHeng/Wudao-dict
 def search_bt_clicked(self):
     self.word = self.ui.lineEdit.text().strip()
     if self.word:
         # if chinese
         if is_alphabet(self.word[0]):
             self.is_zh = False
         else:
             self.is_zh = True
         # query on server
         server_context = self.client.get_word_info(self.word).strip()
         if server_context != 'None':
             wi = json.loads(server_context)
             self.painter.html = ''
             if self.is_zh:
                 self.painter.draw_zh_text(wi, self.draw_conf)
             else:
                 self.history_manager.add_item(self.word)
                 self.painter.draw_text(wi, self.draw_conf)
             self.ui.textBrowser.setHtml(self.painter.html)
         else:
             # Online search
             html = ''
             if self.ui.ol_cb.isChecked():
                 self.painter.html = ''
                 try:
                     self.ui.textBrowser.setHtml(self.painter.P_W_PATTERN % 'Searching OL...')
                     from src.WudaoOnline import get_text, get_zh_text
                     from urllib.error import URLError
                     if self.is_zh:
                         word_info = get_zh_text(self.word)
                     else:
                         word_info = get_text(self.word)
                     if not word_info['paraphrase']:
                         html += self.painter.P_W_PATTERN % ('No such word: %s found online' % self.word)
                     if not self.is_zh:
                         self.history_manager.add_word_info(word_info)
                         self.painter.draw_text(word_info, self.draw_conf)
                     else:
                         self.painter.draw_zh_text(word_info, self.draw_conf)
                     self.ui.textBrowser.setHtml(self.painter.html)
                     return
                 except ImportError:
                     html += self.painter.P_W_PATTERN % 'You need install bs4 first.'
                     html += self.painter.P_W_PATTERN % 'Use \'pip3 install bs4\' or get bs4 online.'
                 except URLError:
                     html += self.painter.P_W_PATTERN % 'No Internet : Please check your connection first'
             else:
                 # search in online cache first
                 word_info = self.history_manager.get_word_info(self.word)
                 if word_info:
                     self.history_manager.add_item(self.word)
                     self.painter.draw_text(word_info, self.draw_conf)
                     self.ui.textBrowser.setHtml(self.painter.html)
                     return
                 else:
                     html += self.painter.P_W_PATTERN % ('Error: no such word :' + self.word)
                     html += self.painter.P_W_PATTERN % 'You can check Online Box to search it online.'
             self.ui.textBrowser.setHtml(html)
コード例 #4
0
 def query(self, word, notename='notebook'):
     word_info = {}
     is_zh = False
     if word:
         if not is_alphabet(word[0]):
             is_zh = True
     # 1. query on server
     word_info = None
     server_context = self.client.get_word_info(word).strip()
     if server_context != 'None':
         word_info = json.loads(server_context)
     # 2. search in online cache first
     if not word_info:
         word_info = self.history_manager.get_word_info(word)
     # 3. online search
     if not word_info:
         try:
             # online search
             from src.WudaoOnline import get_text, get_zh_text
             from urllib.error import URLError
             import bs4
             import lxml
             if is_zh:
                 word_info = get_zh_text(word)
             else:
                 word_info = get_text(word)
             if not word_info['paraphrase']:
                 print('No such word: %s found online' %
                       (self.painter.RED_PATTERN % word))
                 return
             # store struct
             self.history_manager.add_word_info(word_info)
         except ImportError:
             print('Word not found, auto Online search...')
             print('You need install bs4, lxml first.')
             print('Use ' +
                   self.painter.RED_PATTERN % 'sudo pip3 install bs4 lxml' +
                   ' or get bs4 online.')
             return
         except URLError:
             print('Word not found, auto Online search...')
             print('No Internet : connection time out.')
             return
         except socket.error as socketerror:
             print("Error: ", socketerror)
             return
     # 4. save note
     if self.conf['save'] and not is_zh:
         self.history_manager.save_note(word_info, notename)
     # 5. draw
     if word_info:
         if is_zh:
             self.painter.draw_zh_text(word_info, self.conf)
         else:
             self.painter.draw_text(word_info, self.conf)
             self.history_manager.add_item(word_info)
     else:
         print('Word not exists.')
コード例 #5
0
ファイル: WudaoCommand.py プロジェクト: cywxf/Wudao-dict
 def param_separate(self):
     if len(sys.argv) == 1:
         self.param_list.append('h')
     else:
         for v in sys.argv[1:]:
             if v.startswith('-'):
                 self.param_list.append(v[1:])
             else:
                 self.word += ' ' + v
     self.word = self.word.strip()
     if self.word:
         if not is_alphabet(self.word[0]):
             self.is_zh = True
コード例 #6
0
 def param_separate(self):
     if len(sys.argv) == 1:
         self.param_list.append('h')
     else:
         for v in sys.argv[1:]:
             if v.startswith('-'):
                 self.param_list.append(v[1:])
             else:
                 self.word += ' ' + v
     self.word = self.word.strip()
     if self.word:
         if not is_alphabet(self.word[0]):
             self.is_zh = True
コード例 #7
0
 def run(self):
     while True:
         # Get bytes
         conn, addr = self.server.accept()
         data = conn.recv(256)
         word = data.decode('utf-8').strip()
         print('Get:' + str(len(data)) + ' bytes ' + word)
         # Shutdown
         if word == '---shutdown keyword---':
             self.server.close()
             print('Bye!~~~')
             sys.exit(0)
         # Get word
         try:
             word_info = None
             if word:
                 if is_alphabet(word[0]):
                     word_info = self.json_reader.get_word_info(word)
                 else:
                     word_info = self.json_reader.get_zh_word_info(word)
             if word_info is not None:
                 conn.sendall(word_info.encode('utf-8'))
                 print('Send: ' + str(len(word_info)) + ' bytes ')
             else:
                 conn.sendall('None'.encode('utf-8'))
         except KeyError:
             print('No words: ' + word)
         conn.close()
         # report
         try:
             if ie():
                 if word_info is None:
                     report_new_word(word, self.ip)
                     print('report new word')
                 else:
                     report_old_word(word, self.ip)
                     print('report old word')
             else:
                 print('no ie, report failed')
         except:
             print('exception occured, report failed')
コード例 #8
0
ファイル: mainwindow.py プロジェクト: zhouweitong3/Wudao-dict
 def search_bt_clicked(self):
     self.word = self.ui.lineEdit.text().strip()
     if self.word:
         # if chinese
         if is_alphabet(self.word[0]):
             self.is_zh = False
         else:
             self.is_zh = True
         # query on server
         server_context = self.client.get_word_info(self.word).strip()
         if server_context != 'None':
             wi = json.loads(server_context)
             self.painter.html = ''
             if self.is_zh:
                 self.painter.draw_zh_text(wi, self.draw_conf)
             else:
                 self.history_manager.add_item(self.word)
                 self.painter.draw_text(wi, self.draw_conf)
             self.ui.textBrowser.setHtml(self.painter.html)
         else:
             # Online search
             html = ''
             if self.ui.ol_cb.isChecked():
                 self.painter.html = ''
                 try:
                     self.ui.textBrowser.setHtml(self.painter.P_W_PATTERN %
                                                 'Searching OL...')
                     from src.WudaoOnline import get_text, get_zh_text
                     from urllib.error import URLError
                     if self.is_zh:
                         word_info = get_zh_text(self.word)
                     else:
                         word_info = get_text(self.word)
                     if not word_info['paraphrase']:
                         html += self.painter.P_W_PATTERN % (
                             'No such word: %s found online' % self.word)
                     if not self.is_zh:
                         self.history_manager.add_word_info(word_info)
                         self.painter.draw_text(word_info, self.draw_conf)
                     else:
                         self.painter.draw_zh_text(word_info,
                                                   self.draw_conf)
                     self.ui.textBrowser.setHtml(self.painter.html)
                     return
                 except ImportError:
                     html += self.painter.P_W_PATTERN % 'You need install bs4 first.'
                     html += self.painter.P_W_PATTERN % 'Use \'pip3 install bs4\' or get bs4 online.'
                 except URLError:
                     html += self.painter.P_W_PATTERN % 'No Internet : Please check your connection first'
             else:
                 # search in online cache first
                 word_info = self.history_manager.get_word_info(self.word)
                 if word_info:
                     self.history_manager.add_item(self.word)
                     self.painter.draw_text(word_info, self.draw_conf)
                     self.ui.textBrowser.setHtml(self.painter.html)
                     return
                 else:
                     html += self.painter.P_W_PATTERN % (
                         'Error: no such word :' + self.word)
                     html += self.painter.P_W_PATTERN % 'You can check Online Box to search it online.'
             self.ui.textBrowser.setHtml(html)