-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiciba.py
149 lines (133 loc) · 4.91 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python
# -*- coding:utf8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
__author__ = '[email protected]'
__version__ = '20220501:10'
from alfred.feedback import Feedback
import requests
import sys
import time
import hashlib
import json
# reload(sys)
# sys.setdefaultencoding('utf8')
def get_phonetic_symbols_new(word, headers):
word = word.strip()
# url = "https://dict.iciba.com/dictionary/word/query/web?client=6&key=1000006×tamp=1628233093905&word=hello&signature=0a78228c59344a8c368f20d47fe6fd2b"
data = [
("client", "6"),
("key", "1000006"),
("timestamp", str(int(time.time() * 1000))),
("word", word),
]
url = "https://dict.iciba.com/dictionary/word/query/web"
to_sign = url[len("https:"):].replace("//dict.iciba.com", "")
for k, v in data:
to_sign += v
to_sign += "7ece94d9f9c202b0d2ec557dg4r9bc"
sign = hashlib.md5(to_sign.encode('utf8')).hexdigest()
data.append(("signature", sign))
url += "?"
for k, v in data:
url += "{k}={v}&".format(k=k, v=v)
url.rstrip("&")
r = requests.get(url, headers=headers)
res = r.json()
try:
fb = Feedback()
msg = res["message"]
for symbol in msg['baesInfo']['symbols']:
means = symbol['parts']
for mean in means:
if mean['part']:
subtitle = mean['part'] + ' ' + '; '.join(mean['means'])
else:
subtitle = '; '.join(mean['means'])
if 'ph_en' in symbol and 'ph_am' in symbol:
title = '{word} 英:[{en}] 美:[{am}]'.format(word=word, en=symbol['ph_en'], am=symbol['ph_am'])
elif 'word_symbol' in symbol:
title = '{word} 拼音:[{word_symbol}]'.format(word=word, word_symbol=symbol['word_symbol'])
kwargs = {
'title': title,
'subtitle': subtitle,
'arg': 'http://www.iciba.com/%s' % word,
}
fb.addItem(**kwargs)
fb.output()
except Exception as ex:
print("EXCEPT:", ex)
def get_phonetic_symbols(word, headers):
word = word.strip()
url = 'http://www.iciba.com/index.php?a=getWordMean&c=search&list=1&word=%s' % word
try:
r = requests.get(url, headers=headers)
res = r.json()
fb = Feedback()
for symbol in res['baesInfo']['symbols']:
means = symbol['parts']
for mean in means:
if mean['part']:
subtitle = mean['part'] + ' ' + '; '.join(mean['means'])
else:
subtitle = '; '.join(mean['means'])
if 'ph_en' in symbol and 'ph_am' in symbol:
title = '{word} 英:[{en}] 美:[{am}]'.format(word=word, en=symbol['ph_en'], am=symbol['ph_am'])
elif 'word_symbol' in symbol:
title = '{word} 拼音:[{word_symbol}]'.format(word=word, word_symbol=symbol['word_symbol'])
kwargs = {
'title': title,
'subtitle': subtitle,
'arg': 'http://www.iciba.com/%s' % word,
}
fb.addItem(**kwargs)
fb.output()
except:
pass
def get_suggest(q, headers):
q = q.strip()
url = 'http://dict-mobile.iciba.com/interface/index.php?c=word&m=getsuggest&nums=5&client=6&uid=0&is_need_mean=1&word=%s' % q
try:
r = requests.get(url, headers=headers)
res = r.json()
fb = Feedback()
for msg in res['message']:
means = msg['means']
if not means:
kwargs = {
'title': msg['key'],
'autocomplete': '> %s' % msg['key'],
'valid': False,
}
fb.addItem(**kwargs)
continue
for mean in means:
if mean['part']:
subtitle = mean['part'] + ' ' + '; '.join(mean['means'])
else:
subtitle = '; '.join(mean['means'])
kwargs = {
'title': msg['key'],
'subtitle': subtitle,
'arg': 'http://www.iciba.com/%s' % q,
'autocomplete': '> %s' % msg['key'],
'valid': False,
}
fb.addItem(**kwargs)
fb.output()
except:
pass
if __name__ == '__main__':
if len(sys.argv) < 2:
sys.exit()
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.89 Safari/537.36',
}
q = sys.argv[1]
if '>' in q:
q = q.split('>')[-1]
get_phonetic_symbols_new(q, headers)
else:
get_suggest(q, headers)