VRCMelonAssistant/ModAssistant/Classes/External Interfaces/Playlists.cs

127 lines
4.6 KiB
C#
Raw Normal View History

2020-05-10 04:28:39 +12:00
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
2020-05-10 04:28:39 +12:00
using System.Windows;
2020-11-17 02:22:30 +13:00
using static ModAssistant.Http;
2020-05-10 04:28:39 +12:00
namespace ModAssistant.API
{
public class Playlists
{
private const string BSaberURLPrefix = "https://bsaber.com/PlaylistAPI/";
private const string PlaylistsFolder = "Playlists";
private static readonly string BeatSaberPath = Utils.BeatSaberPath;
public static void CreatePlaylistsFolder()
{
string playlistsPath = Path.Combine(BeatSaberPath, PlaylistsFolder);
Directory.CreateDirectory(playlistsPath);
}
2020-05-10 04:28:39 +12:00
public static async Task DownloadAll(Uri uri)
{
switch (uri.Host)
{
case "playlist":
Uri url = new Uri($"{uri.LocalPath.Trim('/')}");
string filename = await Get(url);
await DownloadFrom(filename);
break;
}
}
public static async Task<string> Get(Uri url)
{
string filename = HttpUtility.UrlDecode(url.Segments.Last());
2020-05-10 04:28:39 +12:00
string absolutePath = Path.Combine(BeatSaberPath, PlaylistsFolder, filename);
try
{
2020-10-23 03:34:34 +13:00
CreatePlaylistsFolder();
2020-05-10 04:28:39 +12:00
await Utils.DownloadAsset(url.ToString(), PlaylistsFolder, filename);
2020-10-23 03:34:34 +13:00
2020-05-10 04:28:39 +12:00
return absolutePath;
}
catch
{
return null;
}
}
2020-05-20 05:48:25 +12:00
public static async Task DownloadFrom(string file)
2020-05-10 04:28:39 +12:00
{
2020-10-23 03:34:34 +13:00
CreatePlaylistsFolder();
2020-05-19 04:24:58 +12:00
if (Path.Combine(BeatSaberPath, PlaylistsFolder) != Path.GetDirectoryName(file))
{
string destination = Path.Combine(BeatSaberPath, PlaylistsFolder, Path.GetFileName(file));
File.Copy(file, destination, true);
}
2020-05-19 03:05:42 +12:00
int Errors = 0;
int Minimum = 0;
int Value = 0;
2020-05-20 05:48:25 +12:00
2020-05-10 04:28:39 +12:00
Playlist playlist = JsonSerializer.Deserialize<Playlist>(File.ReadAllText(file));
2020-05-19 03:05:42 +12:00
int Maximum = playlist.songs.Length;
2020-05-10 04:28:39 +12:00
foreach (Playlist.Song song in playlist.songs)
{
2020-05-19 03:05:42 +12:00
API.BeatSaver.BeatSaverMap response = new BeatSaver.BeatSaverMap();
2020-05-10 04:28:39 +12:00
if (!string.IsNullOrEmpty(song.hash))
{
2020-05-19 03:05:42 +12:00
response = await BeatSaver.GetFromHash(song.hash, false);
2020-05-10 04:28:39 +12:00
}
else if (!string.IsNullOrEmpty(song.key))
{
2020-05-19 03:05:42 +12:00
response = await BeatSaver.GetFromKey(song.key, false);
2020-05-10 04:28:39 +12:00
}
2020-05-19 03:05:42 +12:00
Value++;
2020-05-20 05:48:25 +12:00
if (response.Success)
{
Utils.SetMessage($"{string.Format((string)Application.Current.FindResource("Options:InstallingPlaylist"), TextProgress(Minimum, Maximum, Value))} {response.Name}");
}
else
2020-05-19 03:05:42 +12:00
{
2020-05-20 05:48:25 +12:00
Utils.SetMessage($"{string.Format((string)Application.Current.FindResource("Options:FailedPlaylistSong"), song.songName)}");
ModAssistant.Utils.Log($"Failed installing BeatSaver map: {song.songName} | {song.key} | {song.hash} | ({response?.response?.ratelimit?.Remaining})");
App.CloseWindowOnFinish = false;
2020-05-20 05:48:25 +12:00
await Task.Delay(3 * 1000);
Errors++;
2020-05-19 03:05:42 +12:00
}
}
Utils.SetMessage($"{string.Format((string)Application.Current.FindResource("Options:FinishedPlaylist"), Errors, playlist.playlistTitle)}");
2020-05-10 04:28:39 +12:00
}
2020-05-19 03:05:42 +12:00
private static string TextProgress(int min, int max, int value)
{
if (max == value)
{
return $" {string.Concat(Enumerable.Repeat("", 10))} [{value}/{max}]";
}
2020-11-17 03:19:59 +13:00
int interval = (int)Math.Floor((double)value / (((double)max - (double)min) / (double)10));
2020-05-19 03:05:42 +12:00
return $" {string.Concat(Enumerable.Repeat("", interval))}{string.Concat(Enumerable.Repeat("", 10 - interval))} [{value}/{max}]";
}
2020-05-10 04:28:39 +12:00
2020-11-17 03:08:18 +13:00
#pragma warning disable IDE1006 // Naming Styles
2020-05-10 04:28:39 +12:00
class Playlist
{
public string playlistTitle { get; set; }
public string playlistAuthor { get; set; }
public string image { get; set; }
public Song[] songs { get; set; }
public class Song
{
public string key { get; set; }
public string hash { get; set; }
public string songName { get; set; }
public string uploader { get; set; }
}
}
}
}
2020-11-17 03:08:18 +13:00
#pragma warning restore IDE1006 // Naming Styles