Permalink
Browse files

Importing just urlopen from urllib2 now

  • Loading branch information...
1 parent 54e21e9 commit 543903b2097545939284f46e4956dc1bfef0ed90 @zachwill committed May 9, 2011
Showing with 14 additions and 8 deletions.
  1. +10 −4 rottentomatoes/rottentomatoes.py
  2. +4 −4 test.py
@@ -6,14 +6,20 @@
Main file for interacting with the Rotten Tomatoes API.
"""
-import urllib2
from urllib import urlencode
+
try:
import json
except ImportError: # pragma: no cover
# For older versions of Python.
import simplejson as json
+try:
+ from urllib2 import urlopen
+except ImportError: # pragma: no cover
+ # For Python 3.
+ from urllib.request import urlopen
+
from rottentomatoes_api_key import API_KEY
@@ -56,7 +62,7 @@ def search(self, query, datatype='movies', **kwargs):
search_url = [self.movie_url, '?']
kwargs.update({'apikey': self.api_key, 'q': query})
search_url.append(urlencode(kwargs))
- data = json.loads(urllib2.urlopen(''.join(search_url)).read())
+ data = json.loads(urlopen(''.join(search_url)).read())
return data[datatype]
def lists(self, directory=None, sub=None, **kwargs):
@@ -78,7 +84,7 @@ def lists(self, directory=None, sub=None, **kwargs):
lists_url.append('/%s' % directory)
kwargs.update({'apikey': self.api_key})
lists_url.extend(['.json?', urlencode(kwargs)])
- data = json.loads(urllib2.urlopen(''.join(lists_url)).read())
+ data = json.loads(urlopen(''.join(lists_url)).read())
return data
def info(self, id_num, specific_info=None):
@@ -99,7 +105,7 @@ def info(self, id_num, specific_info=None):
movie_url.append('/%s' % specific_info)
end_of_url = ['.json?', urlencode({'apikey': self.api_key})]
movie_url.extend(end_of_url)
- data = json.loads(urllib2.urlopen(''.join(movie_url)).read())
+ data = json.loads(urlopen(''.join(movie_url)).read())
return data
def new(self, kind='movies', **kwargs):
View
@@ -16,18 +16,18 @@
def set_up():
"""
- Mock both urllib2 and json.loads' return value. Makes for fast unit tests.
+ Mock both and json.loads' return value. Makes for fast unit tests.
"""
- rottentomatoes.urllib2.urlopen = Mock()
+ rottentomatoes.urlopen = Mock()
movies_dict = {'movies': ['first_result', 'second_result'],
'total': 2}
rottentomatoes.json.loads = Mock(return_value=movies_dict)
rottentomatoes.API_KEY = 'my_api_key'
def call_args(kind='query'):
- """Find out what urllib2.urlopen called while mocking."""
- call = rottentomatoes.urllib2.urlopen.call_args[0][0]
+ """Find out what urlopen called while mocking."""
+ call = rottentomatoes.urlopen.call_args[0][0]
parsed_call = urlparse(call)
if kind == 'query':
return parse_qs(parsed_call.query)

0 comments on commit 543903b

Please sign in to comment.