示例#1
0
    def test_spinner(self, yaspin):
        type(yaspin.return_value).green = PropertyMock(return_value=yaspin)
        with spinner("foo") as sp:
            self.assertEqual(sp, yaspin.return_value)

        yaspin.return_value.start.assert_called_once_with()
        yaspin.return_value.stop.assert_called_once_with()
示例#2
0
def fetch_tracks(*args):
    kwargs = dict(provider=Provider.lastfm)
    if args:
        kwargs["id"] = lambda x: x in args

    # So wrong, but yaspin doesn't support nested spinners
    LastService.get_tags()
    with spinner("Fetching track lists") as sp:
        for playlist in PlaylistManager.find(**kwargs):
            tracklist = LastService.get_tracks(
                type=playlist.type, **playlist.arguments
            )

            track_ids: List[str] = []
            for entry in tracklist:
                id = TrackManager.set(
                    dict(artist=entry.artist.name, name=entry.name)
                ).id

                if id not in track_ids:
                    track_ids.append(id)

            sp.write(
                "Playlist: {} - {} tracks".format(playlist.id, len(track_ids))
            )
            PlaylistManager.update(playlist, dict(tracks=track_ids))
示例#3
0
    def test_spinner_with_exception(self, yaspin, secho):
        type(yaspin.return_value).green = PropertyMock(return_value=yaspin)
        with spinner("foo"):
            raise Exception("Fatal")

        yaspin.return_value.start.assert_called_once_with()
        yaspin.return_value.stop.assert_called_once_with()
        secho.assert_called_once_with("Fatal")
示例#4
0
 def retrieve_tags():
     page = 1
     tags = []  # type: List[dict]
     with spinner("Fetching tags"):
         while len(tags) < 1000:
             tags.extend([
                 t.to_dict()
                 for t in Tag.get_top_tags(limit=250, page=page)
             ])
             page += 1
     return tags
示例#5
0
def fetch_tracks():
    tracks = TrackManager.find(youtube_id=None)
    message = "Matching tracks to videos"
    with spinner(message) as sp:
        for track in tracks:
            sp.text = "{}: {} - {}".format(message, track.artist, track.name)
            youtube_id = YouService.search_track(track)
            TrackManager.update(track, dict(youtube_id=youtube_id))

        total = len(tracks)
        if total > 0:
            sp.text = "Matched {} tracks to videos".format(magenta(total))
示例#6
0
def push_playlists():
    playlists = PlaylistManager.find(youtube_id=None)
    message = "Creating playlists"
    with spinner(message) as sp:
        for playlist in playlists:
            sp.text = "{}: {}".format(message, playlist.title)
            youtube_id = YouService.create_playlist(playlist)
            PlaylistManager.update(playlist, dict(youtube_id=youtube_id))

        total = len(playlists)
        if total > 0:
            sp.text = "{0}: {1}/{1} ".format(message, total)
示例#7
0
def push_tracks():
    online_playlists = PlaylistManager.find(youtube_id=lambda x: x is not None)
    click.secho("Syncing playlists", bold=True)
    for playlist in online_playlists:
        add = items = remove = []
        with spinner("Fetching playlist items: {}".format(playlist.title)):
            items = YouService.get_playlist_items(playlist)
            online = set([item.video_id for item in items])
            offline = set([
                track.youtube_id for track in TrackManager.find(
                    youtube_id=lambda x: x is not None,
                    id=lambda x: x in playlist.tracks,
                )
            ])

            add = offline - online
            remove = online - offline

        message = "Adding new playlist items"
        with spinner(message) as sp:
            for video_id in sorted(add):
                sp.text = "{}: {}".format(message, video_id)
                YouService.create_playlist_item(playlist, video_id)

            if len(add) > 0:
                sp.text = "{}: {}".format(message, len(add))

        message = "Removing playlist items"
        with spinner(message) as sp:
            remove = [item for item in items if item.video_id in remove]
            for item in sorted(remove):
                sp.text = "{}: {}".format(message, video_id)
                YouService.remove_playlist_item(item)

            if len(remove) > 0:
                sp.text = "{}: {}".format(message, len(remove))

        if len(add) or len(remove):
            PlaylistManager.update(playlist, dict(uploaded=timestamp()))
示例#8
0
def fetch_playlists():
    with spinner("Fetching playlists info") as sp:
        playlists = YouService.get_playlists()
        for playlist in playlists:
            if not PlaylistManager.exists(playlist):
                items = YouService.get_playlist_items(playlist)
                track_ids = [
                    TrackManager.set(
                        dict(
                            artist=item.artist,
                            name=item.name,
                            youtube_id=item.video_id,
                        )).id for item in items
                ]
                playlist.tracks = track_ids
            PlaylistManager.set(playlist.asdict())

        total = len(playlists)
        if total > 0:
            sp.text = "Fetched {} playlist(s) info".format(magenta(total))