[Audio] Playlist download addition (#2482)

Bot owners can use `[p]playlist download playlistname` to download a playlist file that is compatible with the playlist upload command. Songs can also be exported for v2 use by using `[p]playlist download playlistname True`, which strips out tracks besides YouTube or Soundcloud URLs.
This commit is contained in:
aikaterna 2019-03-02 19:29:42 -08:00 committed by Toby Harradine
parent 6051ccb23c
commit 2755592175

View File

@ -4,6 +4,8 @@ import datetime
import discord
from fuzzywuzzy import process
import heapq
from io import StringIO
import json
import lavalink
import logging
import math
@ -1248,6 +1250,44 @@ class Audio(commands.Cog):
return await self._embed_msg(ctx, _("No playlist with that name."))
await self._embed_msg(ctx, _("{name} playlist deleted.").format(name=playlist_name))
@checks.is_owner()
@playlist.command(name="download")
async def _playlist_download(self, ctx, playlist_name, v2=False):
"""Download a copy of a playlist.
These files can be used with the [p]playlist upload command.
Red v2-compatible playlists can be generated by passing True
for the v2 variable."""
if not await self._playlist_check(ctx):
return
playlists = await self.config.guild(ctx.guild).playlists.get_raw()
v2_valid_urls = ["https://www.youtube.com/watch?v=", "https://soundcloud.com/"]
song_list = []
playlist_url = None
try:
if playlists[playlist_name]["playlist_url"]:
playlist_url = playlists[playlist_name]["playlist_url"]
for track in playlists[playlist_name]["tracks"]:
if v2:
if track["info"]["uri"].startswith(tuple(v2_valid_urls)):
song_list.append(track["info"]["uri"])
else:
song_list.append(track["info"]["uri"])
except TypeError:
return await self._embed_msg(ctx, _("That playlist has no tracks."))
except KeyError:
return await self._embed_msg(ctx, _("That playlist doesn't exist."))
playlist_data = json.dumps(
{"author": ctx.author.id, "link": playlist_url, "playlist": song_list}
)
to_write = StringIO()
to_write.write(playlist_data)
to_write.seek(0)
await ctx.send(file=discord.File(to_write, filename=f"{playlist_name}.txt"))
to_write.close()
@playlist.command(name="info")
async def _playlist_info(self, ctx, playlist_name):
"""Retrieve information from a saved playlist."""