Changeset 1857

Show
Ignore:
Timestamp:
02/11/08 14:57:53 (2 months ago)
Author:
duncan
svm:headrev:

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

Corrected a typo
Inproved http error handling

Still not perfect, but coming along

Files:
1 modified

Legend:

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

    r1855 r1857  
    6767    def __init__(self, why): 
    6868        Exception.__init__(self) 
    69         self.why = why 
     69        print 'DJW:why:', why, type(why), dir(why) 
     70        self.why = str(why) 
    7071 
    7172    def __str__(self): 
     
    106107        plugin.MainMenuPlugin.__init__(self) 
    107108        self.menuitem = None 
    108         if not os.path.isdir(config.LASTFM_DIR): 
    109             os.makedirs(config.LASTFM_DIR, 0777) 
    110109 
    111110 
     
    224223 
    225224 
    226     @benchmark(benchmarking, True) #benchmarkcall) 
     225    @benchmark(benchmarking, benchmarkcall) 
    227226    def eventhandler(self, event, menuw=None): 
    228227        _debug_('LastFMItem.eventhandler(event=%s, menuw=%r)' % (event, menuw), 2) 
     228        print 'LastFMItem.eventhandler(event=%r, arg=%r)' % (event.name, event.arg) 
    229229        if event == 'STOP': 
    230230            self.stop(self.arg, self.menuw) 
     
    264264                    if menuw: 
    265265                        AlertBox(text='No recs :(').show() 
     266                    traceback.print_stack() 
    266267                    rc.post_event(PLAY_END) 
    267268                    return 
     
    271272                    if menuw: 
    272273                        AlertBox(text=_('Cannot get XSFP')).show() 
     274                    traceback.print_stack() 
    273275                    rc.post_event(PLAY_END) 
    274276                    return 
     
    276278                _debug_(why, DWARNING) 
    277279                if menuw: 
     280                    print 'DJW:why:', why, type(why), '%r' % str(why) 
    278281                    AlertBox(text=str(why)).show() 
     282                traceback.print_stack() 
    279283                rc.post_event(PLAY_END) 
     284                return 
    280285 
    281286        entry = self.feed.entries[0] 
    282         self.stream_name = urllib.unquote_plus(self.feed.feed.title) 
     287        print 'DJW:entry:', entry.artist, '/', entry.album, '/', entry.title 
     288        self.stream_name = urllib.unquote_plus(self.feed.title) 
    283289        self.album = entry.album 
    284290        self.artist = entry.artist 
     
    286292        self.location_url = entry.location_url 
    287293        self.length = entry.duration 
    288         basename = entry.artist + '-' + entry.album + '-' + entry.title 
    289         self.basename = basename.lower().replace(' ', '_').replace('.', '').replace('\'', '').replace(':', '') 
    290         self.url = os.path.join(config.LASTFM_DIR, self.basename + os.path.splitext(entry.location_url)[1]) 
    291         self.trackpath = os.path.join(config.LASTFM_DIR, self.basename + os.path.splitext(entry.location_url)[1]) 
    292         self.image = os.path.join(config.LASTFM_DIR, self.basename + os.path.splitext(entry.image_url)[1]) 
    293         self.image_downloader = self.webservices.download(entry.image_url, self.image) 
    294         self.track_downloader = self.webservices.download(self.location_url, self.trackpath, istrack=True) 
     294        basename = os.path.join(config.LASTFM_DIR, self.stream_name, entry.artist, entry.album, entry.title).lower() 
     295        self.basename = basename.replace(' ', '_').replace('.', '').replace('\'', '').replace(':', '').replace(',', '') 
     296        if not os.path.exists(os.path.dirname(self.basename)): 
     297            _debug_('make directory %r' % (os.path.dirname(self.basename),), DINFO) 
     298            os.makedirs(os.path.dirname(self.basename), 0777) 
     299        # url is changed, to include file:// 
     300        self.url = os.path.join(self.basename + os.path.splitext(entry.location_url)[1]) 
     301        self.trackpath = os.path.join(self.basename + os.path.splitext(entry.location_url)[1]) 
     302        self.track_downloader = self.webservices.download(self.location_url, self.trackpath) 
     303        if entry.image_url: 
     304            self.image = os.path.join(self.basename + os.path.splitext(entry.image_url)[1]) 
     305            self.image_downloader = self.webservices.download(entry.image_url, self.image) 
     306            # Wait three seconds for the image to be downloaded 
     307            for i in range(30): 
     308                if not self.image_downloader.isrunning(): 
     309                    break 
     310                time.sleep(0.1) 
     311        else: 
     312            self.image = None 
    295313        #self.is_playlist = True 
    296314        # Wait for a bit of the file to be downloaded 
    297315        while self.track_downloader.filesize() < 1024 * 20: 
    298316            if not self.track_downloader.isrunning(): 
     317                traceback.print_stack() 
    299318                rc.post_event(PLAY_END) 
    300319                return 
    301             time.sleep(0.1) 
    302         # Wait for the image to be downloaded 
    303         for i in range(30): 
    304             if not self.image_downloader.isrunning(): 
    305                 break 
    306320            time.sleep(0.1) 
    307321        self.player = PlayerGUI(self, menuw) 
     
    315329            if menuw: 
    316330                AlertBox(text=error).show() 
     331            traceback.print_stack() 
    317332            rc.post_event(PLAY_END) 
    318333 
     
    421436                if len(reply) == 0: 
    422437                    self.running = False 
     438                    print 'DJW:downloaded %s' % self.filename 
    423439                    _debug_('%s downloaded' % self.filename) 
    424                     print 'DJW:downloaded %s' % self.filename 
    425440                    # what we could do now is to add tags to track 
    426441                    break 
    427442                self.size += len(reply) 
    428443            else: 
    429                 _debug_('%s aborted' % self.filename) 
     444                print 'DJW:aborted %s' % self.filename 
     445                os.remove(self.filename) 
     446                #_debug_('%s aborted' % self.filename) 
    430447            fd.close() 
    431448            f.close() 
    432449        except ValueError, why: 
    433             _debug_('%s: %s' % (self.filename, why), DWARNING) 
     450            _debug_('%s: %s' % (self.url, why), DWARNING) 
    434451        except urllib2.HTTPError, why: 
    435             _debug_('%s: %s' % (self.filename, why), DWARNING) 
     452            _debug_('%s: %s' % (self.url, why), DWARNING) 
    436453 
    437454 
     
    532549                f = opener.open(request) 
    533550                reply = f.read() 
     551            except urllib2.HTTPError, why: 
     552                _debug_('%s: %s' % (url, why), DWARNING) 
     553                raise LastFMError(why) 
    534554            except Exception, why: 
    535555                _debug_('%s: %s' % (url, why), DWARNING) 
    536                 raise 
     556                raise LastFMError(why) 
    537557            _debug_('len(reply)=%r' % (len(reply),), 1) 
    538558            return reply 
     
    577597        if not self.session: 
    578598            self._login() 
    579         #request_url = 'http://%s%s/xspf.php?sk=%s&discovery=0&desktop=%s' % \ 
    580         request_url = 'http://%s%s/xspf.php?sk=%s&discovery=1&desktop=%s' % \ 
     599        request_url = 'http://%s%s/xspf.php?sk=%s&discovery=0&desktop=%s' % \ 
    581600            (self.base_url, self.base_path, self.session, LastFMWebServices._version) 
    582601        return self._urlopen(request_url, lines=False) 
     
    618637 
    619638    @benchmark(benchmarking, benchmarkcall) 
    620     def download(self, url, filename, istrack=False): 
     639    def download(self, url, filename): 
    621640        """ 
    622641        Download album cover or track to last.fm directory. 
     
    634653            'User-agent': 'Freevo-%s (r%s)' % (version.__version__, revision.__revision__) 
    635654        } 
    636         if istrack: 
    637             headers.update({'Host': 'play.last.fm'}) 
    638655        self.downloader = LastFMDownloader(url, filename, headers) 
    639656        self.downloader.start() 
     
    717734    @benchmark(benchmarking, benchmarkcall) 
    718735    def __init__(self): 
    719         self.entries = [] 
    720736        self.feed = feedparser.FeedParserDict() 
     737        self.feed.entries = [] 
    721738 
    722739 
     
    755772                trackauth = track_elem.find('{%s}trackauth' % LastFMXSPF._LASTFM_NS) 
    756773                track.trackauth = trackauth is not None and trackauth.text or u'' 
    757                 self.entries.append(track) 
    758         return self 
     774                self.feed.entries.append(track) 
     775        return self.feed 
    759776 
    760777