From b165b2d30d358909c77b3e1f3f027c3329e68511 Mon Sep 17 00:00:00 2001 From: sthoo Date: Sun, 4 Nov 2018 13:32:14 +0800 Subject: [PATCH] Add vocabulary.com web dictionary fix #83 --- addons/fastwq/service/dict/vocabulary.py | 38 ++++++++++++++++++++++ addons21/fastwq/service/dict/vocabulary.py | 38 ++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 addons/fastwq/service/dict/vocabulary.py create mode 100644 addons21/fastwq/service/dict/vocabulary.py diff --git a/addons/fastwq/service/dict/vocabulary.py b/addons/fastwq/service/dict/vocabulary.py new file mode 100644 index 0000000..2f45f73 --- /dev/null +++ b/addons/fastwq/service/dict/vocabulary.py @@ -0,0 +1,38 @@ +#-*- coding:utf-8 -*- +import random +from ..base import * + + +@register(u'Vocabulary.com') +class Vocabulary(WebService): + + def __init__(self): + super(Vocabulary, self).__init__() + + def _get_from_api(self): + data = self.get_response(u'https://www.vocabulary.com/dictionary/{}'.format(self.quote_word)) + soup = parse_html(data) + result = { + 'short': u'', + 'long': u'', + } + + # short + element = soup.find('p', class_='short') + if element: + result['short'] = u''.join(str(e) for e in element.contents) + + # long + element = soup.find('p', class_='long') + if element: + result['long'] = u''.join(str(e) for e in element.contents) + + return self.cache_this(result) + + @export([u'简短释义', u'Short definition']) + def fld_definate(self): + return self._get_field('short') + + @export([u'详细释义', u'Long definition']) + def fld_example(self): + return self._get_field('long') diff --git a/addons21/fastwq/service/dict/vocabulary.py b/addons21/fastwq/service/dict/vocabulary.py new file mode 100644 index 0000000..2f45f73 --- /dev/null +++ b/addons21/fastwq/service/dict/vocabulary.py @@ -0,0 +1,38 @@ +#-*- coding:utf-8 -*- +import random +from ..base import * + + +@register(u'Vocabulary.com') +class Vocabulary(WebService): + + def __init__(self): + super(Vocabulary, self).__init__() + + def _get_from_api(self): + data = self.get_response(u'https://www.vocabulary.com/dictionary/{}'.format(self.quote_word)) + soup = parse_html(data) + result = { + 'short': u'', + 'long': u'', + } + + # short + element = soup.find('p', class_='short') + if element: + result['short'] = u''.join(str(e) for e in element.contents) + + # long + element = soup.find('p', class_='long') + if element: + result['long'] = u''.join(str(e) for e in element.contents) + + return self.cache_this(result) + + @export([u'简短释义', u'Short definition']) + def fld_definate(self): + return self._get_field('short') + + @export([u'详细释义', u'Long definition']) + def fld_example(self): + return self._get_field('long')