Kaynağa Gözat

Properly encode URL for the search engine.

tags/v0.2
Ben Kurtovic 12 yıl önce
ebeveyn
işleme
5677664476
1 değiştirilmiş dosya ile 10 ekleme ve 2 silme
  1. +10
    -2
      earwigbot/wiki/copyvios/search.py

+ 10
- 2
earwigbot/wiki/copyvios/search.py Dosyayı Görüntüle

@@ -23,6 +23,7 @@
from gzip import GzipFile from gzip import GzipFile
from json import loads from json import loads
from StringIO import StringIO from StringIO import StringIO
from urllib import quote


from earwigbot import importer from earwigbot import importer
from earwigbot.exceptions import SearchQueryError from earwigbot.exceptions import SearchQueryError
@@ -60,6 +61,13 @@ class YahooBOSSSearchEngine(BaseSearchEngine):
"""A search engine interface with Yahoo! BOSS.""" """A search engine interface with Yahoo! BOSS."""
name = "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): def search(self, query):
"""Do a Yahoo! BOSS web search for *query*. """Do a Yahoo! BOSS web search for *query*.


@@ -76,13 +84,13 @@ class YahooBOSSSearchEngine(BaseSearchEngine):
"oauth_nonce": oauth.generate_nonce(), "oauth_nonce": oauth.generate_nonce(),
"oauth_timestamp": oauth.Request.make_timestamp(), "oauth_timestamp": oauth.Request.make_timestamp(),
"oauth_consumer_key": consumer.key, "oauth_consumer_key": consumer.key,
"q": query.encode("utf8"), "count": 5,
"q": query.encode("utf8"), "count": "5",
"type": "html,text", "format": "json", "type": "html,text", "format": "json",
} }


req = oauth.Request(method="GET", url=url, parameters=params) req = oauth.Request(method="GET", url=url, parameters=params)
req.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, None) 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() result = response.read()


if response.headers.get("Content-Encoding") == "gzip": if response.headers.get("Content-Encoding") == "gzip":


Yükleniyor…
İptal
Kaydet