From dcc23ba2a428ae7ad5eef28ca50df44bc65b5d12 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sun, 5 Oct 2014 20:06:22 +0200 Subject: [PATCH] get rid of all urllib2 calls --- ox/django/views.py | 12 ++++++------ ox/form.py | 2 +- ox/web/rottentomatoes.py | 6 ------ ox/web/youtube.py | 8 ++++---- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/ox/django/views.py b/ox/django/views.py index 9c925b9..24a20d6 100644 --- a/ox/django/views.py +++ b/ox/django/views.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- # vi:si:et:sw=4:sts=4:ts=4 -import cookielib -import urllib2 -from StringIO import StringIO +from six import StringIO, PY2 +from six.moves import urllib +from six.moves import http_cookiejar as cookielib from celery.utils import get_full_cls_name from celery.backends import default_backend @@ -49,15 +49,15 @@ def api_proxy(request): cj = SessionCookieJar() if 'cj' in request.session: cj.load(request.session['cj']) - opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) + opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) opener.addheaders = [ ('User-Agent', request.META.get('HTTP_USER_AGENT')) ] form = ox.MultiPartForm() for key in request.POST: form.add_field(key, request.POST[key]) - r = urllib2.Request(url) - body = str(form) + r = urllib.request.Request(url) + body = form.body() r.add_header('Content-type', form.get_content_type()) r.add_header('Content-length', len(body)) r.add_data(body) diff --git a/ox/form.py b/ox/form.py index 8969e03..d9fe66d 100644 --- a/ox/form.py +++ b/ox/form.py @@ -68,7 +68,7 @@ class MultiPartForm(object): return body def body(self): - """Return a string representing the form data, including attached files.""" + """Return a byte string representing the form data, including attached files.""" # Build a list of lists, each containing "lines" of the # request. Each part is separated by a boundary string. # Once the list is built, return a string where each diff --git a/ox/web/rottentomatoes.py b/ox/web/rottentomatoes.py index 8c89fd8..fd3265d 100644 --- a/ox/web/rottentomatoes.py +++ b/ox/web/rottentomatoes.py @@ -7,12 +7,6 @@ from ox import find_re, strip_tags def get_url(id=None, imdb=None): - #this would also wor but does not cache: - ''' - from urllib2 import urlopen - u = urlopen(url) - return u.url - ''' if imdb: url = "http://www.rottentomatoes.com/alias?type=imdbid&s=%s" % imdb data = read_url(url) diff --git a/ox/web/youtube.py b/ox/web/youtube.py index 5ac9ee2..7268598 100644 --- a/ox/web/youtube.py +++ b/ox/web/youtube.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- # vi:si:et:sw=4:sts=4:ts=4 -from urllib import quote, unquote_plus -import urllib2 -import cookielib +from six.moves.urllib.parse import quote, unquote_plus +from six.moves import urllib +from six.moves import http_cookiejar as cookielib import re from xml.dom.minidom import parseString import json @@ -167,7 +167,7 @@ def download_webm(id, filename): stream_type = 'video/webm' url = "http://www.youtube.com/watch?v=%s" % id cj = cookielib.CookieJar() - opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) + opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) opener.addheaders = [ ('User-Agent', 'Mozilla/5.0 (X11; Linux i686; rv:2.0) Gecko/20100101 Firefox/4.0'),