From 4c9b541facdd5d6bc0d0ec8cf9bd30c48cc0e464 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sun, 15 Apr 2012 04:05:22 -0400 Subject: [PATCH] Properly urlencode when we're dealing with unicode objects --- earwigbot/tasks/afc_statistics.py | 2 +- earwigbot/wiki/site.py | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/earwigbot/tasks/afc_statistics.py b/earwigbot/tasks/afc_statistics.py index 79c7706..b49af99 100644 --- a/earwigbot/tasks/afc_statistics.py +++ b/earwigbot/tasks/afc_statistics.py @@ -614,7 +614,7 @@ class Task(BaseTask): returned if we cannot determine when the page was "special"-ed, or if it was "special"-ed more than 100 edits ago. """ - if chart ==self.CHART_NONE: + if chart == self.CHART_NONE: return None, None, None elif chart == self.CHART_MISPLACE: return self.get_create(pageid) diff --git a/earwigbot/wiki/site.py b/earwigbot/wiki/site.py index 2675ed0..a0dbc61 100644 --- a/earwigbot/wiki/site.py +++ b/earwigbot/wiki/site.py @@ -29,7 +29,7 @@ from re import escape as re_escape, match as re_match from StringIO import StringIO from threading import Lock from time import sleep, time -from urllib import unquote_plus, urlencode +from urllib import quote_plus from urllib2 import build_opener, HTTPCookieProcessor, URLError from urlparse import urlparse @@ -173,6 +173,16 @@ class Site(object): return res.format(self.name(), self.project(), self.lang(), self.domain()) + def _urlencode_utf8(self, params): + """Implement urllib.urlencode(params) with support for unicode input.""" + enc = lambda s: s.encode("utf8") if isinstance(s, unicode) else str(s) + args = [] + for key, val in params.iteritems(): + key = quote_plus(enc(key)) + val = quote_plus(enc(val)) + args.append(key + "=" + val) + return "&".join(args) + def _api_query(self, params, tries=0, wait=5): """Do an API query with `params` as a dict of parameters. @@ -250,7 +260,7 @@ class Site(object): if self._maxlag: # If requested, don't overload the servers params["maxlag"] = self._maxlag - data = urlencode(params) + data = self._urlencode_utf8(params) return url, data def _handle_api_query_result(self, result, params, tries, wait):