Changeset 1878

Show
Ignore:
Timestamp:
10/11/08 22:01:15 (2 months ago)
Author:
duncan
svm:headrev:

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

Added tagging of downloaded track
Updated error handling in play

Files:
1 modified

Legend:

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

    r1872 r1878  
    6666    """ 
    6767    @benchmark(benchmarking, benchmarkcall) 
    68     def __init__(self, why, url=''): 
     68    def __init__(self, why, url=None): 
    6969        Exception.__init__(self) 
    70         self.why = str(why) + ': ' + url 
     70        if url: 
     71            self.why = str(why) + ': ' + url 
     72        else: 
     73            self.why = str(why) 
     74 
    7175 
    7276    def __str__(self): 
     
    177181    @benchmark(benchmarking, benchmarkcall) 
    178182    def shutdown(self): 
     183        pprint.pprint(self.__dict__) 
    179184        if self.webservices is not None: 
    180185            self.webservices.shutdown() 
     
    256261            self.menuw = menuw 
    257262 
    258         pop = None 
    259         if self.feed is None: 
    260             pop = PopupBox(text=_('Downloading, please wait...')) 
    261             pop.show() 
    262  
    263263        try: 
    264             if self.feed is None or len(self.feed.entries) <= 1: 
    265                 try: 
     264            pop = None 
     265            if self.feed is None: 
     266                pop = PopupBox(text=_('Downloading, please wait...')) 
     267                pop.show() 
     268 
     269            try: 
     270                if self.feed is None or len(self.feed.entries) <= 1: 
    266271                    for i in range(3): 
    267272                        xspf = self.webservices.request_xspf() 
     
    270275                        time.sleep(2) 
    271276                    else: 
    272                         if menuw: 
    273                             AlertBox(text='No recs :(').show() 
    274                         traceback.print_stack() 
    275                         rc.post_event(STOP) 
    276                         return 
     277                        raise LastFMError('No recs :(') 
    277278 
    278279                    self.feed = self.xspf.parse(xspf) 
    279280                    if self.feed is None: 
    280                         if menuw: 
    281                             AlertBox(text=_('Cannot get XSFP')).show() 
    282                         traceback.print_stack() 
    283                         rc.post_event(STOP) 
    284                         return 
    285                 except LastFMError, why: 
    286                     _debug_(why, DWARNING) 
    287                     if menuw: 
    288                         AlertBox(text=str(why)).show() 
    289                     rc.post_event(STOP) 
    290                     return 
    291  
    292             entry = self.feed.entries[0] 
    293             _debug_('entry "%s / %s / %s" of %s' % (entry.artist, entry.album, entry.title, len(self.feed.entries))) 
    294             self.stream_name = urllib.unquote_plus(self.feed.title) 
    295             self.album = entry.album 
    296             self.artist = entry.artist 
    297             self.title = entry.title 
    298             self.location_url = entry.location_url 
    299             self.length = entry.duration 
    300             basename = os.path.join(config.LASTFM_DIR, self.stream_name, entry.artist, entry.album, entry.title) 
    301             self.basename = basename.lower().replace(' ', '_').\ 
    302                 replace('.', '').replace('\'', '').replace(':', '').replace(',', '') 
    303             if not os.path.exists(os.path.dirname(self.basename)): 
    304                 _debug_('make directory %r' % (os.path.dirname(self.basename),), DINFO) 
    305                 os.makedirs(os.path.dirname(self.basename), 0777) 
    306             # url is changed, to include file:// 
    307             self.url = os.path.join(self.basename + os.path.splitext(entry.location_url)[1]) 
    308             self.trackpath = os.path.join(self.basename + os.path.splitext(entry.location_url)[1]) 
    309             self.track_downloader = self.webservices.download(self.location_url, self.trackpath) 
    310             if entry.image_url: 
    311                 self.image = os.path.join(self.basename + os.path.splitext(entry.image_url)[1]) 
    312                 self.image_downloader = self.webservices.download(entry.image_url, self.image) 
    313                 # Wait three seconds for the image to be downloaded 
    314                 for i in range(30): 
    315                     if not self.image_downloader.isrunning(): 
    316                         break 
     281                        raise LastFMError('Cannot get XSFP') 
     282 
     283                entry = self.feed.entries[0] 
     284                _debug_('entry "%s / %s / %s" of %s' % (entry.artist, entry.album, entry.title, len(self.feed.entries))) 
     285                self.stream_name = urllib.unquote_plus(self.feed.title) 
     286                self.album = entry.album 
     287                self.artist = entry.artist 
     288                self.title = entry.title 
     289                self.location_url = entry.location_url 
     290                self.length = entry.duration 
     291                basename = os.path.join(config.LASTFM_DIR, self.stream_name, entry.artist, entry.album, entry.title) 
     292                self.basename = basename.lower().replace(' ', '_').\ 
     293                    replace('.', '').replace('\'', '').replace(':', '').replace(',', '') 
     294                if not os.path.exists(os.path.dirname(self.basename)): 
     295                    _debug_('make directory %r' % (os.path.dirname(self.basename),), DINFO) 
     296                    os.makedirs(os.path.dirname(self.basename), 0777) 
     297                # url is changed, to include file:// 
     298                self.url = os.path.join(self.basename + os.path.splitext(entry.location_url)[1]) 
     299                self.trackpath = os.path.join(self.basename + os.path.splitext(entry.location_url)[1]) 
     300                if entry.image_url: 
     301                    self.image = os.path.join(self.basename + os.path.splitext(entry.image_url)[1]) 
     302                    self.image_downloader = self.webservices.download(entry.image_url, self.image) 
     303                    # Wait three seconds for the image to be downloaded 
     304                    for i in range(30): 
     305                        if not self.image_downloader.isrunning(): 
     306                            break 
     307                        time.sleep(0.1) 
     308                else: 
     309                    self.image = None 
     310                self.track_downloader = self.webservices.download(self.location_url, self.trackpath, self) 
     311                # Wait for a bit of the file to be downloaded 
     312                while self.track_downloader.filesize() < 1024 * 20: 
     313                    if not self.track_downloader.isrunning(): 
     314                        raise LastFMError('Failed to download track', entry.location_url) 
    317315                    time.sleep(0.1) 
    318             else: 
    319                 self.image = None 
    320             #self.is_playlist = True 
    321             # Wait for a bit of the file to be downloaded 
    322             while self.track_downloader.filesize() < 1024 * 20: 
    323                 if not self.track_downloader.isrunning(): 
    324                     traceback.print_stack() 
    325                     rc.post_event(STOP) 
    326                     return 
    327                 time.sleep(0.1) 
    328             self.player = PlayerGUI(self, menuw) 
    329             error = self.player.play() 
    330             if error: 
    331                 _debug_('player play error=%r' % (error,), DWARNING) 
    332                 if menuw: 
    333                     AlertBox(text=error).show() 
    334                 traceback.print_stack() 
    335                 rc.post_event(STOP) 
    336         finally: 
    337             if pop is not None: 
    338                 pop.destroy() 
     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() 
     323        except LastFMError, why: 
     324            _debug_('player play error=%r' % (error,), DWARNING) 
     325            if menuw: 
     326                AlertBox(text=str(why)).show() 
     327            rc.post_event(STOP) 
     328            return 
    339329 
    340330 
     
    575565 
    576566    @benchmark(benchmarking, benchmarkcall) 
    577     def download(self, url, filename): 
     567    def download(self, url, filename, entry=None): 
    578568        """ 
    579569        Download album cover or track to last.fm directory. 
     
    583573        @param url: location of item to download 
    584574        @param filename: path to downloaded file 
     575        @param entry: metadata for the entry 
    585576        """ 
    586577        _debug_('download(url=%r, filename=%r)' % (url, filename), 1) 
     
    591582            'User-agent': 'Freevo-%s (r%s)' % (version.__version__, revision.__revision__) 
    592583        } 
    593         self.downloader = LastFMDownloader(url, filename, headers) 
     584        self.downloader = LastFMDownloader(url, filename, headers, entry) 
    594585        self.downloader.start() 
    595586        return self.downloader 
     
    670661    """ 
    671662    @benchmark(benchmarking, benchmarkcall) 
    672     def __init__(self, url, filename, headers=None): 
     663    def __init__(self, url, filename, headers=None, entry=None): 
    673664        Thread.__init__(self) 
    674665        self.url = url 
    675666        self.filename = filename 
    676667        self.headers = headers 
     668        self.entry = entry 
    677669        self.size = 0 
    678670        self.running = True 
     
    699691                    #_debug_('%s downloaded' % self.filename) 
    700692                    # what we could do now is to add tags to track 
     693                    if self.entry: 
     694                        from kaa.metadata.audio import eyeD3 
     695                        try: 
     696                            tag = eyeD3.Tag() 
     697                            tag.link(self.filename) 
     698                            tag.header.setVersion(eyeD3.ID3_V2_3) 
     699                            tag.setArtist(self.entry.artist) 
     700                            tag.setAlbum(self.entry.album) 
     701                            tag.setTitle(self.entry.title) 
     702                            #tag.setGenre(self.entry.genre) 
     703                            tag.addImage(eyeD3.ImageFrame.FRONT_COVER, self.entry.image) 
     704                            tag.update() 
     705                        except Exception, why: 
     706                            print why 
    701707                    break 
    702708                self.size += len(reply)