From c2df36a7bdff995d69f3db2b71fba56c968737ff Mon Sep 17 00:00:00 2001 From: taoxinyi <850804004@qq.com> Date: Mon, 4 May 2020 14:31:09 -0400 Subject: [PATCH] Update endpoint for python --- .gitignore | 2 ++ win_python/color.ini | 4 ++- win_python/idict.py | 76 +++++++++++++++++++++++++++++++------------- 3 files changed, 59 insertions(+), 23 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..82195aa --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +venv +.idea \ No newline at end of file diff --git a/win_python/color.ini b/win_python/color.ini index ee8244c..3a1ca7e 100644 --- a/win_python/color.ini +++ b/win_python/color.ini @@ -3,4 +3,6 @@ word_color = 91 hint_color = 93 section_color = 92 text_color = 97 -source_color = 90 \ No newline at end of file +source_color = 90 +[CONFIG] +endpoint = \ No newline at end of file diff --git a/win_python/idict.py b/win_python/idict.py index 0c7d69b..791a8bd 100644 --- a/win_python/idict.py +++ b/win_python/idict.py @@ -7,6 +7,7 @@ import configparser import arrow from colorama import init +import traceback def get_color(color_code): @@ -17,44 +18,57 @@ def parse_brief(brief): sentences = None if args.news: sentences = json.loads( - requests.get("https://corpus.vocabulary.com/api/1.0/examples.json?maxResults=5&query=" + args.word).text)[ + requests.get( + "https://corpus.vocabulary.com/api/1.0/examples.json?maxResults=5&query=" + args.word).text)[ 'result']['sentences'] word = WORD_COLOR + brief['wordOut'] + ": " if 'relation' in brief['lemma']: word += TEXT_COLOR + ( - "%s为%s的%s" % (brief['wordOut'], brief['lemma']['lemma'], brief['lemma']['relation'])) + "%s为%s的%s" % ( + brief['wordOut'], brief['lemma']['lemma'], + brief['lemma']['relation'])) print(word) pron = "" if 'usPron' in brief: - pron += HINT_COLOR + " 美音 " + TEXT_COLOR + "/%s/" % brief['usPron']['ps'] + pron += HINT_COLOR + " 美音 " + TEXT_COLOR + "/%s/" % brief['usPron'][ + 'ps'] if 'ukPron' in brief: - pron += HINT_COLOR + " 英音 " + TEXT_COLOR + "/%s/" % brief['ukPron']['ps'] + pron += HINT_COLOR + " 英音 " + TEXT_COLOR + "/%s/" % brief['ukPron'][ + 'ps'] if pron: print(pron) if 'chnDefinitions' in brief: print(SECTION_COLOR + "中文释义") for chn_def in brief['chnDefinitions']: if 'pos' in chn_def: - print(" " + HINT_COLOR + chn_def['pos'].ljust(8) + TEXT_COLOR + chn_def[ - 'meaning']) + print( + " " + HINT_COLOR + chn_def['pos'].ljust(8) + TEXT_COLOR + + chn_def[ + 'meaning']) else: print(" " + "".ljust(8) + TEXT_COLOR + chn_def['meaning']) if 'engDefinitions' in brief: print(SECTION_COLOR + "英文释义") for eng_def in brief['engDefinitions']: if 'pos' in eng_def: - print(" " + HINT_COLOR + eng_def['pos'].ljust(8) + TEXT_COLOR + eng_def[ - 'meaning']) + print( + " " + HINT_COLOR + eng_def['pos'].ljust(8) + TEXT_COLOR + + eng_def[ + 'meaning']) else: print(" " + "".ljust(8) + TEXT_COLOR + eng_def['meaning']) if sentences: print(SECTION_COLOR + "新闻例句") for i, sentence in enumerate(sentences): print(TEXT_COLOR, - "".ljust(4) + (str(i + 1) + ".").ljust(3) + sentence['sentence']) - print(SOURCE_COLOR, "".ljust(7) + sentence['volume']['corpus']['name'] + "".ljust(4) + - arrow.get(sentence['volume']['dateAdded']).format("MMM DD, YYYY")) + "".ljust(4) + (str(i + 1) + ".").ljust(3) + sentence[ + 'sentence']) + print(SOURCE_COLOR, + "".ljust(7) + sentence['volume']['corpus']['name'] + "".ljust( + 4) + + arrow.get(sentence['volume']['dateAdded']).format( + "MMM DD, YYYY")) def parse_source(sentence_group): @@ -72,7 +86,8 @@ def parse_detail(detail): count = 1 print("".ljust(4) + HINT_COLOR + parse_source(sentenceGroup)) for sentence in sentenceGroup['sentences']: - print(TEXT_COLOR + "".ljust(8) + ("%s." % str(count)).ljust(3) + sentence['eng']) + print(TEXT_COLOR + "".ljust(8) + ("%s." % str(count)).ljust(3) + + sentence['eng']) print("".ljust(8) + "".ljust(3) + sentence['chn']) if count >= default_sent: break @@ -81,12 +96,15 @@ def parse_detail(detail): init() -sourceDict = {"CAMBRIDGE": "剑桥高阶英汉双解词典", "LONGMAN": "朗文当代高级英语词典", "COLLINS": "柯林斯英汉双解大词典", "ONLINE": "金山词霸"} +sourceDict = {"CAMBRIDGE": "剑桥高阶英汉双解词典", "LONGMAN": "朗文当代高级英语词典", + "COLLINS": "柯林斯英汉双解大词典", "ONLINE": "金山词霸"} parser = argparse.ArgumentParser(description='manual to this script') parser.add_argument('word', type=str, help="The word you want to query") -parser.add_argument('--detail', '-d', action='store', default=0, const=2, nargs='?', type=int, dest='detail', +parser.add_argument('--detail', '-d', action='store', default=0, const=2, + nargs='?', type=int, dest='detail', help="Show the detailed meaning of the word") -parser.add_argument('--brief', '-b', action='store_true', default=True, help="Show the brief meaning of the word", ) +parser.add_argument('--brief', '-b', action='store_true', default=True, + help="Show the brief meaning of the word", ) parser.add_argument('--news', '-n', action='store_true', default=False, help="Whether show sentence examples from news") @@ -100,18 +118,32 @@ def parse_detail(detail): config_path = os.path.join(bundle_dir, "color.ini") config = configparser.ConfigParser() config.read(config_path) -WORD_COLOR = get_color(config.getint('COLOR', 'word_color') if config.getint('COLOR', 'word_color') else 91) -HINT_COLOR = get_color(config.getint('COLOR', 'hint_color') if config.getint('COLOR', 'hint_color') else 92) -SECTION_COLOR = get_color(config.getint('COLOR', 'section_color') if config.getint('COLOR', 'section_color') else 93) -TEXT_COLOR = get_color(config.getint('COLOR', 'text_color') if config.getint('COLOR', 'text_color') else 97) -SOURCE_COLOR = get_color(config.getint('COLOR', 'source_color') if config.getint('COLOR', 'source_color') else 90) +WORD_COLOR = get_color( + config.getint('COLOR', 'word_color') if config.getint('COLOR', + 'word_color') else 91) +HINT_COLOR = get_color( + config.getint('COLOR', 'hint_color') if config.getint('COLOR', + 'hint_color') else 92) +SECTION_COLOR = get_color( + config.getint('COLOR', 'section_color') if config.getint('COLOR', + 'section_color') else 93) +TEXT_COLOR = get_color( + config.getint('COLOR', 'text_color') if config.getint('COLOR', + 'text_color') else 97) +SOURCE_COLOR = get_color( + config.getint('COLOR', 'source_color') if config.getint('COLOR', + 'source_color') else 90) +ENDPOINT = config.get("CONFIG", "endpoint") -detail = json.loads(requests.get("https://ireading.site/word/detail?json=true&word=" + args.word).text) +detail = json.loads( + requests.get(ENDPOINT + "/word/detail?json=true&word=" + args.word).text) default_sent = args.detail + try: if args.detail: parse_detail(detail) else: parse_brief(detail['wordBrief']) -except: +except Exception as e: + traceback.print_exc() print("该单词不存在")