get rid of all urllib2 calls

This commit is contained in:
j 2014-10-05 20:06:22 +02:00
parent 1f14f6db55
commit dcc23ba2a4
4 changed files with 11 additions and 17 deletions

View file

@ -1,8 +1,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
import cookielib from six import StringIO, PY2
import urllib2 from six.moves import urllib
from StringIO import StringIO from six.moves import http_cookiejar as cookielib
from celery.utils import get_full_cls_name from celery.utils import get_full_cls_name
from celery.backends import default_backend from celery.backends import default_backend
@ -49,15 +49,15 @@ def api_proxy(request):
cj = SessionCookieJar() cj = SessionCookieJar()
if 'cj' in request.session: if 'cj' in request.session:
cj.load(request.session['cj']) cj.load(request.session['cj'])
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
opener.addheaders = [ opener.addheaders = [
('User-Agent', request.META.get('HTTP_USER_AGENT')) ('User-Agent', request.META.get('HTTP_USER_AGENT'))
] ]
form = ox.MultiPartForm() form = ox.MultiPartForm()
for key in request.POST: for key in request.POST:
form.add_field(key, request.POST[key]) form.add_field(key, request.POST[key])
r = urllib2.Request(url) r = urllib.request.Request(url)
body = str(form) body = form.body()
r.add_header('Content-type', form.get_content_type()) r.add_header('Content-type', form.get_content_type())
r.add_header('Content-length', len(body)) r.add_header('Content-length', len(body))
r.add_data(body) r.add_data(body)

View file

@ -68,7 +68,7 @@ class MultiPartForm(object):
return body return body
def body(self): 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 # Build a list of lists, each containing "lines" of the
# request. Each part is separated by a boundary string. # request. Each part is separated by a boundary string.
# Once the list is built, return a string where each # Once the list is built, return a string where each

View file

@ -7,12 +7,6 @@ from ox import find_re, strip_tags
def get_url(id=None, imdb=None): 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: if imdb:
url = "http://www.rottentomatoes.com/alias?type=imdbid&s=%s" % imdb url = "http://www.rottentomatoes.com/alias?type=imdbid&s=%s" % imdb
data = read_url(url) data = read_url(url)

View file

@ -1,8 +1,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
from urllib import quote, unquote_plus from six.moves.urllib.parse import quote, unquote_plus
import urllib2 from six.moves import urllib
import cookielib from six.moves import http_cookiejar as cookielib
import re import re
from xml.dom.minidom import parseString from xml.dom.minidom import parseString
import json import json
@ -167,7 +167,7 @@ def download_webm(id, filename):
stream_type = 'video/webm' stream_type = 'video/webm'
url = "http://www.youtube.com/watch?v=%s" % id url = "http://www.youtube.com/watch?v=%s" % id
cj = cookielib.CookieJar() cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
opener.addheaders = [ opener.addheaders = [
('User-Agent', ('User-Agent',
'Mozilla/5.0 (X11; Linux i686; rv:2.0) Gecko/20100101 Firefox/4.0'), 'Mozilla/5.0 (X11; Linux i686; rv:2.0) Gecko/20100101 Firefox/4.0'),