Browse Source

Properly urlencode when we're dealing with unicode objects

tags/v0.1^2
Ben Kurtovic 12 years ago
parent
commit
4c9b541fac
2 changed files with 13 additions and 3 deletions
  1. +1
    -1
      earwigbot/tasks/afc_statistics.py
  2. +12
    -2
      earwigbot/wiki/site.py

+ 1
- 1
earwigbot/tasks/afc_statistics.py View File

@@ -614,7 +614,7 @@ class Task(BaseTask):
returned if we cannot determine when the page was "special"-ed, or if returned if we cannot determine when the page was "special"-ed, or if
it was "special"-ed more than 100 edits ago. it was "special"-ed more than 100 edits ago.
""" """
if chart ==self.CHART_NONE:
if chart == self.CHART_NONE:
return None, None, None return None, None, None
elif chart == self.CHART_MISPLACE: elif chart == self.CHART_MISPLACE:
return self.get_create(pageid) return self.get_create(pageid)


+ 12
- 2
earwigbot/wiki/site.py View File

@@ -29,7 +29,7 @@ from re import escape as re_escape, match as re_match
from StringIO import StringIO from StringIO import StringIO
from threading import Lock from threading import Lock
from time import sleep, time from time import sleep, time
from urllib import unquote_plus, urlencode
from urllib import quote_plus
from urllib2 import build_opener, HTTPCookieProcessor, URLError from urllib2 import build_opener, HTTPCookieProcessor, URLError
from urlparse import urlparse from urlparse import urlparse


@@ -173,6 +173,16 @@ class Site(object):
return res.format(self.name(), self.project(), self.lang(), return res.format(self.name(), self.project(), self.lang(),
self.domain()) 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): def _api_query(self, params, tries=0, wait=5):
"""Do an API query with `params` as a dict of parameters. """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 if self._maxlag: # If requested, don't overload the servers
params["maxlag"] = self._maxlag params["maxlag"] = self._maxlag


data = urlencode(params)
data = self._urlencode_utf8(params)
return url, data return url, data


def _handle_api_query_result(self, result, params, tries, wait): def _handle_api_query_result(self, result, params, tries, wait):


Loading…
Cancel
Save