Changeset 1853
- Timestamp:
- 01/11/08 18:56:59 (2 months ago)
- svm:headrev:
cc3e1ea1-1e01-0410-8d68-8b121e83a9d5:11134- Files:
-
- 1 modified
-
freevo/src/audio/plugins/lastfm2.py (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
freevo/src/audio/plugins/lastfm2.py
r1850 r1853 40 40 import plugin 41 41 import rc 42 import version, revision 42 43 from event import PLAY_END 43 44 from menu import MenuItem, Menu … … 280 281 281 282 entry = self.feed.entries[self.entry] 283 self.stream_name = urllib.unquote_plus(self.feed.feed.title) 282 284 self.album = entry.album 283 285 self.artist = entry.artist 284 286 self.title = entry.title 287 self.location_url = entry.location_url 285 288 self.length = entry.duration 286 289 basename = entry.artist + '-' + entry.album + '-' + entry.title … … 288 291 self.url = os.path.join(config.LASTFM_DIR, self.basename + os.path.splitext(entry.location_url)[1]) 289 292 self.trackpath = os.path.join(config.LASTFM_DIR, self.basename + os.path.splitext(entry.location_url)[1]) 290 self.location_url = entry.location_url 291 self.track_downloader = self.webservices.download(self.location_url, self.trackpath) 292 self.stream_name = urllib.unquote_plus(self.feed.feed.title) 293 self.imagepath = os.path.join(config.LASTFM_DIR, self.basename + os.path.splitext(entry.image_url)[1]) 294 self.image_downloader = self.webservices.download(entry.image_url, self.imagepath) 293 self.image = os.path.join(config.LASTFM_DIR, self.basename + os.path.splitext(entry.image_url)[1]) 294 self.image_downloader = self.webservices.download(entry.image_url, self.image) 295 self.track_downloader = self.webservices.download(self.location_url, self.trackpath, istrack=True) 295 296 #self.is_playlist = True 296 297 # Wait for a bit of the file to be downloaded … … 299 300 rc.post_event(PLAY_END) 300 301 return 302 time.sleep(0.1) 303 # Wait for the image to be downloaded 304 for i in range(30): 305 if not self.image_downloader.isrunning(): 306 break 301 307 time.sleep(0.1) 302 308 self.player = PlayerGUI(self, menuw) … … 307 313 error = self.player.play() 308 314 if error: 309 _debug_(' error=%r' % (error,), DWARNING)315 _debug_('player play error=%r' % (error,), DWARNING) 310 316 if menuw: 311 317 AlertBox(text=error).show() … … 372 378 class SmartRedirectHandler(urllib2.HTTPRedirectHandler): 373 379 def http_error_301(self, req, fp, code, msg, headers): 380 #print 'DJW:http_error_301' 374 381 result = urllib2.HTTPRedirectHandler.http_error_301(self, req, fp, code, msg, headers) 375 382 result.status = code … … 377 384 378 385 def http_error_302(self, req, fp, code, msg, headers): 386 #print 'DJW:http_error_302' 379 387 result = urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers) 380 388 result.status = code … … 404 412 requested to stop. 405 413 """ 406 httplib.HTTPConnection.debuglevel = 1414 #print 'DJW:self.url:', self.url, 'self.headers:', self.headers 407 415 request = urllib2.Request(self.url, headers=self.headers) 408 416 opener = urllib2.build_opener(SmartRedirectHandler()) 409 417 try: 410 httplib.HTTPConnection.debuglevel = 1411 418 f = opener.open(request) 412 419 fd = open(self.filename, 'wb') … … 417 424 self.running = False 418 425 _debug_('%s downloaded' % self.filename) 426 #print 'DJW:downloaded %s' % self.filename 419 427 # what we could do now is to add tags to track 420 428 break … … 424 432 fd.close() 425 433 f.close() 434 break 435 except ValueError, why: 436 _debug_('%s: %s' % (self.filename, why), DWARNING) 426 437 except urllib2.HTTPError, why: 427 438 _debug_('%s: %s' % (self.filename, why), DWARNING) 428 httplib.HTTPConnection.debuglevel = 0429 439 430 440 … … 457 467 """ 458 468 _version = '1.1.2' 469 headers = { 470 'User-agent': 'Freevo-%s (r%s)' % (version.__version__, revision.__revision__) 471 } 459 472 460 473 @benchmark(benchmarking, benchmarkcall) … … 495 508 @returns: reply from request 496 509 """ 497 httplib.HTTPConnection.debuglevel = 1 498 try: 499 _debug_('url=%r, data=%r' % (url, data), 1) 500 request = urllib2.Request(url) 501 opener = urllib2.build_opener(SmartRedirectHandler()) 502 if lines: 503 reply = [] 504 try: 505 f = opener.open(request) 506 lines = f.readlines() 507 if lines is None: 508 return [] 509 for line in lines: 510 reply.append(line.strip('\n')) 511 except httplib.BadStatusLine, why: 512 print 'BadStatusLine:', why 513 reply = None 514 except AttributeError, why: 515 reply = None 516 except Exception, why: 517 _debug_('%s: %s' % (url, why), DWARNING) 518 raise 519 _debug_('reply=%r' % (reply,), 1) 520 return reply 521 else: 522 reply = '' 523 try: 524 f = opener.open(request) 525 reply = f.read() 526 except Exception, why: 527 _debug_('%s: %s' % (url, why), DWARNING) 528 raise 529 _debug_('len(reply)=%r' % (len(reply),), 1) 530 return reply 531 finally: 532 httplib.HTTPConnection.debuglevel = 0 510 _debug_('url=%r, data=%r' % (url, data), 1) 511 request = urllib2.Request(url, headers=LastFMWebServices.headers) 512 opener = urllib2.build_opener(SmartRedirectHandler()) 513 if lines: 514 reply = [] 515 try: 516 f = opener.open(request) 517 lines = f.readlines() 518 if lines is None: 519 return [] 520 for line in lines: 521 reply.append(line.strip('\n')) 522 except httplib.BadStatusLine, why: 523 print 'BadStatusLine:', why 524 reply = None 525 except AttributeError, why: 526 reply = None 527 except Exception, why: 528 _debug_('%s: %s' % (url, why), DWARNING) 529 raise 530 _debug_('reply=%r' % (reply,), 1) 531 return reply 532 else: 533 reply = '' 534 try: 535 f = opener.open(request) 536 reply = f.read() 537 except Exception, why: 538 _debug_('%s: %s' % (url, why), DWARNING) 539 raise 540 _debug_('len(reply)=%r' % (len(reply),), 1) 541 return reply 533 542 534 543 … … 612 621 613 622 @benchmark(benchmarking, benchmarkcall) 614 def download(self, url, filename ):623 def download(self, url, filename, istrack=False): 615 624 """ 616 625 Download album cover or track to last.fm directory. … … 625 634 self._login() 626 635 headers = { 627 'Session': self.session, 636 'Cookie': 'Session=%s' % self.session, 637 'User-agent': 'Freevo-%s (r%s)' % (version.__version__, revision.__revision__) 628 638 } 639 if istrack: 640 headers.update({'Host': 'play.last.fm'}) 629 641 self.downloader = LastFMDownloader(url, filename, headers) 630 642 self.downloader.start()
