diff --git a/weboob/backends/gazelle/browser.py b/weboob/backends/gazelle/browser.py index c8bb9f30..2904064d 100644 --- a/weboob/backends/gazelle/browser.py +++ b/weboob/backends/gazelle/browser.py @@ -22,7 +22,6 @@ from weboob.tools.browser import BaseBrowser from .pages.index import IndexPage, LoginPage from .pages.torrents import TorrentsPage -import urllib __all__ = ['GazelleBrowser'] @@ -61,7 +60,7 @@ class GazelleBrowser(BaseBrowser): return self.location('%s://%s/login.php' % (self.PROTOCOL, self.DOMAIN)) def iter_torrents(self, pattern): - self.location('/torrents.php?searchstr=%s' % urllib.quote_plus(pattern)) + self.location(self.buildurl('/torrents.php', searchstr=pattern)) assert self.is_on_page(TorrentsPage) return self.page.iter_torrents() diff --git a/weboob/backends/youporn/browser.py b/weboob/backends/youporn/browser.py index f803ff08..af924b2b 100644 --- a/weboob/backends/youporn/browser.py +++ b/weboob/backends/youporn/browser.py @@ -18,8 +18,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. """ -import urllib - from weboob.tools.browser import BaseBrowser from .pages.index import IndexPage @@ -47,7 +45,7 @@ class YoupornBrowser(BaseBrowser): if not pattern: self.home() else: - self.location('/search/%s?query=%s' % (sortby, urllib.quote_plus(pattern))) + self.location(self.buildurl('/search/%s' % sortby, query=pattern)) assert self.is_on_page(IndexPage) return self.page.iter_videos() diff --git a/weboob/tools/browser.py b/weboob/tools/browser.py index a09abead..52d37227 100644 --- a/weboob/tools/browser.py +++ b/weboob/tools/browser.py @@ -19,6 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. """ import mechanize +import urllib import urllib2 import ClientForm import re @@ -305,6 +306,27 @@ class BaseBrowser(mechanize.Browser): if self._cookie: self._cookie.save() + def buildurl(self, base, *args, **kwargs): + """ + Build an URL and escape arguments. + You can give a serie of tuples in *args (and the order is keept), or + a dict in **kwargs (but the order is lost). + + Example: + >>> buildurl('/blah.php', ('a', '&'), ('c', '=') + '/blah.php?a=%26&b=%3D' + >>> buildurl('/blah.php', a='&', 'c'='=') + '/blah.php?b=%3D&a=%26' + + """ + + if not args: + args = kwargs + if not args: + return base + else: + return '%s?%s' % (base, urllib.urlencode(args)) + def str(self, s): if isinstance(s, unicode): s = s.encode('iso-8859-15', 'replace')