Merge lp:~stefanor/ibid/microblogging-345112 into lp:~ibid-core/ibid/old-trunk-pack-0.92

Proposed by Stefano Rivera
Status: Merged
Approved by: Jonathan Hitchcock
Approved revision: 591
Merged at revision: 588
Proposed branch: lp:~stefanor/ibid/microblogging-345112
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/microblogging-345112
Reviewer Review Type Date Requested Status
Jonathan Hitchcock Approve
Michael Gorven Approve
Review via email: mp+4657@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Stefano Rivera (stefanor) wrote :

Does some other things that I came across while fixing the microblogging bug

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

 review approve

review: Approve
Revision history for this message
Jonathan Hitchcock (vhata) :
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-03-09 19:26:27 +0000
+++ ibid/plugins/google.py 2009-03-19 08:15:15 +0000
@@ -1,29 +1,22 @@
1from cgi import parse_qs
1import htmlentitydefs2import htmlentitydefs
2import re3import re
3import simplejson4import simplejson
4from urllib import quote5from urllib import quote
5from urllib2 import urlopen, Request6from urllib2 import urlopen, Request
7from urlparse import urlparse
68
7from BeautifulSoup import BeautifulSoup9from BeautifulSoup import BeautifulSoup
810
9from ibid.plugins import Processor, match11from ibid.plugins import Processor, match
10from ibid.config import Option12from ibid.config import Option
11from ibid.utils import ibid_version13from ibid.utils import decode_htmlentities, ibid_version
1214
13help = {'google': u'Retrieves results from Google and Google Calculator.'}15help = {'google': u'Retrieves results from Google and Google Calculator.'}
1416
15default_user_agent = 'Mozilla/5.0'17default_user_agent = 'Mozilla/5.0'
16default_referrer = "http://ibid.omnia.za.net/"18default_referrer = "http://ibid.omnia.za.net/"
1719
18def de_entity(text):
19 "Remove HTML entities, and replace with their characters"
20 replace = lambda match: unichr(int(match.group(1)))
21 text = re.sub("&#(\d+);", replace, text)
22
23 replace = lambda match: unichr(htmlentitydefs.name2codepoint[match.group(1)])
24 text = re.sub("&(\w+);", replace, text)
25 return text
26
27class GoogleAPISearch(Processor):20class GoogleAPISearch(Processor):
28 u"""google [for] <term>21 u"""google [for] <term>
29 googlefight [for] <term> and <term>"""22 googlefight [for] <term> and <term>"""
@@ -58,7 +51,7 @@
5851
59 title = item["titleNoFormatting"]52 title = item["titleNoFormatting"]
6053
61 results.append(u'"%s" %s' % (de_entity(title), item["unescapedUrl"]))54 results.append(u'"%s" %s' % (decode_htmlentities(title), item["unescapedUrl"]))
62 55
63 if results:56 if results:
64 event.addresponse(u'%s', u', '.join(results))57 event.addresponse(u'%s', u', '.join(results))
@@ -120,7 +113,7 @@
120113
121 definitions = []114 definitions = []
122 for li in soup.findAll('li'):115 for li in soup.findAll('li'):
123 definitions.append(de_entity(li.contents[0].strip()))116 definitions.append(decode_htmlentities(li.contents[0].strip()))
124117
125 if definitions:118 if definitions:
126 event.addresponse(u'%s', u' :: '.join(definitions))119 event.addresponse(u'%s', u' :: '.join(definitions))
@@ -137,10 +130,12 @@
137 for item in items:130 for item in items:
138 try:131 try:
139 url = item.a['href']132 url = item.a['href']
133 if url.startswith(u"/aclk?"):
134 url = parse_qs(urlparse(url).query)['q'][0]
140 title = u''.join([e.string for e in item.a.contents])135 title = u''.join([e.string for e in item.a.contents])
141 if title.startswith("Image results for"):136 if title.startswith("Image results for"):
142 continue137 continue
143 results.append(u'"%s" %s' % (de_entity(title), url))138 results.append(u'"%s" %s' % (decode_htmlentities(title), url))
144 except Exception:139 except Exception:
145 pass140 pass
146 if len(results) >= 8:141 if len(results) >= 8:
147142
=== modified file 'ibid/plugins/lookup.py'
--- ibid/plugins/lookup.py 2009-03-18 11:36:41 +0000
+++ ibid/plugins/lookup.py 2009-03-19 08:16:53 +0000
@@ -155,7 +155,7 @@
155 status = loads(f.read())155 status = loads(f.read())
156 f.close()156 f.close()
157157
158 return {'screen_name': status['user']['screen_name'], 'text': status['text']}158 return {'screen_name': status['user']['screen_name'], 'text': decode_htmlentities(status['text'])}
159159
160 def remote_latest(self, service, user):160 def remote_latest(self, service, user):
161 service_url = self.services[service]161 service_url = self.services[service]
@@ -170,7 +170,7 @@
170 url = "%s/notice/%i" % (service_url[:-5], latest["id"])170 url = "%s/notice/%i" % (service_url[:-5], latest["id"])
171171
172 return {172 return {
173 'text': latest['text'],173 'text': decode_htmlentities(latest['text']),
174 'ago': ago(datetime.utcnow() - datetime.strptime(latest["created_at"], '%a %b %d %H:%M:%S +0000 %Y'), 1),174 'ago': ago(datetime.utcnow() - datetime.strptime(latest["created_at"], '%a %b %d %H:%M:%S +0000 %Y'), 1),
175 'url': url,175 'url': url,
176 }176 }
177177
=== modified file 'ibid/utils.py'
--- ibid/utils.py 2009-03-10 10:45:47 +0000
+++ ibid/utils.py 2009-03-19 08:05:41 +0000
@@ -34,21 +34,13 @@
34 formatted = ' and '.join(parts)34 formatted = ' and '.join(parts)
35 return formatted.replace(' and ', ', ', len(parts)-2)35 return formatted.replace(' and ', ', ', len(parts)-2)
3636
37def substitute_entity(match):37def decode_htmlentities(text):
38 ent = match.group(2)38 replace = lambda match: unichr(int(match.group(1)))
39 if match.group(1) == "#":39 text = re.sub("&#(\d+);", replace, text)
40 return unichr(int(ent))40
41 else:41 replace = lambda match: match.group(1) in name2codepoint and unichr(name2codepoint[match.group(1)]) or match.group(0)
42 cp = name2codepoint.get(ent)42 text = re.sub("&(\w+);", replace, text)
4343 return text
44 if cp:
45 return unichr(cp)
46 else:
47 return match.group()
48
49def decode_htmlentities(string):
50 entity_re = re.compile("&(#?)(\d{1,5}|\w{1,8});")
51 return entity_re.subn(substitute_entity, string)[0]
5244
53def cacheable_download(url, cachefile):45def cacheable_download(url, cachefile):
54 """Download url to cachefile if it's modified since cachefile.46 """Download url to cachefile if it's modified since cachefile.

Subscribers

People subscribed via source and target branches