User Guide     Develop Guide     Service Shop

The advanced users can implement new web dictionary services to grab the word explanations, or local dictionary services to extract some certain fields. See a webservice sample and a mdxservice sample for the details.

WebService

Sample.

Inherit WebService class

@register(label) is used to register the service, and parameter label as the dictionary name will be shown in the dictioary list.

@register(u'有道词典')
class Youdao(WebService):
    """service implementation"""

Define Dictionary Field

The field export function has to be decorated with @export(fld_name, order).

@export(u'美式音标', 1)
def fld_phonetic_us(self):
    return self._get_field('phonitic_us')

@export(u'英式音标', 2)
def fld_phonetic_uk(self):
    return self._get_field('phonitic_uk')

Decorating the Field (optional)

Using @with_style(**kwargs) to specify the css style strings or files, javascript strings or files, whether wrapping the css to avoid latent style interference.

@with_styles(cssfile='_youdao.css', need_wrap_css=True, wrap_class='youdao')
def _get_singledict(self, single_dict, lang='eng'):
    url = "http://m.youdao.com/singledict?q=%s&dict=%s&le=%s&more=false" % (
        self.word, single_dict, lang)
    try:
        return urllib2.urlopen(url, timeout=5).read()
    except:
        return ''

MdxService

This addon implements a basic local dictionary service supporting mdx and stardict formats, which roughly extracts the entire explanation with the field name “default”. It is impossible for the basic service to extract all the intended fields for any given dictionary, but the user can create a inherited one for some path-specified dictionary.

Sample.

Inherit MdxService class

@register(label) is used to register the service, and parameter label as the dictionary name will be shown in the dictioary list.

@register('Sample-LDOCE6')
class Ldoce6(MdxService):
    """service implementation"""

Define Dictionary Field

@export(u'音标', 1)
def fld_phonetic(self):
    html = self.get_html()
    m = re.search(r'<span class="pron">(.*?)</span>', html)
    if m:
        return m.groups()[0]
    return ''