Skip to content

Instantly share code, notes, and snippets.

@yuikns
Last active October 2, 2016 06:09
Show Gist options
  • Save yuikns/ffb2de9388a0059a46055e796655a097 to your computer and use it in GitHub Desktop.
Save yuikns/ffb2de9388a0059a46055e796655a097 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# encoding: utf-8
import sys
import argparse
from workflow import Workflow
#from workflow import Workflow3
from DictionaryServices import *
import urllib2
import urllib
import json
import re
def main(wf):
parser = argparse.ArgumentParser()
parser.add_argument('--query', dest='query', default='')
args = parser.parse_args(wf.args)
token=args.query.strip()#.replace(' ','%20').replace('?','%3F').replace('/','%2F')
searchword=token#.decode('utf-8')
wordrange = (0, len(searchword))
dictresult = DCSCopyTextDefinition(None, searchword, wordrange)
if not dictresult:
wf.add_item('No results')
wf.send_feedback()
else:
s = dictresult.encode("utf-8")
s = s.replace('\xe2\x96\xb6', "\n\xe2\x96\xb6 ") # arrow
s = s.replace('\xe2\x80\xa2', "\n\xe2\x80\xa2 ") # bullet
s = s.replace('PHRASES', "\n[P] ")
s = s.replace('DERIVATIVES', "\n[D] ")
s = s.replace('ORIGIN', "\n[O]")
result=[i for i in s.split('\n') if i]
for i in range(min(10,len(result))):
lid = 0
for chunk in chunkstring(result[i], 60):
if lid == 0 :
wf.add_item(chunk.decode('utf-8'))
lid = 1
else:
wf.add_item((" " + chunk).decode('utf-8'))
#wf.add_item(result[i].decode('utf-8'))
wf.send_feedback()
#result=dictresult.encode('utf-8').split('\n')
#for i in range(min(10,len(result))):
# wf.add_item(result[i].decode('utf-8'))
#wf.send_feedback()
return 0
def chunkstring(string, length):
return (string[0+i:length+i] for i in range(0, len(string), length))
if __name__==u"__main__":
wf=Workflow3()
#wf=Workflow()
sys.exit(wf.run(main))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment