Skip to content

Commit

Permalink
Update endpoint for python
Browse files Browse the repository at this point in the history
  • Loading branch information
taoxinyi committed May 4, 2020
1 parent 2e9144a commit c2df36a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
venv
.idea
4 changes: 3 additions & 1 deletion win_python/color.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ word_color = 91
hint_color = 93
section_color = 92
text_color = 97
source_color = 90
source_color = 90
[CONFIG]
endpoint =
76 changes: 54 additions & 22 deletions win_python/idict.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import configparser
import arrow
from colorama import init
import traceback


def get_color(color_code):
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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")

Expand All @@ -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("该单词不存在")

0 comments on commit c2df36a

Please sign in to comment.