Esempio n. 1
0
    def browse_album(self, album, callback):
        ''' Browse an album, invoking the callback when done

        :param album:          An album instance to browse.
        :param callback:       A callback to invoke when the album is loaded.
                               Should take the browser as a single parameter.
        '''
        link = Link.from_album(album)

        def callback_wrapper(browser, userdata):
            self.log("Album browse complete: %s" % link)
            callback(browser)

        self.log("Browse album: %s" % link)
        return AlbumBrowser(album, callback_wrapper)
Esempio n. 2
0
    def run(self):
        # wait for container
        container_loaded.wait()
        container_loaded.clear()

        while True:
            link_string = self._queue.next_link()

            if link_string == '':
                break

            link = Link.from_string(link_string)

            if link.type() == Link.LINK_TRACK:
                track = link.as_track()
                itrack = iter([track])

            elif link.type() == Link.LINK_PLAYLIST \
                    or link_string == 'spotify:user:'******'username')+':starred':
                print 'Loading playlist %s ...' % link_string

                playlist = link.as_playlist()

                while not playlist.is_loaded():
                    time.sleep(0.1)

                itrack = iter(playlist)

            elif link.type() == Link.LINK_ALBUM:
                print 'Processing album %s' % str(link)
                itrack = [ ]
                album = link.as_album()
                album_browser = AlbumBrowser(album)

                while not album_browser.is_loaded():
                    time.sleep(0.1)

                if self._util.is_compilation(album):
                    continue

                if album.is_available():
                    print 'Getting tracks for %s' % album.name()
                    for track in album_browser:
                        itrack.append(track)

            elif link.type() == Link.LINK_ARTIST:
                print "Processing artist %s ..." % str(link)

                artist         = link.as_artist()
                artist_browser = ArtistBrowser(artist, 'no_tracks')

                while not artist_browser.is_loaded():
                    time.sleep(0.1)

                print "Artist loaded"
                print(artist.name())

                similar_artists = artist_browser.similar_artists()

                for similar_artist in similar_artists:
                    self._queue.add_artist_link(similar_artist, 'similar')

                albums           = artist_browser.albums()
                processed_albums = [ ]
                itrack           = [ ]

                for album in albums:
                    if self._util.is_compilation(album):
                        continue

                    if album.is_available() and Link.from_album(album) \
                            not in processed_albums:
                        processed_albums.append(Link.from_album(album))
                        print 'Getting tracks for %s' % album.name()
                        album_browser = AlbumBrowser(album)

                        while not album_browser.is_loaded():
                            time.sleep(0.1)

                        for track in album_browser:
                            itrack.append(track)

            else:
                print "Unrecognised link"
                os._exit(2)
                return

            # ripping loop
            session = self._ripper.session

            for track in itrack:
                if self._util.is_known_not_available(Link.from_track(track)):
                    continue

                try:
                    self._ripper.load_track(track)

                    while not track.is_loaded():
                        time.sleep(0.1)

                    for track_artist in track.artists():
                        self._queue.add_artist_link(track_artist, 'track')

                    if self._ripper.rip_init(session, track):
                        self._ripper.play()
                        self._end_of_track.wait()
                        self._end_of_track.clear()
                        self._ripper.rip_terminate(session, track)
                        self._ripper.rip_id3(session, track)

                except TrackNotAvailableException:
                    print "Track not available (%s)" % track.name()
                    self._util.mark_as_not_available(Link.from_track(track))

        self._ripper.disconnect()
Esempio n. 3
0
    def run(self):
        # wait for container
        container_loaded.wait()
        container_loaded.clear()

        # output dir
        outputdir = os.getcwd()
        if args.outputdir != None:
            outputdir = os.path.normpath(os.path.realpath(args.outputdir[0]))

        session = self.ripper.session

        # create track iterator
        link = Link.from_string(args.url[0])
        if link.type() == Link.LINK_TRACK:
            track = link.as_track()
            itrack = iter([track])
        elif link.type() == Link.LINK_PLAYLIST or link.type() == Link.LINK_STARRED:
            playlist = link.as_playlist()
            print('loading playlist ...')
            while not playlist.is_loaded():
                print(' pending playlist ')
                time.sleep(0.5)
            print('done')
            itrack = iter(playlist)
        elif link.type() == Link.LINK_ALBUM:
            album = AlbumBrowser(link.as_album())
            print('loading album ...')            
            while not album.is_loaded():
                print(' pending album ')
                time.sleep(0.5)
            print('done')
            itrack = iter(album)
        elif link.type() == Link.LINK_ARTIST:
            artist = ArtistBrowser(link.as_artist())
            print('loading artist')
            while not artist.is_loaded():
                print(' pending artist ')
                time.sleep(0.5)
            print('done')
            itrack = iter(artist)
                  

        # ripping loop
        count = 0
        for track in itrack:
                count += 1
                # if the track is not loaded, track.availability is not ready
                self.ripper.load_track(track)
                if interrupt.isSet():
                    break
                while not track.is_loaded():
                    time.sleep(0.1)
                if track.availability() != 1:
                    print('Skipping. Track not available')
                else:
                    #self.ripper.load_track(track)
                    exists = library_track_exists(track, outputdir)
                    if exists:
                        print("Skipping. Track found at " + exists)
                    else:
                        try:
                            rip_init(session, track, outputdir)

                            self.ripper.play()

                            end_of_track.wait()
                            end_of_track.clear() # TODO check if necessary

                            rip_terminate(session, track)
                            if interrupt.isSet():
                                rip_delete(track, outputdir)
                                break
                            rip_id3(session, track, outputdir)
                        except (KeyboardInterrupt, SystemExit):
                            raise
                        except Exception as inst:
                            if not args.ignoreerrors:
                                raise
                            print("Unexpected error: ", type(inst))
                            print(inst)
                            print("Skipping to next track, if in playlist")

        self.ripper.disconnect()