From 9fca1dbc919fb3d827b622b97ae5106c2e4b0e19 Mon Sep 17 00:00:00 2001 From: xiangyufan Date: Fri, 29 Sep 2017 11:14:05 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E5=8D=95=E8=AF=8D=E5=86=99=E8=BF=9B?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/dataSources.xml | 14 +++++++++ ydcv.py | 69 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 .idea/dataSources.xml diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..2710e88 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,14 @@ + + + + + sqlite.xerial + true + org.sqlite.JDBC + jdbc:sqlite:C:\Users\Administrator\.ydcv\word.db + + + + + + \ No newline at end of file diff --git a/ydcv.py b/ydcv.py index 1cb7744..0cd8871 100755 --- a/ydcv.py +++ b/ydcv.py @@ -11,6 +11,10 @@ import re import sys import platform +import os +import sqlite3 + +from termcolor import colored try: # Py3 @@ -23,6 +27,17 @@ reload(sys) sys.setdefaultencoding('utf8') +DEFAULT_PATH = os.path.join(os.path.expanduser('~'), '.ydcv') + +CREATE_TABLE_WORD = ''' +CREATE TABLE IF NOT EXISTS Word +( +name TEXT PRIMARY KEY, +content TEXT, +resource TEXT +addtime TIMESTAMP NOT NULL DEFAULT (DATETIME('NOW', 'LOCALTIME')) +) +''' API = "YouDaoCV" API_KEY = "659600698" @@ -132,6 +147,7 @@ def print_explanation(data, options): else: print() + resource = None if options.simple is False: # Web reference if 'web' in _d: @@ -149,8 +165,8 @@ def print_explanation(data, options): ol_res = online_resources(query) if len(ol_res) > 0: print(_c('\n Online Resource:', 'cyan')) - res = ol_res if options.full else ol_res[:1] - print(*map((' * ' + _c('{0}', 'underline')).format, res), sep='\n') + resource = ol_res if options.full else ol_res[:1] + print(*map((' * ' + _c('{0}', 'underline')).format, resource), sep='\n') # read out the word if options.read: sys_name = platform.system() @@ -165,7 +181,7 @@ def print_explanation(data, options): if not has_result: print(_c(' -- No result for this query.', 'red')) - print() + return json.dumps(data, ensure_ascii=False), json.dumps(resource) if resource else "" def lookup_word(word): @@ -178,7 +194,46 @@ def lookup_word(word): except IOError: print("Network is unavailable") else: - print_explanation(json.loads(data), options) + return print_explanation(json.loads(data), options) + +def add_word(word): + '''add the word or phrase to database.''' + + conn = sqlite3.connect(os.path.join(DEFAULT_PATH, 'word.db')) + curs = conn.cursor() + curs.execute('SELECT content FROM Word WHERE name = "%s"' % word) + res = curs.fetchall() + if res: + print(colored(word + ' 在数据库中已存在,不需要添加', 'white', 'on_red')) + sys.exit() + + try: + content, resource = lookup_word(word) + curs.execute('insert into word(name, content, resource) values ("%s", "%s", "%s")' % ( + word, content, resource)) + except Exception as e: + print(colored('something\'s wrong, you can\'t add the word', 'white', 'on_red')) + print(e) + else: + conn.commit() + print(colored('%s has been inserted into database' % word, 'green')) + finally: + curs.close() + conn.close() + +def init_db(): + """ init database + """ + if not os.path.exists(os.path.join(DEFAULT_PATH, 'word.db')): + os.mkdir(DEFAULT_PATH) + with open(os.path.join(DEFAULT_PATH, 'word_list.txt'), 'w') as f: + pass + conn = sqlite3.connect(os.path.join(DEFAULT_PATH, 'word.db')) + curs = conn.cursor() + curs.execute(CREATE_TABLE_WORD) + conn.commit() + curs.close() + conn.close() if __name__ == "__main__": @@ -213,13 +268,18 @@ def lookup_word(word): parser.add_argument('words', nargs='*', help="words to lookup, or quoted sentences to translate.") + parser.add_argument("--init", + help="") options = parser.parse_args() if options.words: + init_db() for word in options.words: lookup_word(word) + add_word(word) else: + init_db() if options.selection: last = check_output(["xclip", "-o"], universal_newlines=True) print("Waiting for selection>") @@ -231,6 +291,7 @@ def lookup_word(word): last = curr if last.strip(): lookup_word(last) + add_word(last) print("Waiting for selection>") except (KeyboardInterrupt, EOFError): break From 82c980500a24cc7123e68570ac7022750dcc4f64 Mon Sep 17 00:00:00 2001 From: xiangyufan Date: Fri, 29 Sep 2017 15:45:58 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E7=94=B1=E4=BA=8E=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E8=A7=A3=E5=86=B3sqlite3=E6=8F=92=E5=85=A5=E5=BC=95=E5=8F=B7?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C=E6=9A=82=E6=97=B6=E5=8F=AA?= =?UTF-8?q?=E5=86=99=E5=85=A5=E5=8D=95=E8=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ydcv.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ydcv.py b/ydcv.py index 0cd8871..1bc125e 100755 --- a/ydcv.py +++ b/ydcv.py @@ -33,8 +33,6 @@ CREATE TABLE IF NOT EXISTS Word ( name TEXT PRIMARY KEY, -content TEXT, -resource TEXT addtime TIMESTAMP NOT NULL DEFAULT (DATETIME('NOW', 'LOCALTIME')) ) ''' @@ -148,6 +146,7 @@ def print_explanation(data, options): print() resource = None + web = None if options.simple is False: # Web reference if 'web' in _d: @@ -181,7 +180,8 @@ def print_explanation(data, options): if not has_result: print(_c(' -- No result for this query.', 'red')) - return json.dumps(data, ensure_ascii=False), json.dumps(resource) if resource else "" + return json.dumps(data, ensure_ascii=False) if web else '', \ + json.dumps(resource) if resource else '' def lookup_word(word): @@ -208,9 +208,9 @@ def add_word(word): sys.exit() try: - content, resource = lookup_word(word) - curs.execute('insert into word(name, content, resource) values ("%s", "%s", "%s")' % ( - word, content, resource)) + curs.execute( + "insert into word(name) values ('{}')" + .format(word)) except Exception as e: print(colored('something\'s wrong, you can\'t add the word', 'white', 'on_red')) print(e) From 1a4f6c901a0c5ccd5c2b0a8ec53b487b57a45082 Mon Sep 17 00:00:00 2001 From: xiangyufan Date: Fri, 29 Sep 2017 16:43:44 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E5=B1=8F=E5=B9=95=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E5=8E=86=E5=8F=B2=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/dataSources.xml | 2 +- ydcv.py | 90 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 73 insertions(+), 19 deletions(-) diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml index 2710e88..e31aeb0 100644 --- a/.idea/dataSources.xml +++ b/.idea/dataSources.xml @@ -1,7 +1,7 @@ - + sqlite.xerial true org.sqlite.JDBC diff --git a/ydcv.py b/ydcv.py index 1bc125e..5e6c57e 100755 --- a/ydcv.py +++ b/ydcv.py @@ -24,6 +24,7 @@ # Py 2.7 from urllib import quote from urllib2 import urlopen + reload(sys) sys.setdefaultencoding('utf8') @@ -86,7 +87,6 @@ def colorize(cls, s, color=None): def online_resources(query): - english = re.compile('^[a-z]+$', re.IGNORECASE) chinese = re.compile('^[\u4e00-\u9fff]+$', re.UNICODE) @@ -115,7 +115,8 @@ def print_explanation(data, options): try: if 'uk-phonetic' in _b and 'us-phonetic' in _b: - print(" UK: [{0}]".format(_c(_b['uk-phonetic'], 'yellow')), end=',') + print(" UK: [{0}]".format(_c(_b['uk-phonetic'], 'yellow')), + end=',') print(" US: [{0}]".format(_c(_b['us-phonetic'], 'yellow'))) elif 'phonetic' in _b: print(" [{0}]".format(_c(_b['phonetic'], 'yellow'))) @@ -165,7 +166,8 @@ def print_explanation(data, options): if len(ol_res) > 0: print(_c('\n Online Resource:', 'cyan')) resource = ol_res if options.full else ol_res[:1] - print(*map((' * ' + _c('{0}', 'underline')).format, resource), sep='\n') + print(*map((' * ' + _c('{0}', 'underline')).format, resource), + sep='\n') # read out the word if options.read: sys_name = platform.system() @@ -175,7 +177,9 @@ def print_explanation(data, options): if spawn.find_executable('festival'): Popen('echo ' + query + ' | festival --tts', shell=True) else: - print(_c(' -- Please Install festival(http://www.cstr.ed.ac.uk/projects/festival/).', 'red')) + print(_c( + ' -- Please Install festival(http://www.cstr.ed.ac.uk/projects/festival/).', + 'red')) if not has_result: print(_c(' -- No result for this query.', 'red')) @@ -190,37 +194,71 @@ def lookup_word(word): data = urlopen( "http://fanyi.youdao.com/openapi.do?keyfrom={0}&" "key={1}&type=data&doctype=json&version=1.2&q={2}" - .format(API, API_KEY, word)).read().decode("utf-8") + .format(API, API_KEY, word)).read().decode("utf-8") except IOError: print("Network is unavailable") else: return print_explanation(json.loads(data), options) + def add_word(word): '''add the word or phrase to database.''' conn = sqlite3.connect(os.path.join(DEFAULT_PATH, 'word.db')) curs = conn.cursor() - curs.execute('SELECT content FROM Word WHERE name = "%s"' % word) + curs.execute('SELECT name FROM Word WHERE name = "%s"' % word) res = curs.fetchall() if res: - print(colored(word + ' 在数据库中已存在,不需要添加', 'white', 'on_red')) sys.exit() try: - curs.execute( - "insert into word(name) values ('{}')" - .format(word)) + curs.execute('insert into word(name) values ("{}")'.format(word)) except Exception as e: - print(colored('something\'s wrong, you can\'t add the word', 'white', 'on_red')) + print(colored('something\'s wrong, you can\'t add the word', 'white', + 'on_red')) print(e) else: conn.commit() - print(colored('%s has been inserted into database' % word, 'green')) finally: curs.close() conn.close() + +def match_history(parameter): + """ match history words + """ + conn = sqlite3.connect(os.path.join(DEFAULT_PATH, 'word.db')) + curs = conn.cursor() + if parameter.isdigit(): + # parameter is a number + num = int(parameter) + if num != 0: + if num < 0: + order = "ASC" + else: + order = "DESC" + curs.execute( + "SELECT * FROM Word ORDER BY addtime {} LIMIT {}".format(order, + num)) + else: + curs.execute("SELECT * FROM Word") + res = curs.fetchall() + + else: + date = parameter + curs.execute( + "SELECT * FROM Word WHERE addtime LIKE '{}%'".format(date)) + res = curs.fetchall() + return res + + +def show_history(res): + """ show history words on the screen + """ + for word, addtime in res: + print("\t" + word + "\t" + addtime) + + def init_db(): """ init database """ @@ -260,6 +298,8 @@ def init_db(): action="store_true", default=False, help="show explaination of current selection.") + parser.add_argument("-H", "--history", + help="show history words. ") parser.add_argument('--color', choices=['always', 'auto', 'never'], default='auto', @@ -268,16 +308,17 @@ def init_db(): parser.add_argument('words', nargs='*', help="words to lookup, or quoted sentences to translate.") - parser.add_argument("--init", - help="") options = parser.parse_args() if options.words: init_db() for word in options.words: - lookup_word(word) - add_word(word) + if '"' in word: + print("Non-compliant word!") + else: + lookup_word(word) + add_word(word) else: init_db() if options.selection: @@ -286,7 +327,8 @@ def init_db(): while True: try: sleep(0.1) - curr = check_output(["xclip", "-o"], universal_newlines=True) + curr = check_output(["xclip", "-o"], + universal_newlines=True) if curr != last: last = curr if last.strip(): @@ -295,6 +337,14 @@ def init_db(): print("Waiting for selection>") except (KeyboardInterrupt, EOFError): break + elif options.history: + print("Print words history: ") + ret = match_history(options.history) + if ret: + show_history(ret) + else: + print("Did not find historical data! ") + else: try: import readline @@ -307,7 +357,11 @@ def init_db(): else: words = raw_input('> ') if words.strip(): - lookup_word(words) + if '"' in words: + print("Non-compliant word!") + else: + lookup_word(words) + add_word(words) except KeyboardInterrupt: print() continue From 025d5e0e750871e6c673361fb87f48dccde7dd48 Mon Sep 17 00:00:00 2001 From: xiangyufan Date: Fri, 29 Sep 2017 16:51:09 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E6=89=93=E5=8D=B0=E5=AF=B9=E9=BD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ydcv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ydcv.py b/ydcv.py index 5e6c57e..cc24e43 100755 --- a/ydcv.py +++ b/ydcv.py @@ -256,7 +256,7 @@ def show_history(res): """ show history words on the screen """ for word, addtime in res: - print("\t" + word + "\t" + addtime) + print('|' + word.ljust(20) + '|' + addtime.center(20) + '|') def init_db(): From 189b52326c683d951e67813a6419d53631648d28 Mon Sep 17 00:00:00 2001 From: xiangyufan Date: Fri, 29 Sep 2017 16:59:10 +0800 Subject: [PATCH 5/6] debug --- ydcv.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ydcv.py b/ydcv.py index cc24e43..656e266 100755 --- a/ydcv.py +++ b/ydcv.py @@ -229,7 +229,7 @@ def match_history(parameter): """ conn = sqlite3.connect(os.path.join(DEFAULT_PATH, 'word.db')) curs = conn.cursor() - if parameter.isdigit(): + try: # parameter is a number num = int(parameter) if num != 0: @@ -244,7 +244,7 @@ def match_history(parameter): curs.execute("SELECT * FROM Word") res = curs.fetchall() - else: + except ValueError: date = parameter curs.execute( "SELECT * FROM Word WHERE addtime LIKE '{}%'".format(date)) From 3ca4e28ddfd1e4d0f6a35e6d194472eb7edee7e8 Mon Sep 17 00:00:00 2001 From: xiangyufan Date: Fri, 29 Sep 2017 18:12:44 +0800 Subject: [PATCH 6/6] debug --- ydcv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ydcv.py b/ydcv.py index 656e266..1c72f49 100755 --- a/ydcv.py +++ b/ydcv.py @@ -239,7 +239,7 @@ def match_history(parameter): order = "DESC" curs.execute( "SELECT * FROM Word ORDER BY addtime {} LIMIT {}".format(order, - num)) + abs(num))) else: curs.execute("SELECT * FROM Word") res = curs.fetchall()