コード例 #1
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)
コード例 #2
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)
コード例 #3
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.')
コード例 #4
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():
                 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)
コード例 #5
0
    def query(self):
        # query on server
        server_context = self.client.get_word_info(self.word).strip()
        if server_context != "None":
            wi = json.loads(server_context)
            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)
        else:
            # Online search
            if "o" in self.param_list or "-online-search" in self.param_list:
                try:
                    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"]:
                        print("No such word: %s found online" % self.word)
                        exit(0)
                    self.history_manager.add_item(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)
                except ImportError:
                    print("You need install bs4 first.")
                    print("Use 'pip3 install bs4' or get bs4 online.")
                except URLError:
                    print("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)
                else:
                    print("Error: no such word :" + self.word)
                    print("You can use -o to search online.")
コード例 #6
0
ファイル: WudaoCommand.py プロジェクト: cywxf/Wudao-dict
 def query(self):
     # query on server
     server_context = self.client.get_word_info(self.word).strip()
     if server_context != 'None':
         wi = json.loads(server_context)
         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)
     else:
         # Online search
         if 'o' in self.param_list or '-online-search' in self.param_list:
             try:
                 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']:
                     print('No such word: %s found online' % self.word)
                     exit(0)
                 self.history_manager.add_item(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)
             except ImportError:
                 print('You need install bs4 first.')
                 print('Use \'pip3 install bs4\' or get bs4 online.')
             except URLError:
                 print('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)
             else:
                 print('Error: no such word :' + self.word)
                 print('You can use -o to search online.')
コード例 #7
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)