def query_without_print(self): self.word_info = {} server_contex = self.client.get_word_info(self.word).strip() if server_contex != 'None': self.word_info = json.loads(server_contex) else: self.word_info = self.history_manager.get_word_info(self.word) if not self.word_info: if ie(): try: from src.WudaoOnline import get_text, get_zh_text from urllib.error import URLError import bs4 import lxml self.word_info = get_text(self.word) if not self.word_info['paraphrase']: print('No such word: %s found online' % (self.painter.RED_PATTERN % self.word)) exit(0) self.history_manager.add_word_info(self.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.') exit(0) except URLError: print('Word not found, auto Online search...') print('No Internet : connection time out.') else: print('Word not found, auto Online search...') print('No Internet : Please check your connection or try again.') exit(0)
def query(self): word_info = {} # query on server server_context = self.client.get_word_info(self.word).strip() if not self.is_zh: self.history_manager.add_item(self.word) if server_context != 'None': word_info = json.loads(server_context) if self.is_zh: self.painter.draw_zh_text(word_info, self.conf) else: self.painter.draw_text(word_info, self.conf) else: # search in online cache first word_info = self.history_manager.get_word_info(self.word) if word_info: self.painter.draw_text(word_info, self.conf) else: if ie(): init() try: # online search from src.WudaoOnline import get_text, get_zh_text from urllib.error import URLError import bs4 import lxml if self.is_zh: word_info = get_zh_text(self.word) else: word_info = get_text(self.word) if not word_info['paraphrase']: print('No such word: %s found online' % (self.painter.RED_PATTERN % self.word)) exit(0) # store struct self.history_manager.add_word_info(word_info) if not self.is_zh: self.painter.draw_text(word_info, self.conf) else: self.painter.draw_zh_text(word_info, self.conf) 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.') exit(0) except URLError: print('Word not found, auto Online search...') print('No Internet : connection time out.') else: print('Word not found, auto Online search...') print( 'No Internet : Please check your connection or try again.' ) exit(0) if not self.conf['save'] and not self.is_zh: self.history_manager.save_note(word_info)
def query(self): word_info = {} # query on server server_context = self.client.get_word_info(self.word).strip() if not self.is_zh: self.history_manager.add_item(self.word) if server_context != 'None': word_info = json.loads(server_context) if self.is_zh: self.painter.draw_zh_text(word_info, self.conf) else: self.painter.draw_text(word_info, self.conf) else: # search in online cache first word_info = self.history_manager.get_word_info(self.word) if word_info: self.painter.draw_text(word_info, self.conf) else: if ie(): try: # online search from src.WudaoOnline import get_text, get_zh_text from urllib.error import URLError import bs4 import lxml if self.is_zh: word_info = get_zh_text(self.word) else: word_info = get_text(self.word) if not word_info['paraphrase']: print('No such word: %s found online' % (self.painter.RED_PATTERN % self.word)) exit(0) # store struct self.history_manager.add_word_info(word_info) if not self.is_zh: self.painter.draw_text(word_info, self.conf) else: self.painter.draw_zh_text(word_info, self.conf) 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.') exit(0) except URLError: print('Word not found, auto Online search...') print('No Internet : connection time out.') else: print('Word not found, auto Online search...') print('No Internet : Please check your connection or try again.') exit(0) if not self.conf['save'] and not self.is_zh: self.history_manager.save_note(word_info)
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')