Changeset 1902

Show
Ignore:
Timestamp:
19/11/08 21:46:12 (7 weeks ago)
Author:
duncan
svm:headrev:

cc3e1ea1-1e01-0410-8d68-8b121e83a9d5:11184
Message:

Moved the popup box to the adjust station method, this can take a while > 10 secs

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • freevo/src/audio/plugins/lastfm2.py

    r1899 r1902  
    11# -*- coding: iso-8859-1 -*- 
    22# ----------------------------------------------------------------------- 
    3 # 
     3# LastFM radio player plug-in (http://www.last.fm/listen) 
    44# ----------------------------------------------------------------------- 
    55# $Id$ 
    66# 
    7 # Notes: 
     7# Notes: For the API 1.2 
     8# http://code.google.com/p/thelastripper/wiki/LastFM12UnofficialDocumentation 
    89# Todo: 
    910# 
     
    2930# ----------------------------------------------------------------------- 
    3031 
    31  
    3232import sys, os, time, traceback 
    3333import md5, urllib, urllib2, httplib, re 
     
    6161 
    6262 
    63  
    6463class LastFMError(Exception): 
    6564    """ 
     
    7372        else: 
    7473            self.why = str(why) 
    75  
    7674 
    7775    def __str__(self): 
     
    261259 
    262260        try: 
    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) 
    307267                else: 
    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 
    314298                    time.sleep(0.1) 
    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 
    323313        except LastFMError, why: 
    324314            traceback.print_exc() 
     
    537527        """Change Last FM Station""" 
    538528        _debug_('adjust_station(station_url=%r)' % (station_url,), 2) 
    539         if not self.session: 
    540             self._login() 
    541         tune_url = 'http://ws.audioscrobbler.com/radio/adjust.php?session=%s&url=%s&lang=%s&debug=0' % \ 
    542             (self.session, station_url, config.LASTFM_LANG) 
     529        pop = PopupBox(text=_('Tuning radio station, please wait...')) 
     530        pop.show() 
    543531        try: 
    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() 
    552547 
    553548 
     
    690685                if len(reply) == 0: 
    691686                    self.running = False 
    692                     if config.DEBUG: 
     687                    if config.DEBUG >= 2: 
    693688                        print '%s downloaded' % self.filename 
    694689                    # debugs fail during shutdown 
    695690                    #_debug_('%s downloaded' % self.filename) 
    696                     # what we could do now is to add tags to track 
     691                    # XXX this may upset mplayer, stopping playback before the end of the track 
    697692                    if self.entry: 
     693                        time.sleep(3) 
    698694                        from kaa.metadata.audio import eyeD3 
    699695                        try: 
     
    723719 
    724720 
    725     @benchmark(benchmarking, benchmarkcall) 
     721    #@benchmark(benchmarking, benchmarkcall) 
    726722    def filesize(self): 
    727723        """ 
     
    740736 
    741737 
    742     @benchmark(benchmarking, benchmarkcall) 
     738    #@benchmark(benchmarking, benchmarkcall) 
    743739    def isrunning(self): 
    744740        """