i have been with Leo for a short time, but i realized that Leo is very 
powerful than what i can imagine.
with an need, i really hope someone could share the highlights to make it 
possible.  very appreciated!

I had tried to figure out this function, so i wrote a small button to work 
with google dict api. let me share the code as below. hope to get some 
response/suggestions to improve this. I assume there must be a better 
approach to fulfill this function.   :) don't laugh at my poor code, as i 
am just new to python and Leo. 

>>>>>>>>>code to work with google dictionary>>>>>>>>

# -*- coding: utf8 -*- 
@language python

# 
http://www.jsmix.com/blog/others/google-dictionary-api-and-voice-library.html
g.redirectStderr() # Redirect stderr to the current log pane.
g.redirectStdout() # Redirect stdout to the current log pane.

import codecs
import socket
import urllib2

timeout = 5 # timeout en seconds
socket.setdefaulttimeout(timeout)
w = c.frame.body.bodyCtrl # Leo's body pane.
urlStr_1 = 
'http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q='
urlStr_2 = w.getSelectedText().strip().lower().replace(' ','_') # Return 
selected text if any. default = ''
urlStr_3 = '&sl=en&tl=en&restrict=pr%2Cde&client=te'

# initialize the str in case no data from google

g_query = urlStr_2
g_meaning_1st = 'NO-meaning'
g_type = 'NO-type'
g_mp3url= 'NO-mp3'
g_phonetic = 'NO-phonetic'

try:
    
    assert len(urlStr_2) > 1
                #get dict html data from google, it is a JSON
    req = urllib2.Request(urlStr_1 + urlStr_2 + urlStr_3)
    handle = urllib2.urlopen(req)
    webStr = handle.read()
                #webStr = urlopen(urlStr_1 + urlStr_2 + urlStr_3, 
timeout=5).read()
                # convert str to a dict obj
    dictinfo = eval(webStr[25:-10])
                #dict.keys()=['query', 'webDefinitions', 'targetLanguage', 
'primaries', 'sourceLanguage']
                #dictinfo['primaries'][0] #dict.keys()= ['type', 'terms', 
'entries']
    
    g_query = dictinfo['primaries'][0]['terms'][0]['text'] 
    g_meaning_1st = 
dictinfo['primaries'][0]['entries'][1]['terms'][0]['text']
    g_type  = dictinfo['primaries'][0]['terms'][0]['labels'][0]['text']
    g_mp3url= dictinfo['primaries'][0]['terms'][2]['text'] 
    g_phonetic = dictinfo['primaries'][0]['terms'][1]['text'] 

except AssertionError:
    g.es('No Text Selected!',color='red',tabName='Dict')
except TypeError:
    g.es('webStr is abnormal!',color='red',tabName='Dict')
except IndexError:
    g.es('No mp3/phonetic!',color='red',tabName='Dict')
except SyntaxError:
    g.es('Nothing from Google, or illegal 
query!',color='red',tabName='Dict')
except KeyError:
    g.es('No definition found!',color='red',tabName='Dict')

except:
    raise

else:
    Body = g_mp3url + '\n\n' + g_query + ' | ' + g_type + ' ' + g_phonetic 
+ '\n\n' + g_meaning_1st #define output format
    
    for p in c.all_unique_positions():
        if p.h == '@button DictGoogle @keys Alt-d':
            child = p.insertAsLastChild()
            child.h = g_query + ' | ' + g_type 
            child.b = Body.decode("utf-8")
            c.redraw()
            break

finally:
    Body = g_mp3url + '\n\n' + g_query + ' | ' + g_type + ' ' + g_phonetic 
+ '\n\n' + g_meaning_1st
    c.frame.log.selectTab('Dict')
    g.es('>>>',tabName='Dict')
    g.es(Body.decode("utf-8"),color='blue',tabName='Dict')
    

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/leo-editor?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to