1
1
from __future__ import unicode_literals
2
2
3
3
import itertools
4
- import json
5
4
import logging
6
5
import operator
7
- import urllib2
8
6
import urlparse
9
7
10
8
from mopidy import models
11
9
12
- from mopidy_spotify import utils
10
+ import requests
13
11
14
12
15
13
# NOTE: This module is independent of libspotify and built using the Spotify
25
23
logger = logging .getLogger (__name__ )
26
24
27
25
28
- def get_images (uris ):
26
+ def get_images (requests_session , uris ):
29
27
result = {}
30
28
uri_type_getter = operator .itemgetter ('type' )
31
29
uris = sorted ((_parse_uri (u ) for u in uris ), key = uri_type_getter )
@@ -37,9 +35,10 @@ def get_images(uris):
37
35
else :
38
36
batch .append (uri )
39
37
if len (batch ) >= _API_MAX_IDS_PER_REQUEST :
40
- result .update (_process_uris (uri_type , batch ))
38
+ result .update (
39
+ _process_uris (requests_session , uri_type , batch ))
41
40
batch = []
42
- result .update (_process_uris (uri_type , batch ))
41
+ result .update (_process_uris (requests_session , uri_type , batch ))
43
42
return result
44
43
45
44
@@ -60,20 +59,26 @@ def _parse_uri(uri):
60
59
raise ValueError ('Could not parse %r as a Spotify URI' % uri )
61
60
62
61
63
- def _process_uris (uri_type , uris ):
62
+ def _process_uris (requests_session , uri_type , uris ):
64
63
result = {}
64
+ ids = [u ['id' ] for u in uris ]
65
65
ids_to_uris = {u ['id' ]: u for u in uris }
66
66
67
67
if not uris :
68
68
return result
69
69
70
+ lookup_uri = _API_BASE_URI % (uri_type , ',' .join (ids ))
71
+
72
+ try :
73
+ response = requests_session .get (lookup_uri )
74
+ except requests .RequestException as exc :
75
+ logger .debug ('Fetching %s failed: %s' , lookup_uri , exc )
76
+ return result
77
+
70
78
try :
71
- lookup_uri = _API_BASE_URI % (
72
- uri_type , ',' .join (sorted (ids_to_uris .keys ())))
73
- data = json .load (urllib2 .urlopen (lookup_uri ))
74
- except (ValueError , IOError ) as e :
75
- error_msg = utils .locale_decode (e )
76
- logger .debug ('Fetching %s failed: %s' , lookup_uri , error_msg )
79
+ data = response .json ()
80
+ except ValueError as exc :
81
+ logger .debug ('JSON decoding failed for %s: %s' , lookup_uri , exc )
77
82
return result
78
83
79
84
for item in data .get (uri_type + 's' , []):
0 commit comments