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
1=== modified file 'ibid/plugins/google.py'
2--- ibid/plugins/google.py 2009-06-03 12:40:00 +0000
3+++ ibid/plugins/google.py 2009-06-27 23:44:51 +0000
4@@ -2,7 +2,6 @@
5 import htmlentitydefs
6 from httplib import BadStatusLine
7 import re
8-import simplejson
9 from urllib import quote
10 from urllib2 import urlopen, Request
11 from urlparse import urlparse
12@@ -11,7 +10,7 @@
13
14 from ibid.plugins import Processor, match
15 from ibid.config import Option
16-from ibid.utils import decode_htmlentities, ibid_version
17+from ibid.utils import decode_htmlentities, ibid_version, json_webservice
18
19 help = {'google': u'Retrieves results from Google and Google Calculator.'}
20
21@@ -27,22 +26,20 @@
22 api_key = Option('api_key', 'Your Google API Key (optional)', None)
23 referrer = Option('referrer', 'The referrer string to use (API searches)', default_referrer)
24
25- google_api_url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=%s"
26-
27 def _google_api_search(self, query, resultsize="large"):
28- url = self.google_api_url % quote(query)
29- url += "&rsz=%s" % resultsize
30+ params = {
31+ 'v': '1.0',
32+ 'q': query,
33+ 'rsz': resultsize,
34+ }
35 if self.api_key:
36- url += '&key=%s' % quote(key)
37- req = Request(url, headers={
38+ params['key'] = self.api_key
39+
40+ headers={
41 'user-agent': "Ibid/%s" % ibid_version() or "dev",
42 'referrer': self.referrer,
43- })
44- f = urlopen(req)
45- result = f.read()
46- f.close()
47- result = simplejson.loads(result)
48- return result
49+ }
50+ return json_webservice('http://ajax.googleapis.com/ajax/services/search/web', params, headers)
51
52 @match(r'^google\s+(?:for\s+)?(.+?)$')
53 def search(self, event, query):
54
55=== modified file 'ibid/plugins/lookup.py'
56--- ibid/plugins/lookup.py 2009-06-26 15:05:19 +0000
57+++ ibid/plugins/lookup.py 2009-06-27 23:44:51 +0000
58@@ -5,7 +5,6 @@
59 from time import time
60 from datetime import datetime
61 from random import choice
62-from simplejson import loads
63 from xml.etree.cElementTree import parse
64 from .. math import acos, sin, cos, radians
65 import re
66@@ -15,7 +14,8 @@
67
68 from ibid.plugins import Processor, match, handler
69 from ibid.config import Option
70-from ibid.utils import ago, decode_htmlentities, get_html_parse_tree, cacheable_download
71+from ibid.utils import ago, decode_htmlentities, get_html_parse_tree, \
72+ cacheable_download, json_webservice, JSONException
73
74 log = logging.getLogger('plugins.lookup')
75
76@@ -216,22 +216,27 @@
77 }
78 services = Option('services', 'Micro blogging services', default)
79
80+ class NoSuchUserException(Exception):
81+ pass
82+
83 def setup(self):
84 self.update.im_func.pattern = re.compile(r'^(%s)\s+(\d+)$' % '|'.join(self.services.keys()), re.I)
85 self.latest.im_func.pattern = re.compile(r'^(?:latest|last)\s+(%s)\s+(?:update\s+)?(?:(?:by|from|for)\s+)?(\S+)$'
86 % '|'.join(self.services.keys()), re.I)
87
88 def remote_update(self, service, id):
89- f = urlopen('%sstatuses/show/%s.json' % (service['endpoint'], id))
90- status = loads(f.read())
91- f.close()
92+ status = json_webservice('%sstatuses/show/%s.json' % (service['endpoint'], id))
93
94 return {'screen_name': status['user']['screen_name'], 'text': decode_htmlentities(status['text'])}
95
96 def remote_latest(self, service, user):
97- f = urlopen('%sstatuses/user_timeline/%s.json?count=1' % (service['endpoint'], user))
98- statuses = loads(f.read())
99- f.close()
100+ statuses = json_webservice(
101+ '%sstatuses/user_timeline/%s.json' % (service['endpoint'], user.encode('utf-8')),
102+ {'count': 1})
103+
104+ if not statuses:
105+ raise self.NoSuchUserException(user)
106+
107 latest = statuses[0]
108
109 if service['api'] == 'twitter':
110@@ -270,6 +275,8 @@
111 event.addresponse(u'No such %s', service['user'])
112 else:
113 event.addresponse(u'I can only see the Fail Whale')
114+ except self.NoSuchUserException, e:
115+ event.addresponse(u'No such %s', service['user'])
116
117 @match(r'^https?://(?:www\.)?twitter\.com/[^/ ]+/statuse?s?/(\d+)$')
118 def twitter(self, event, id):
119@@ -555,8 +562,6 @@
120
121 feature = 'distance'
122
123- geo_url = 'http://ws.geonames.org/searchJSON?'
124-
125 default_unit_names = {
126 'km': "kilometres",
127 'mi': "miles",
128@@ -570,7 +575,7 @@
129 radius_values = Option('radius_values', 'Radius of the earth in the units in which to specify distances', default_radius_values)
130
131 def get_place_data(self, place, num):
132- return loads(urlopen(self.geo_url + urlencode({'q': place.encode('utf-8'), 'maxRows': num})).read())
133+ return json_webservice('http://ws.geonames.org/searchJSON', {'q': place, 'maxRows': num})
134
135 def get_place(self, place):
136 js = self.get_place_data(place, 1)
137
138=== modified file 'ibid/plugins/trac.py'
139--- ibid/plugins/trac.py 2009-05-01 12:17:57 +0000
140+++ ibid/plugins/trac.py 2009-06-27 23:44:51 +0000
141@@ -1,5 +1,4 @@
142 from datetime import datetime
143-from simplejson import loads
144 import logging
145
146 from sqlalchemy import Table, MetaData
147
148=== modified file 'ibid/utils.py'
149--- ibid/utils.py 2009-03-19 08:05:41 +0000
150+++ ibid/utils.py 2009-06-27 23:44:51 +0000
151@@ -7,11 +7,11 @@
152 import re
153 from StringIO import StringIO
154 import time
155+from urllib import urlencode
156 import urllib2
157 import zlib
158
159-from html5lib import HTMLParser, treebuilders
160-
161+import simplejson
162 from html5lib import HTMLParser, treebuilders
163 from xml.etree import cElementTree
164 from BeautifulSoup import BeautifulSoup
165@@ -162,4 +162,26 @@
166
167 return parser.parse(data, encoding = encoding)
168
169+class JSONException(Exception):
170+ pass
171+
172+def json_webservice(url, params={}, headers={}):
173+ "Request data from a JSON webservice, and deserialise"
174+
175+ for key in params:
176+ if isinstance(params[key], unicode):
177+ params[key] = params[key].encode('utf-8')
178+
179+ if params:
180+ url += '?' + urlencode(params)
181+
182+ req = urllib2.Request(url, headers=headers)
183+ f = urllib2.urlopen(req)
184+ data = f.read()
185+ f.close()
186+ try:
187+ return simplejson.loads(data)
188+ except ValueError, e:
189+ raise JSONException(e)
190+
191 # vi: set et sta sw=4 ts=4:

Subscribers

People subscribed via source and target branches