| 263 | | pop = None |
| 264 | | if self.feed is None: |
| 265 | | pop = PopupBox(text=_('Downloading, please wait...')) |
| 266 | | pop.show() |
| 267 | | |
| 268 | | try: |
| 269 | | if self.feed is None or len(self.feed.entries) <= 1: |
| 270 | | for i in range(3): |
| 271 | | xspf = self.webservices.request_xspf() |
| 272 | | if xspf != 'No recs :(': |
| 273 | | break |
| 274 | | time.sleep(2) |
| 275 | | else: |
| 276 | | raise LastFMError('No recs :(') |
| 277 | | |
| 278 | | self.feed = self.xspf.parse(xspf) |
| 279 | | if self.feed is None: |
| 280 | | raise LastFMError('Cannot get XSFP') |
| 281 | | |
| 282 | | entry = self.feed.entries[0] |
| 283 | | _debug_('entry "%s / %s / %s" of %s' % (entry.artist, entry.album, entry.title, len(self.feed.entries))) |
| 284 | | self.stream_name = urllib.unquote_plus(self.feed.title) |
| 285 | | self.album = entry.album |
| 286 | | self.artist = entry.artist |
| 287 | | self.title = entry.title |
| 288 | | self.location_url = entry.location_url |
| 289 | | self.length = entry.duration |
| 290 | | basename = os.path.join(config.LASTFM_DIR, self.stream_name, entry.artist, entry.album, entry.title) |
| 291 | | self.basename = basename.lower().replace(' ', '_').\ |
| 292 | | replace('.', '').replace('\'', '').replace(':', '').replace(',', '') |
| 293 | | if not os.path.exists(os.path.dirname(self.basename)): |
| 294 | | _debug_('make directory %r' % (os.path.dirname(self.basename),), DINFO) |
| 295 | | os.makedirs(os.path.dirname(self.basename), 0777) |
| 296 | | # url is changed, to include file:// |
| 297 | | self.url = os.path.join(self.basename + os.path.splitext(entry.location_url)[1]) |
| 298 | | self.trackpath = os.path.join(self.basename + os.path.splitext(entry.location_url)[1]) |
| 299 | | if entry.image_url: |
| 300 | | self.image = os.path.join(self.basename + os.path.splitext(entry.image_url)[1]) |
| 301 | | self.image_downloader = self.webservices.download(entry.image_url, self.image) |
| 302 | | # Wait three seconds for the image to be downloaded |
| 303 | | for i in range(30): |
| 304 | | if not self.image_downloader.isrunning(): |
| 305 | | break |
| 306 | | time.sleep(0.1) |
| | 261 | if self.feed is None or len(self.feed.entries) <= 1: |
| | 262 | for i in range(3): |
| | 263 | xspf = self.webservices.request_xspf() |
| | 264 | if xspf != 'No recs :(': |
| | 265 | break |
| | 266 | time.sleep(2) |
| 308 | | self.image = None |
| 309 | | self.track_downloader = self.webservices.download(self.location_url, self.trackpath, self) |
| 310 | | # Wait for a bit of the file to be downloaded |
| 311 | | while self.track_downloader.filesize() < 1024 * 20: |
| 312 | | if not self.track_downloader.isrunning(): |
| 313 | | raise LastFMError('Failed to download track', entry.location_url) |
| | 268 | raise LastFMError('No recs :(') |
| | 269 | |
| | 270 | self.feed = self.xspf.parse(xspf) |
| | 271 | if self.feed is None: |
| | 272 | raise LastFMError('Cannot get XSFP') |
| | 273 | |
| | 274 | entry = self.feed.entries[0] |
| | 275 | _debug_('entry "%s / %s / %s" of %s' % (entry.artist, entry.album, entry.title, len(self.feed.entries))) |
| | 276 | self.stream_name = urllib.unquote_plus(self.feed.title) |
| | 277 | self.album = entry.album |
| | 278 | self.artist = entry.artist |
| | 279 | self.title = entry.title |
| | 280 | self.location_url = entry.location_url |
| | 281 | self.length = entry.duration |
| | 282 | basename = os.path.join(config.LASTFM_DIR, self.stream_name, entry.artist, entry.album, entry.title) |
| | 283 | self.basename = basename.lower().replace(' ', '_').\ |
| | 284 | replace('.', '').replace('\'', '').replace(':', '').replace(',', '') |
| | 285 | if not os.path.exists(os.path.dirname(self.basename)): |
| | 286 | _debug_('make directory %r' % (os.path.dirname(self.basename),), DINFO) |
| | 287 | os.makedirs(os.path.dirname(self.basename), 0777) |
| | 288 | # url is changed, to include file:// |
| | 289 | self.url = os.path.join(self.basename + os.path.splitext(entry.location_url)[1]) |
| | 290 | self.trackpath = os.path.join(self.basename + os.path.splitext(entry.location_url)[1]) |
| | 291 | if entry.image_url: |
| | 292 | self.image = os.path.join(self.basename + os.path.splitext(entry.image_url)[1]) |
| | 293 | self.image_downloader = self.webservices.download(entry.image_url, self.image) |
| | 294 | # Wait three seconds for the image to be downloaded |
| | 295 | for i in range(30): |
| | 296 | if not self.image_downloader.isrunning(): |
| | 297 | break |
| 315 | | if not self.player: |
| 316 | | self.player = PlayerGUI(self, menuw) |
| 317 | | error = self.player.play() |
| 318 | | if error: |
| 319 | | raise LastFMError('Play error=%r' % (error,)) |
| 320 | | finally: |
| 321 | | if pop is not None: |
| 322 | | pop.destroy() |
| | 299 | else: |
| | 300 | self.image = None |
| | 301 | self.track_downloader = self.webservices.download(self.location_url, self.trackpath, self) |
| | 302 | # Wait for a bit of the file to be downloaded |
| | 303 | while self.track_downloader.filesize() < 1024 * 20: |
| | 304 | if not self.track_downloader.isrunning(): |
| | 305 | raise LastFMError('Failed to download track', entry.location_url) |
| | 306 | time.sleep(0.1) |
| | 307 | if not self.player: |
| | 308 | self.player = PlayerGUI(self, menuw) |
| | 309 | error = self.player.play() |
| | 310 | if error: |
| | 311 | raise LastFMError('Play error=%r' % (error,)) |
| | 312 | |
| 544 | | for line in self._urlopen(tune_url): |
| 545 | | if re.search('response=OK', line): |
| 546 | | return True |
| 547 | | return False |
| 548 | | except AttributeError, why: |
| 549 | | return None |
| 550 | | except IOError, why: |
| 551 | | return None |
| | 532 | if not self.session: |
| | 533 | self._login() |
| | 534 | tune_url = 'http://ws.audioscrobbler.com/radio/adjust.php?session=%s&url=%s&lang=%s&debug=0' % \ |
| | 535 | (self.session, station_url, config.LASTFM_LANG) |
| | 536 | try: |
| | 537 | for line in self._urlopen(tune_url): |
| | 538 | if re.search('response=OK', line): |
| | 539 | return True |
| | 540 | return False |
| | 541 | except AttributeError, why: |
| | 542 | return None |
| | 543 | except IOError, why: |
| | 544 | return None |
| | 545 | finally: |
| | 546 | pop.destroy() |