Merge lp:~stefanor/ibid/json-service-api into lp:~ibid-core/ibid/old-trunk-pack-0.92

Proposed by Stefano Rivera
Status: Merged
Approved by: Stefano Rivera
Approved revision: 674
Merged at revision: not available
Proposed branch: lp:~stefanor/ibid/json-service-api
Merge into: lp:~ibid-core/ibid/old-trunk-pack-0.92
Diff against target: None lines
To merge this branch: bzr merge lp:~stefanor/ibid/json-service-api
Reviewer Review Type Date Requested Status
Michael Gorven Approve
Jonathan Hitchcock Approve
Review via email: mp+7976@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Jonathan Hitchcock (vhata) :
review: Approve
Revision history for this message
Jonathan Hitchcock (vhata) wrote :

Although, of course, we don't catch that JSONException anywhere, and deal with it, yet...

Revision history for this message
Michael Gorven (mgorven) wrote :

 review approve

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'ibid/plugins/google.py'
--- ibid/plugins/google.py 2009-06-03 12:40:00 +0000
+++ ibid/plugins/google.py 2009-06-27 23:44:51 +0000
@@ -2,7 +2,6 @@
2import htmlentitydefs2import htmlentitydefs
3from httplib import BadStatusLine3from httplib import BadStatusLine
4import re4import re
5import simplejson
6from urllib import quote5from urllib import quote
7from urllib2 import urlopen, Request6from urllib2 import urlopen, Request
8from urlparse import urlparse7from urlparse import urlparse
@@ -11,7 +10,7 @@
1110
12from ibid.plugins import Processor, match11from ibid.plugins import Processor, match
13from ibid.config import Option12from ibid.config import Option
14from ibid.utils import decode_htmlentities, ibid_version13from ibid.utils import decode_htmlentities, ibid_version, json_webservice
1514
16help = {'google': u'Retrieves results from Google and Google Calculator.'}15help = {'google': u'Retrieves results from Google and Google Calculator.'}
1716
@@ -27,22 +26,20 @@
27 api_key = Option('api_key', 'Your Google API Key (optional)', None)26 api_key = Option('api_key', 'Your Google API Key (optional)', None)
28 referrer = Option('referrer', 'The referrer string to use (API searches)', default_referrer)27 referrer = Option('referrer', 'The referrer string to use (API searches)', default_referrer)
2928
30 google_api_url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=%s"
31
32 def _google_api_search(self, query, resultsize="large"):29 def _google_api_search(self, query, resultsize="large"):
33 url = self.google_api_url % quote(query)30 params = {
34 url += "&rsz=%s" % resultsize31 'v': '1.0',
32 'q': query,
33 'rsz': resultsize,
34 }
35 if self.api_key:35 if self.api_key:
36 url += '&key=%s' % quote(key)36 params['key'] = self.api_key
37 req = Request(url, headers={37
38 headers={
38 'user-agent': "Ibid/%s" % ibid_version() or "dev",39 'user-agent': "Ibid/%s" % ibid_version() or "dev",
39 'referrer': self.referrer,40 'referrer': self.referrer,
40 })41 }
41 f = urlopen(req)42 return json_webservice('http://ajax.googleapis.com/ajax/services/search/web', params, headers)
42 result = f.read()
43 f.close()
44 result = simplejson.loads(result)
45 return result
4643
47 @match(r'^google\s+(?:for\s+)?(.+?)$')44 @match(r'^google\s+(?:for\s+)?(.+?)$')
48 def search(self, event, query):45 def search(self, event, query):
4946
=== modified file 'ibid/plugins/lookup.py'
--- ibid/plugins/lookup.py 2009-06-26 15:05:19 +0000
+++ ibid/plugins/lookup.py 2009-06-27 23:44:51 +0000
@@ -5,7 +5,6 @@
5from time import time5from time import time
6from datetime import datetime6from datetime import datetime
7from random import choice7from random import choice
8from simplejson import loads
9from xml.etree.cElementTree import parse8from xml.etree.cElementTree import parse
10from .. math import acos, sin, cos, radians9from .. math import acos, sin, cos, radians
11import re10import re
@@ -15,7 +14,8 @@
1514
16from ibid.plugins import Processor, match, handler15from ibid.plugins import Processor, match, handler
17from ibid.config import Option16from ibid.config import Option
18from ibid.utils import ago, decode_htmlentities, get_html_parse_tree, cacheable_download17from ibid.utils import ago, decode_htmlentities, get_html_parse_tree, \
18 cacheable_download, json_webservice, JSONException
1919
20log = logging.getLogger('plugins.lookup')20log = logging.getLogger('plugins.lookup')
2121
@@ -216,22 +216,27 @@
216 }216 }
217 services = Option('services', 'Micro blogging services', default)217 services = Option('services', 'Micro blogging services', default)
218218
219 class NoSuchUserException(Exception):
220 pass
221
219 def setup(self):222 def setup(self):
220 self.update.im_func.pattern = re.compile(r'^(%s)\s+(\d+)$' % '|'.join(self.services.keys()), re.I)223 self.update.im_func.pattern = re.compile(r'^(%s)\s+(\d+)$' % '|'.join(self.services.keys()), re.I)
221 self.latest.im_func.pattern = re.compile(r'^(?:latest|last)\s+(%s)\s+(?:update\s+)?(?:(?:by|from|for)\s+)?(\S+)$'224 self.latest.im_func.pattern = re.compile(r'^(?:latest|last)\s+(%s)\s+(?:update\s+)?(?:(?:by|from|for)\s+)?(\S+)$'
222 % '|'.join(self.services.keys()), re.I)225 % '|'.join(self.services.keys()), re.I)
223226
224 def remote_update(self, service, id):227 def remote_update(self, service, id):
225 f = urlopen('%sstatuses/show/%s.json' % (service['endpoint'], id))228 status = json_webservice('%sstatuses/show/%s.json' % (service['endpoint'], id))
226 status = loads(f.read())
227 f.close()
228229
229 return {'screen_name': status['user']['screen_name'], 'text': decode_htmlentities(status['text'])}230 return {'screen_name': status['user']['screen_name'], 'text': decode_htmlentities(status['text'])}
230231
231 def remote_latest(self, service, user):232 def remote_latest(self, service, user):
232 f = urlopen('%sstatuses/user_timeline/%s.json?count=1' % (service['endpoint'], user))233 statuses = json_webservice(
233 statuses = loads(f.read())234 '%sstatuses/user_timeline/%s.json' % (service['endpoint'], user.encode('utf-8')),
234 f.close()235 {'count': 1})
236
237 if not statuses:
238 raise self.NoSuchUserException(user)
239
235 latest = statuses[0]240 latest = statuses[0]
236241
237 if service['api'] == 'twitter':242 if service['api'] == 'twitter':
@@ -270,6 +275,8 @@
270 event.addresponse(u'No such %s', service['user'])275 event.addresponse(u'No such %s', service['user'])
271 else:276 else:
272 event.addresponse(u'I can only see the Fail Whale')277 event.addresponse(u'I can only see the Fail Whale')
278 except self.NoSuchUserException, e:
279 event.addresponse(u'No such %s', service['user'])
273280
274 @match(r'^https?://(?:www\.)?twitter\.com/[^/ ]+/statuse?s?/(\d+)$')281 @match(r'^https?://(?:www\.)?twitter\.com/[^/ ]+/statuse?s?/(\d+)$')
275 def twitter(self, event, id):282 def twitter(self, event, id):
@@ -555,8 +562,6 @@
555562
556 feature = 'distance'563 feature = 'distance'
557 564
558 geo_url = 'http://ws.geonames.org/searchJSON?'
559
560 default_unit_names = {565 default_unit_names = {
561 'km': "kilometres",566 'km': "kilometres",
562 'mi': "miles",567 'mi': "miles",
@@ -570,7 +575,7 @@
570 radius_values = Option('radius_values', 'Radius of the earth in the units in which to specify distances', default_radius_values)575 radius_values = Option('radius_values', 'Radius of the earth in the units in which to specify distances', default_radius_values)
571 576
572 def get_place_data(self, place, num):577 def get_place_data(self, place, num):
573 return loads(urlopen(self.geo_url + urlencode({'q': place.encode('utf-8'), 'maxRows': num})).read())578 return json_webservice('http://ws.geonames.org/searchJSON', {'q': place, 'maxRows': num})
574579
575 def get_place(self, place):580 def get_place(self, place):
576 js = self.get_place_data(place, 1)581 js = self.get_place_data(place, 1)
577582
=== modified file 'ibid/plugins/trac.py'
--- ibid/plugins/trac.py 2009-05-01 12:17:57 +0000
+++ ibid/plugins/trac.py 2009-06-27 23:44:51 +0000
@@ -1,5 +1,4 @@
1from datetime import datetime1from datetime import datetime
2from simplejson import loads
3import logging2import logging
43
5from sqlalchemy import Table, MetaData4from sqlalchemy import Table, MetaData
65
=== modified file 'ibid/utils.py'
--- ibid/utils.py 2009-03-19 08:05:41 +0000
+++ ibid/utils.py 2009-06-27 23:44:51 +0000
@@ -7,11 +7,11 @@
7import re7import re
8from StringIO import StringIO8from StringIO import StringIO
9import time9import time
10from urllib import urlencode
10import urllib211import urllib2
11import zlib12import zlib
1213
13from html5lib import HTMLParser, treebuilders14import simplejson
14
15from html5lib import HTMLParser, treebuilders15from html5lib import HTMLParser, treebuilders
16from xml.etree import cElementTree16from xml.etree import cElementTree
17from BeautifulSoup import BeautifulSoup17from BeautifulSoup import BeautifulSoup
@@ -162,4 +162,26 @@
162162
163 return parser.parse(data, encoding = encoding)163 return parser.parse(data, encoding = encoding)
164164
165class JSONException(Exception):
166 pass
167
168def json_webservice(url, params={}, headers={}):
169 "Request data from a JSON webservice, and deserialise"
170
171 for key in params:
172 if isinstance(params[key], unicode):
173 params[key] = params[key].encode('utf-8')
174
175 if params:
176 url += '?' + urlencode(params)
177
178 req = urllib2.Request(url, headers=headers)
179 f = urllib2.urlopen(req)
180 data = f.read()
181 f.close()
182 try:
183 return simplejson.loads(data)
184 except ValueError, e:
185 raise JSONException(e)
186
165# vi: set et sta sw=4 ts=4:187# vi: set et sta sw=4 ts=4:

Subscribers

People subscribed via source and target branches