Skip to content

Commit

Permalink
修复部分查询存在格式异常的问题;添加缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
zwwangoo committed Nov 6, 2019
1 parent 3e920a7 commit 88c036b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

requires = [
'requests>=2.22.0',
'lxml>=4.4.1'
'lxml>=4.4.1',
'cachelib',
'appdirs'
]


Expand Down
2 changes: 1 addition & 1 deletion translate_it/__version__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__name__ = 'translate-it'
__version__ = '0.1.3'
__version__ = '0.2.0'
__description__ = 'translate it for me.'
__license__ = 'Apache 2.0'
__author__ = 'wen'
Expand Down
20 changes: 17 additions & 3 deletions translate_it/translate_it.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import re
import requests
import appdirs
from lxml import etree
from argparse import ArgumentParser
from cachelib import FileSystemCache, NullCache

CACHE_DIR = appdirs.user_cache_dir('translate_it')
CACHE_ENTRY_MAX = 128

cache = FileSystemCache(CACHE_DIR, CACHE_ENTRY_MAX, default_timeout=0)


def get_content(words):
res = requests.get(f'https://dict.youdao.com/w/{" ".join(words)}')
res = requests.get(f'https://dict.youdao.com/w/{words}')
res.raise_for_status()
return res.text

Expand All @@ -27,8 +35,14 @@ def parse(response):


def tran(words):
response = get_content(words)
results = parse(response)
words = ' '.join(words)
results = cache.get(words)
if not results:
response = get_content(words)
results = [re.sub(r'\s', '', result) for result in parse(response)]
if results:
cache.set(words, results)

return results


Expand Down

0 comments on commit 88c036b

Please sign in to comment.