Skip to content
This repository has been archived by the owner on Apr 16, 2023. It is now read-only.

add the log of each queried word and word counts #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,16 @@

Simple wrapper for Youdao online translate (Chinese <-> English) service [API](http://fanyi.youdao.com/openapi?path=data-mode), as an alternative to the StarDict Console Version(sdcv).


## Record
Each word you query will be logged under the `.ydcv_history` in your home directory, and records the number of times each word was queried in descending order.
For example:
```
hello : 5
你好 : 3
world : 2
...
```

## Environment:
* Python ( >=2.7, 3.x )
* Python ( >=2.7, 3.x )
28 changes: 28 additions & 0 deletions src/ydcv.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/usr/bin/env python

from __future__ import unicode_literals
from __future__ import print_function

import os
from argparse import ArgumentParser
from subprocess import check_output
from subprocess import call
Expand Down Expand Up @@ -42,6 +45,7 @@ def __getattr__(self, name):
raise AttributeError("'%s' has no attribute '%s'" % (
self.__class__.__name__, name))


options = GlobalOptions()


Expand Down Expand Up @@ -217,6 +221,7 @@ def print_explanation(data, options):


def lookup_word(word):
real_word = word
word = quote(word)
if word == '%5Cq' or word == '%3Aq':
sys.exit("Thanks for using, goodbye!")
Expand All @@ -230,9 +235,31 @@ def lookup_word(word):
except IOError:
print("Network is unavailable")
else:
record_word(real_word)
print_explanation(json.loads(data), options)


def record_word(word):
file = os.path.expanduser('~') + '/.ydcv_history'
word_count = {}
try:
with open(file, 'r', encoding='utf-8', errors='ignore') as f:
for line in f.readlines():
arr = line.strip().strip('\n').split(' : ')
word_count[arr[0]] = int(arr[1])
except FileNotFoundError:
pass
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
sorted_key_list = sorted(word_count, key=lambda x: word_count[x], reverse=True)
lines = map(lambda x: (x + ' : ' + str(word_count[x])) + '\n', sorted_key_list)
with open(file, 'w', encoding='utf-8') as f:
for line in lines:
f.write(line)


def arg_parse():
parser = ArgumentParser(description="Youdao Console Version")
parser.add_argument('-f', '--full',
Expand Down Expand Up @@ -321,5 +348,6 @@ def main():
break
print("\nBye")


if __name__ == "__main__":
main()