-
Notifications
You must be signed in to change notification settings - Fork 1
/
codic.py
35 lines (27 loc) · 940 Bytes
/
codic.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
#!/usr/bin/env python
# -*- coding=utf-8 -*-
import sys
from workflow import Workflow
from urllib import urlencode
from urllib2 import Request, urlopen
import json
url = 'https://codic.jp/-/engine/translate.json'
headers = {'X-Requested-With': 'XMLHttpRequest'}
def main(wf):
query = wf.args[0]
values = {'acronym_style': 'literal',
'casing': 'lower underscore'.encode('utf-8'),
'dictionary_id': '0',
'data[0][id]': '0',
'data[0][text]': query.encode('utf-8')}
res = urlopen(Request(url, urlencode(values), headers))
j = json.loads(res.read())
#wf.logger.debug(j)
text = j['data']['translation'][0]['translation']['translatedText']
if not len(text):
text = 'No Results'
wf.add_item(title = text, icon='icon.png', arg = text, valid = True)
wf.send_feedback()
if __name__ == '__main__':
wf = Workflow()
sys.exit(wf.run(main))