From 5677664476f14b251ca91b29e084def486288781 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Mon, 18 Aug 2014 16:39:34 -0400 Subject: [PATCH] Properly encode URL for the search engine. --- earwigbot/wiki/copyvios/search.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/earwigbot/wiki/copyvios/search.py b/earwigbot/wiki/copyvios/search.py index 57e7733..0b29be7 100644 --- a/earwigbot/wiki/copyvios/search.py +++ b/earwigbot/wiki/copyvios/search.py @@ -23,6 +23,7 @@ from gzip import GzipFile from json import loads from StringIO import StringIO +from urllib import quote from earwigbot import importer from earwigbot.exceptions import SearchQueryError @@ -60,6 +61,13 @@ class YahooBOSSSearchEngine(BaseSearchEngine): """A search engine interface with Yahoo! BOSS.""" name = "Yahoo! BOSS" + @staticmethod + def _build_url(base, params): + """Works like urllib.urlencode(), but uses %20 for spaces over +.""" + enc = lambda s: quote(s, safe="") + args = ["=".join((enc(k), enc(v))) for k, v in params.iteritems()] + return base + "?" + "&".join(args) + def search(self, query): """Do a Yahoo! BOSS web search for *query*. @@ -76,13 +84,13 @@ class YahooBOSSSearchEngine(BaseSearchEngine): "oauth_nonce": oauth.generate_nonce(), "oauth_timestamp": oauth.Request.make_timestamp(), "oauth_consumer_key": consumer.key, - "q": query.encode("utf8"), "count": 5, + "q": query.encode("utf8"), "count": "5", "type": "html,text", "format": "json", } req = oauth.Request(method="GET", url=url, parameters=params) req.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, None) - response = self.opener.open(req.to_url()) + response = self.opener.open(self._build_url(url, req)) result = response.read() if response.headers.get("Content-Encoding") == "gzip":