diff options
author | jcass <john.cass77@gmail.com> | 2015-12-28 16:23:57 +0200 |
---|---|---|
committer | jcass <john.cass77@gmail.com> | 2015-12-28 16:23:57 +0200 |
commit | d2a520dbf6257986128d14a6c7d23aa37725d5e3 (patch) | |
tree | 847cf884fe129f8ff4d6cfef243b7b55cd3fa720 /pandora | |
parent | eb91a5c00701689085b6f427d62e487f417c6f51 (diff) | |
download | pydora-d2a520dbf6257986128d14a6c7d23aa37725d5e3.tar.bz2 pydora-d2a520dbf6257986128d14a6c7d23aa37725d5e3.tar.xz pydora-d2a520dbf6257986128d14a6c7d23aa37725d5e3.zip |
Handle IOErrors and ValueErrors when attempting to register ads.
Diffstat (limited to 'pandora')
-rw-r--r-- | pandora/models/pandora.py | 23 | ||||
-rw-r--r-- | pandora/transport.py | 15 |
2 files changed, 33 insertions, 5 deletions
diff --git a/pandora/models/pandora.py b/pandora/models/pandora.py index 40b368a..6abd3a7 100644 --- a/pandora/models/pandora.py +++ b/pandora/models/pandora.py | |||
@@ -130,6 +130,8 @@ class PlaylistModel(PandoraModel): | |||
130 | return cls.get_audio_field(data, "bitrate", preferred_quality) | 130 | return cls.get_audio_field(data, "bitrate", preferred_quality) |
131 | 131 | ||
132 | def get_is_playable(self): | 132 | def get_is_playable(self): |
133 | if not self.audio_url: | ||
134 | return False | ||
133 | return self._api_client.transport.test_url(self.audio_url) | 135 | return self._api_client.transport.test_url(self.audio_url) |
134 | 136 | ||
135 | def prepare_playback(self): | 137 | def prepare_playback(self): |
@@ -223,11 +225,26 @@ class AdItem(PlaylistModel): | |||
223 | def is_ad(self): | 225 | def is_ad(self): |
224 | return True | 226 | return True |
225 | 227 | ||
226 | def register_ad(self, station_id): | 228 | def register_ad(self, station_id=None): |
227 | self._api_client.register_ad(station_id, self.tracking_tokens) | 229 | if not station_id: |
230 | station_id = self.station_id | ||
231 | if self.tracking_tokens: | ||
232 | self._api_client.register_ad(station_id, self.tracking_tokens) | ||
233 | else: | ||
234 | raise ValueError('No ad tracking tokens available for ' | ||
235 | 'registration.') | ||
228 | 236 | ||
229 | def prepare_playback(self): | 237 | def prepare_playback(self): |
230 | self.register_ad(self.station_id) | 238 | try: |
239 | self.register_ad(self.station_id) | ||
240 | except ValueError as e: | ||
241 | if not self.tracking_tokens: | ||
242 | # Ignore registration attempts if no ad tracking tokens are | ||
243 | # available | ||
244 | pass | ||
245 | else: | ||
246 | # Unexpected error, re-raise. | ||
247 | raise e | ||
231 | return super(AdItem, self).prepare_playback() | 248 | return super(AdItem, self).prepare_playback() |
232 | 249 | ||
233 | 250 | ||
diff --git a/pandora/transport.py b/pandora/transport.py index 1a2ee69..62f426a 100644 --- a/pandora/transport.py +++ b/pandora/transport.py | |||
@@ -9,6 +9,7 @@ exception. | |||
9 | 9 | ||
10 | API consumers should use one of the API clients in the pandora.client package. | 10 | API consumers should use one of the API clients in the pandora.client package. |
11 | """ | 11 | """ |
12 | import logging | ||
12 | import random | 13 | import random |
13 | import time | 14 | import time |
14 | import json | 15 | import json |
@@ -22,8 +23,10 @@ from .errors import PandoraException | |||
22 | 23 | ||
23 | DEFAULT_API_HOST = "tuner.pandora.com/services/json/" | 24 | DEFAULT_API_HOST = "tuner.pandora.com/services/json/" |
24 | 25 | ||
26 | logger = logging.getLogger(__name__) | ||
25 | 27 | ||
26 | def retries(max_tries, exceptions=(Exception,)): | 28 | |
29 | def retries(max_tries, exceptions=(IOError,)): | ||
27 | """Function decorator implementing retrying logic. | 30 | """Function decorator implementing retrying logic. |
28 | 31 | ||
29 | exceptions: A tuple of exception classes; default (Exception,) | 32 | exceptions: A tuple of exception classes; default (Exception,) |
@@ -46,7 +49,11 @@ def retries(max_tries, exceptions=(Exception,)): | |||
46 | retries_left -= 1 | 49 | retries_left -= 1 |
47 | return func(*args, **kwargs) | 50 | return func(*args, **kwargs) |
48 | 51 | ||
49 | except exceptions: | 52 | except exceptions as e: |
53 | # Don't retry for PandoraExceptions - unlikely that result | ||
54 | # will change for same set of input parameters. | ||
55 | if isinstance(e, PandoraException): | ||
56 | continue | ||
50 | if retries_left > 0: | 57 | if retries_left > 0: |
51 | time.sleep(delay_exponential( | 58 | time.sleep(delay_exponential( |
52 | 0.5, 2, max_tries - retries_left)) | 59 | 0.5, 2, max_tries - retries_left)) |
@@ -227,9 +234,13 @@ class APITransport(object): | |||
227 | self._start_request(method) | 234 | self._start_request(method) |
228 | 235 | ||
229 | url = self._build_url(method) | 236 | url = self._build_url(method) |
237 | log_data = data | ||
230 | data = self._build_data(method, data) | 238 | data = self._build_data(method, data) |
231 | params = self._build_params(method) | 239 | params = self._build_params(method) |
240 | logger.info('TRANSPORT: \nMETHOD: {}\nPARAMS: {}\nDATA: {}\nURL: {}' | ||
241 | .format(method, params, log_data, url)) | ||
232 | result = self._make_http_request(url, data, params) | 242 | result = self._make_http_request(url, data, params) |
243 | logger.info('TRANSPORT: \nRESULT: {}\n'.format(result)) | ||
233 | 244 | ||
234 | return self._parse_response(result) | 245 | return self._parse_response(result) |
235 | 246 | ||