def clean(): """Cleanup orphan tracks and empty playlists.""" tracks = [] removed_playlists = 0 for playlist in PlaylistManager.find(): if len(playlist.tracks) == 0: PlaylistManager.remove(playlist.id) removed_playlists += 1 else: tracks += playlist.tracks tracks = list(set(tracks)) removed_tracks = 0 for track in TrackManager.find(): if track.id not in tracks: TrackManager.remove(track.id) removed_tracks += 1 click.secho("Cleanup removed:", bold=True) click.secho( tabulate( # type: ignore [ (magenta("Tracks:"), removed_tracks), (magenta("Playlists:"), removed_playlists), ], tablefmt="plain", colalign=("right", "left"), ))
def create_playlist(title, tracks, type, arguments): if not tracks: return click.secho("Tracklist is empty, aborting...") click.clear() click.secho("{}\n\n{}\n".format( tabulate( # type: ignore [ (magenta("Title:"), title), (magenta("Tracks:"), len(tracks)), ], tablefmt="plain", colalign=("right", "left"), ), tabulate( # type: ignore [(i + 1, track[0], track[1]) for i, track in enumerate(tracks)], headers=("No", "Artist", "Track Name"), ), )) click.confirm("Are you sure you want to save this playlist?", abort=True) playlist = PlaylistManager.set( dict( type=type, title=title.strip(), arguments=arguments, provider=Provider.user, tracks=[ TrackManager.set(dict(artist=artist, name=name)).id for artist, name in tracks ], )) click.secho("Added playlist: {}!".format(playlist.id))
def show(id: Optional[str], mime: bool = False): """Show a playlist track list.""" playlist = PlaylistManager.get(id) values = [ (magenta("ID:"), playlist.id), (magenta("Provider:"), playlist.provider), (magenta("Title:"), playlist.title), (magenta("Type:"), playlist.type), (magenta("Arguments:"), playlist.display_arguments), (magenta("Synced:"), date(playlist.synced)), (magenta("Uploaded:"), date(playlist.uploaded)), (magenta("Youtube:"), playlist.youtube_url), ] if mime: values.append((magenta("Mime:"), playlist.mime)) else: values.append((magenta("Tracks:"), "")) info = tabulate( # type: ignore values, tablefmt="plain", colalign=("right", "left")) if mime: click.secho(info) click.secho("\nYou reached the 10 playlists limit per day on youtube?") click.secho("Create a playlist manually and add " "in the bottom the above mime string") click.secho("The mime is base64 signature that " "pytuber uses to link with youtube playlists") click.secho('Use "pytuber fetch youtube --playlists"' " to sync, before you try to push any tracks") return tracks = tabulate( # type: ignore [( t.artist, t.name, click.style("✔", fg="green") if t.youtube_id else "-", ) for t in [TrackManager.get(id) for id in playlist.tracks]], showindex="always", headers=("No", "Artist", "Track Name", "Youtube"), colalign=("left", "left", "left", "center"), ) click.echo_via_pager("{}\n\n{}".format(info, tracks))
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))
def quota(): """Show current youtube calculated quota usage.""" limit = ConfigManager.get(Provider.youtube).data["quota_limit"] usage = YouService.get_quota_usage() pt_date = YouService.quota_date(obj=True) next_reset = timedelta( hours=23 - pt_date.hour, minutes=59 - pt_date.minute, seconds=60 - pt_date.second, ) values = [ (magenta("Provider:"), Provider.youtube), (magenta("Limit:"), limit), (magenta("Usage:"), usage), (magenta("Next reset:"), str(next_reset)), ] click.secho( tabulate( # type: ignore values, tablefmt="plain", colalign=("right", "left")))
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))