use py2.6 json module instead of yaml as a json decoder
This commit is contained in:
parent
f204e42197
commit
90cb5bf47d
|
@ -1,8 +1,9 @@
|
||||||
import urllib
|
import urllib
|
||||||
import htmlentitydefs
|
import htmlentitydefs
|
||||||
import re
|
import re
|
||||||
|
import json
|
||||||
|
|
||||||
from util import hook, yaml
|
from util import hook
|
||||||
|
|
||||||
########### from http://effbot.org/zone/re-sub.htm#unescape-html #############
|
########### from http://effbot.org/zone/re-sub.htm#unescape-html #############
|
||||||
|
|
||||||
|
@ -39,8 +40,8 @@ def goog_trans(text, slang, tlang):
|
||||||
req_url = 'http://ajax.googleapis.com/ajax/services/language/translate' \
|
req_url = 'http://ajax.googleapis.com/ajax/services/language/translate' \
|
||||||
'?v=1.0&q=%s&langpair=%s'
|
'?v=1.0&q=%s&langpair=%s'
|
||||||
url = req_url % (urllib.quote(text, safe=''), slang + '%7C' + tlang)
|
url = req_url % (urllib.quote(text, safe=''), slang + '%7C' + tlang)
|
||||||
json = urllib.urlopen(url).read()
|
page = urllib.urlopen(url).read()
|
||||||
parsed = yaml.load(json)
|
parsed = json.loads(page)
|
||||||
if not 200 <= parsed['responseStatus'] < 300:
|
if not 200 <= parsed['responseStatus'] < 300:
|
||||||
raise IOError('error with the translation server: %d: %s' % (
|
raise IOError('error with the translation server: %d: %s' % (
|
||||||
parsed['responseStatus'], ''))
|
parsed['responseStatus'], ''))
|
||||||
|
|
|
@ -1,16 +1,17 @@
|
||||||
import urllib
|
import urllib
|
||||||
import random
|
import random
|
||||||
from lxml import html
|
from lxml import html
|
||||||
|
import json
|
||||||
|
|
||||||
from util import hook, yaml
|
from util import hook
|
||||||
|
|
||||||
|
|
||||||
def api_get(kind, query):
|
def api_get(kind, query):
|
||||||
req_url = 'http://ajax.googleapis.com/ajax/services/search/%s?' \
|
req_url = 'http://ajax.googleapis.com/ajax/services/search/%s?' \
|
||||||
'v=1.0&safe=off&q=%s'
|
'v=1.0&safe=off&q=%s'
|
||||||
url = req_url % (kind, urllib.quote(query, safe=''))
|
url = req_url % (kind, urllib.quote(query, safe=''))
|
||||||
json = urllib.urlopen(url).read()
|
page = urllib.urlopen(url).read()
|
||||||
return yaml.load(json)
|
return json.loads(page)
|
||||||
|
|
||||||
|
|
||||||
@hook.command
|
@hook.command
|
||||||
|
|
|
@ -2,8 +2,9 @@ import random
|
||||||
import urllib
|
import urllib
|
||||||
import urllib2
|
import urllib2
|
||||||
import re
|
import re
|
||||||
|
import json
|
||||||
|
|
||||||
from util import hook, yaml
|
from util import hook
|
||||||
|
|
||||||
@hook.command
|
@hook.command
|
||||||
def suggest(inp):
|
def suggest(inp):
|
||||||
|
@ -21,9 +22,9 @@ def suggest(inp):
|
||||||
num = 0
|
num = 0
|
||||||
|
|
||||||
url = 'http://google.com/complete/search?q=' + urllib.quote(inp, safe='')
|
url = 'http://google.com/complete/search?q=' + urllib.quote(inp, safe='')
|
||||||
json = urllib2.urlopen(url).read()
|
page = urllib2.urlopen(url).read()
|
||||||
json = json[json.find('(') + 1: -1]
|
page_json = page.split('(', 1)[1][:-1]
|
||||||
suggestions = yaml.load(json)[1]
|
suggestions = json.loads(page_json)[1]
|
||||||
if not suggestions:
|
if not suggestions:
|
||||||
return 'no suggestions found'
|
return 'no suggestions found'
|
||||||
if num:
|
if num:
|
||||||
|
|
Loading…
Reference in New Issue