Changeset 1853

Show
Ignore:
Timestamp:
01/11/08 18:56:59 (2 months ago)
Author:
duncan
svm:headrev:

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

Removed debug statements
Added User-agent and other headers

Files:
1 modified

Legend:

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

    r1850 r1853  
    4040import plugin 
    4141import rc 
     42import version, revision 
    4243from event import PLAY_END 
    4344from menu import MenuItem, Menu 
     
    280281 
    281282        entry = self.feed.entries[self.entry] 
     283        self.stream_name = urllib.unquote_plus(self.feed.feed.title) 
    282284        self.album = entry.album 
    283285        self.artist = entry.artist 
    284286        self.title = entry.title 
     287        self.location_url = entry.location_url 
    285288        self.length = entry.duration 
    286289        basename = entry.artist + '-' + entry.album + '-' + entry.title 
     
    288291        self.url = os.path.join(config.LASTFM_DIR, self.basename + os.path.splitext(entry.location_url)[1]) 
    289292        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) 
    295296        #self.is_playlist = True 
    296297        # Wait for a bit of the file to be downloaded 
     
    299300                rc.post_event(PLAY_END) 
    300301                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 
    301307            time.sleep(0.1) 
    302308        self.player = PlayerGUI(self, menuw) 
     
    307313        error = self.player.play() 
    308314        if error: 
    309             _debug_('error=%r' % (error,), DWARNING) 
     315            _debug_('player play error=%r' % (error,), DWARNING) 
    310316            if menuw: 
    311317                AlertBox(text=error).show() 
     
    372378class SmartRedirectHandler(urllib2.HTTPRedirectHandler): 
    373379    def http_error_301(self, req, fp, code, msg, headers): 
     380        #print 'DJW:http_error_301' 
    374381        result = urllib2.HTTPRedirectHandler.http_error_301(self, req, fp, code, msg, headers) 
    375382        result.status = code 
     
    377384 
    378385    def http_error_302(self, req, fp, code, msg, headers): 
     386        #print 'DJW:http_error_302' 
    379387        result = urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers) 
    380388        result.status = code 
     
    404412        requested to stop. 
    405413        """ 
    406         httplib.HTTPConnection.debuglevel = 1 
     414        #print 'DJW:self.url:', self.url, 'self.headers:', self.headers 
    407415        request = urllib2.Request(self.url, headers=self.headers) 
    408416        opener = urllib2.build_opener(SmartRedirectHandler()) 
    409417        try: 
    410             httplib.HTTPConnection.debuglevel = 1 
    411418            f = opener.open(request) 
    412419            fd = open(self.filename, 'wb') 
     
    417424                    self.running = False 
    418425                    _debug_('%s downloaded' % self.filename) 
     426                    #print 'DJW:downloaded %s' % self.filename 
    419427                    # what we could do now is to add tags to track 
    420428                    break 
     
    424432            fd.close() 
    425433            f.close() 
     434            break 
     435        except ValueError, why: 
     436            _debug_('%s: %s' % (self.filename, why), DWARNING) 
    426437        except urllib2.HTTPError, why: 
    427438            _debug_('%s: %s' % (self.filename, why), DWARNING) 
    428         httplib.HTTPConnection.debuglevel = 0 
    429439 
    430440 
     
    457467    """ 
    458468    _version = '1.1.2' 
     469    headers = { 
     470        'User-agent': 'Freevo-%s (r%s)' % (version.__version__, revision.__revision__) 
     471    } 
    459472 
    460473    @benchmark(benchmarking, benchmarkcall) 
     
    495508        @returns: reply from request 
    496509        """ 
    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 
    533542 
    534543 
     
    612621 
    613622    @benchmark(benchmarking, benchmarkcall) 
    614     def download(self, url, filename): 
     623    def download(self, url, filename, istrack=False): 
    615624        """ 
    616625        Download album cover or track to last.fm directory. 
     
    625634            self._login() 
    626635        headers = { 
    627             'Session': self.session, 
     636            'Cookie': 'Session=%s' % self.session, 
     637            'User-agent': 'Freevo-%s (r%s)' % (version.__version__, revision.__revision__) 
    628638        } 
     639        if istrack: 
     640            headers.update({'Host': 'play.last.fm'}) 
    629641        self.downloader = LastFMDownloader(url, filename, headers) 
    630642        self.downloader.start()