-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
119 lines (103 loc) · 4.52 KB
/
main.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import sys
import json
import urllib
import urllib2
import hashlib
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.HideWindowAction import HideWindowAction
from ulauncher.api.shared.action.DoNothingAction import DoNothingAction
from ulauncher.api.shared.action.CopyToClipboardAction import CopyToClipboardAction
from ulauncher.api.shared.event import KeywordQueryEvent
reload(sys)
sys.setdefaultencoding("utf-8")
class HaiciDictExtension(Extension):
def __init__(self):
super(HaiciDictExtension, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
class KeywordQueryEventListener(EventListener):
def getMd5(self, value):
md5 = hashlib.md5()
md5.update(value)
return md5.hexdigest()
def getToken(self, word):
url = 'http://apii.dict.cn/mini.php?q=' + word
headers = {
'Cookie' : 'dictsid=1201.373420645.1071507.128476'
}
request = urllib2.Request(url = url, headers=headers)
response = None
try:
response = urllib2.urlopen(request, timeout=2)
responseData = response.read()
tokenStringStart = responseData.find('dict_pagetoken="')
if tokenStringStart == -1:
return False
return responseData[tokenStringStart +16 : tokenStringStart +16 + 32]
except urllib2.URLError as e:
return False
except:
pass
finally:
if response:
response.close()
def getExplain(self, word, token):
t = word + 'dictcn' + token
query = { 'q' : word, 's' : 2, 't' : self.getMd5(t)}
serializeData = urllib.urlencode(query)
url = "http://apii.dict.cn/ajax/dictcontent.php"
headers = {
'Content-Type' : 'application/x-www-form-urlencoded',
'Cookie' : 'dictsid=1201.373420645.1071507.128476'
}
request = urllib2.Request(url = url,data =serializeData, headers=headers)
response = None
try:
response = urllib2.urlopen(request, timeout=2)
if response.getcode() != 200:
return False
responseData = response.read()
return responseData
except urllib2.URLError as e:
return False
except:
pass
finally:
if response:
response.close()
def on_event(self, event, extension):
word = event.get_argument()
icon = 'images/icon.png'
showList = []
if word:
#step1. get token3
token = self.getToken(word)
print token
if token == False:
showList.append(ExtensionResultItem(icon=icon, name='network error in getting token,please try again', on_enter=HideWindowAction()))
#step2. get explain by token
explain = self.getExplain(word, token)
print explain
if explain == False:
showList.append(ExtensionResultItem(icon=icon, name='network error in getting explain,please try again', on_enter=HideWindowAction()))
#step3. analazy
jsonResult = json.loads(explain)
if 'e' not in jsonResult:
showList.append(ExtensionResultItem(icon=icon, name='no result', on_enter=HideWindowAction()))
else:
for item in jsonResult['e'].split('<br />'):
showList.append(ExtensionResultItem(icon=icon, name=item, on_enter=CopyToClipboardAction(item)))
# if has speech
if 't' in jsonResult:
showList.append(ExtensionResultItem(icon=icon, name='**inflections below**', on_enter=HideWindowAction()))
for item in jsonResult['t'].replace('<i>', '').replace('</i>', '').replace(' ', '').split('\r\n'):
if item != '':
showList.append(ExtensionResultItem(icon=icon, name=item, on_enter=CopyToClipboardAction(item)))
else:
showList.append(ExtensionResultItem(icon=icon, name='please type in some words', on_enter=HideWindowAction()))
#to render result
return RenderResultListAction(showList)
if __name__ == '__main__':
HaiciDictExtension().run()