def query(keyword, use_db=True, use_api=False, play_voice=False, use_dict=True): update_word = [True] word = Word.get_word(keyword) result = {'query': keyword, 'errorCode': 60} if use_db and word: result.update(json.loads(word.json_data)) update_word[0] = False elif update_word[0]: # 从starditc中查找 if use_dict and config.config.get('stardict'): try: from lib.cpystardict import Dictionary except ImportError: from lib.pystardict import Dictionary colors = deque(['cyan', 'yellow', 'blue']) stardict_base = config.config.get('stardict') stardict_trans = [] for dic_dir in os.listdir(stardict_base): dic_file = os.listdir(os.path.join(stardict_base, dic_dir))[0] name, ext = os.path.splitext(dic_file) name = name.split('.')[0] dic = Dictionary(os.path.join(stardict_base, dic_dir, name)) try: dic_exp = dic[keyword.encode("utf-8")] except KeyError: pass else: dic_exp = unicode(dic_exp.decode('utf-8')) stardict_trans.append( colored( u"[{dic}]:{word}".format(dic=name, word=keyword), 'green')) color = colors.popleft() colors.append(color) stardict_trans.append(colored(dic_exp, color)) stardict_trans.append( colored(u'========================', 'magenta')) if stardict_trans: result['stardict'] = u'\n'.join(stardict_trans) result['errorCode'] = 0 # 从stardict中没有匹配单词 if not result['errorCode'] == 0: spider = YoudaoSpider(keyword) try: result.update(spider.get_result(use_api)) except requests.HTTPError, e: print colored(u'网络错误: %s' % e.message, 'red') sys.exit() # 更新数据库 new_word = word if word else Word() new_word.keyword = keyword new_word.json_data = json.dumps(result) new_word.save()
def query(keyword, use_db=True, use_api=False, play_voice=False, use_dict=True): update_word = [True] word = Word.get_word(keyword) result = {"query": keyword, "errorCode": 60} if use_db and word: result.update(json.loads(word.json_data)) update_word[0] = False elif update_word[0]: # 从starditc中查找 if use_dict and config.config.get("stardict"): try: from lib.cpystardict import Dictionary except ImportError: from lib.pystardict import Dictionary colors = deque(["cyan", "yellow", "blue"]) stardict_base = config.config.get("stardict") stardict_trans = [] for dic_dir in os.listdir(stardict_base): dic_file = os.listdir(os.path.join(stardict_base, dic_dir))[0] name, ext = os.path.splitext(dic_file) name = name.split(".")[0] dic = Dictionary(os.path.join(stardict_base, dic_dir, name)) try: dic_exp = dic[keyword.encode("utf-8")] except KeyError: pass else: dic_exp = unicode(dic_exp.decode("utf-8")) stardict_trans.append(colored(u"[{dic}]:{word}".format(dic=name, word=keyword), "green")) color = colors.popleft() colors.append(color) stardict_trans.append(colored(dic_exp, color)) stardict_trans.append(colored(u"========================", "magenta")) if stardict_trans: result["stardict"] = u"\n".join(stardict_trans) result["errorCode"] = 0 # 从stardict中没有匹配单词 if not result["errorCode"] == 0: spider = YoudaoSpider(keyword) try: result.update(spider.get_result(use_api)) except requests.HTTPError, e: print colored(u"网络错误: %s" % e.message, "red") sys.exit() # 更新数据库 new_word = word if word else Word() new_word.keyword = keyword new_word.json_data = json.dumps(result) new_word.save()
def batch(fileUrl, words): with open(fileUrl, "w") as file: for word in words: # keyword = unicode(word, encoding=sys.getfilesystemencoding()) spider = YoudaoSpider(word) try: result = spider.get_result(False) except requests.HTTPError, e: print colored(u'网络错误: %s' % e.message, 'red') sys.exit() basic = ''; translation = ''; if 'basic' in result: if 'us-phonetic' in result['basic']: basic += u'美音:[' + result['basic']['us-phonetic'] + ']' if 'uk-phonetic' in result['basic']: basic += u'英音:[' + result['basic']['uk-phonetic'] + ']' translation = u' '.join(result['basic']['explains']) row = word + '\t' + basic + '\t' + translation + '\n' file.write(row.encode('utf8'))
try: result.update(spider.get_result(use_api)) except requests.HTTPError, e: print colored(u'网络错误: %s' % e.message, 'red') sys.exit() # 更新数据库 new_word = word if word else Word() new_word.keyword = keyword new_word.json_data = json.dumps(result) new_word.save() show_result(result) if play_voice: print(colored(u'获取发音:{word}'.format(word=keyword), 'green')) voice_file = YoudaoSpider.get_voice(keyword) print(colored(u'获取成功,播放中...', 'green')) play(voice_file) def show_db_list(): print colored(u'保存在数据库中的单词及查询次数:', 'blue') for word in Word.select(): print colored(word.keyword, 'cyan'), colored(str(word.count), 'green') def del_word(keyword): if keyword: try: word = Word.select().where(Word.keyword == keyword).get() word.delete_instance()
try: result.update(spider.get_result(use_api)) except requests.HTTPError, e: print colored(u"网络错误: %s" % e.message, "red") sys.exit() # 更新数据库 new_word = word if word else Word() new_word.keyword = keyword new_word.json_data = json.dumps(result) new_word.save() show_result(result) if play_voice: print (colored(u"获取发音:{word}".format(word=keyword), "green")) voice_file = YoudaoSpider.get_voice(keyword) print (colored(u"获取成功,播放中...", "green")) play(voice_file) def show_db_list(): print colored(u"保存在数据库中的单词及查询次数:", "blue") for word in Word.select(): print colored(word.keyword, "cyan"), colored(str(word.count), "green") def del_word(keyword): if keyword: try: word = Word.select().where(Word.keyword == keyword).get() word.delete_instance()