-
Notifications
You must be signed in to change notification settings - Fork 3
/
iciba.py
83 lines (60 loc) · 1.79 KB
/
iciba.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from urllib.parse import urlencode
import urllib3
from common import LitPlugin, LitJob
import logging
import webbrowser
BASE = 'http://www.iciba.com/index.php'
def _uri(key):
return '%s?%s' % (BASE, urlencode({'a': 'suggest', 's': key.replace(' ', '|1{')}))
def _trans_uri(query):
return 'http://cdict.net/?%s' % urlencode({'q': query})
def _parse(data):
def inner():
for line in data.decode('utf-8').split('\n'):
key = line.strip().split('_')[0].replace('|1{', ' ')
if key:
yield key
return list(inner())
class Iciba(LitPlugin):
def __init__(self):
super(Iciba, self).__init__()
self.http = urllib3.PoolManager()
@property
def name(self):
return 't'
def _fetch(self, key):
r = self.http.request('GET', _uri(key))
if r.status == 200:
return r.data
else:
return None
def _query(self, key):
try:
data = self._fetch(key)
if data:
return _parse(data)
except Exception as e:
logging.error(e)
return None
def lit(self, query, *args, **kargs):
return Job(self, query)
def select(self, content, index):
try:
webbrowser.open(_trans_uri(content[index.row()]))
except Exception as e:
logging.error(e)
class Job(LitJob):
def __init__(self, master, query):
super(Job, self).__init__()
self.master = master
self.query = query
self._finished = False
@property
def finished(self):
return self._finished
def __call__(self):
result = self.master._query(self.query)
self._finished = not result is None
return result