예제 #1
0
def query(keyword, use_db=True, use_dict=True, play_voice=False):
    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)
            result.update(spider.get_result(use_api=False))

        # 更新数据库
        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)
예제 #2
0
파일: main.py 프로젝트: hyzhangsf/youdao
            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()
예제 #3
0
파일: main.py 프로젝트: tiancode/youdao
            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()