Playlist logic

This commit is contained in:
Assistant 2020-05-09 10:28:39 -06:00
parent c9ac2b7b9c
commit 3379379a45
5 changed files with 120 additions and 13 deletions

View file

@ -13,16 +13,16 @@ namespace ModAssistant.API
private static readonly string CustomSongsFolder = Path.Combine("Beat Saber_Data", "CustomLevels");
private const bool BypassDownloadCounter = false;
public static async Task GetFromKey(string Key)
public static async Task GetFromKey(string Key, bool showNotification = true)
{
BeatSaverApiResponse Map = await GetResponse(BeatSaverURLPrefix + "/api/maps/detail/" + Key);
await InstallMap(Map);
await InstallMap(Map, showNotification);
}
public static async Task GetFromHash(string Hash)
public static async Task GetFromHash(string Hash, bool showNotification = true)
{
BeatSaverApiResponse Map = await GetResponse(BeatSaverURLPrefix + "/api/maps/by-hash/" + Hash);
await InstallMap(Map);
await InstallMap(Map, showNotification);
}
private static async Task<BeatSaverApiResponse> GetResponse(string url)
@ -41,21 +41,23 @@ namespace ModAssistant.API
}
}
public static async Task InstallMap(BeatSaverApiResponse Map)
public static async Task InstallMap(BeatSaverApiResponse Map, bool showNotification = true)
{
string zip = Path.Combine(Utils.BeatSaberPath, CustomSongsFolder, Map.hash) + ".zip";
string mapName = string.Concat(($"{Map.key} ({Map.metadata.songName} - {Map.metadata.levelAuthorName})")
.Split(ModAssistant.Utils.Constants.IllegalCharacters));
string directory = Path.Combine(Utils.BeatSaberPath, CustomSongsFolder, mapName);
#pragma warning disable CS0162 // Unreachable code detected
if (BypassDownloadCounter)
{
await Utils.DownloadAsset(BeatSaverURLPrefix + Map.directDownload, CustomSongsFolder, Map.hash + ".zip", mapName);
await Utils.DownloadAsset(BeatSaverURLPrefix + Map.directDownload, CustomSongsFolder, Map.hash + ".zip", mapName, showNotification);
}
else
{
await Utils.DownloadAsset(BeatSaverURLPrefix + Map.downloadURL, CustomSongsFolder, Map.hash + ".zip", mapName);
await Utils.DownloadAsset(BeatSaverURLPrefix + Map.downloadURL, CustomSongsFolder, Map.hash + ".zip", mapName, showNotification);
}
#pragma warning restore CS0162 // Unreachable code detected
if (File.Exists(zip))
{

View file

@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading.Tasks;
using static ModAssistant.Http;
using System.Windows;
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 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 = url.Segments.Last();
string absolutePath = Path.Combine(BeatSaberPath, PlaylistsFolder, filename);
try
{
await Utils.DownloadAsset(url.ToString(), PlaylistsFolder, filename);
return absolutePath;
}
catch
{
return null;
}
}
public static async Task DownloadFrom(string file)
{
Playlist playlist = JsonSerializer.Deserialize<Playlist>(File.ReadAllText(file));
int songCount = playlist.songs.Length;
int processedSongs = 0;
foreach (Playlist.Song song in playlist.songs)
{
if (!string.IsNullOrEmpty(song.hash))
{
await BeatSaver.GetFromHash(song.hash, false);
}
else if (!string.IsNullOrEmpty(song.key))
{
await BeatSaver.GetFromKey(song.key, false);
}
processedSongs++;
}
}
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; }
}
}
}
}

View file

@ -11,12 +11,18 @@ namespace ModAssistant.API
{
public static readonly string BeatSaberPath = App.BeatSaberInstallDirectory;
public static async Task DownloadAsset(string link, string folder, bool showNotifcation, string fileName = null)
{
await DownloadAsset(link, folder, fileName, null, showNotifcation);
}
public static async Task DownloadAsset(string link, string folder, string fileName = null, string displayName = null)
{
if (string.IsNullOrEmpty(displayName))
{
displayName = Path.GetFileNameWithoutExtension(fileName);
}
await DownloadAsset(link, folder, fileName, displayName, true);
}
public static async Task DownloadAsset(string link, string folder, string fileName, string displayName, bool showNotification)
{
if (string.IsNullOrEmpty(BeatSaberPath))
{
ModAssistant.Utils.SendNotify((string)Application.Current.FindResource("OneClick:InstallDirNotFound"));
@ -32,9 +38,16 @@ namespace ModAssistant.API
{
fileName = WebUtility.UrlDecode(Path.Combine(BeatSaberPath, folder, fileName));
}
if (string.IsNullOrEmpty(displayName))
{
displayName = Path.GetFileNameWithoutExtension(fileName);
}
await ModAssistant.Utils.Download(link, fileName);
ModAssistant.Utils.SendNotify(string.Format((string)Application.Current.FindResource("OneClick:InstalledAsset"), displayName));
if (showNotification)
{
ModAssistant.Utils.SendNotify(string.Format((string)Application.Current.FindResource("OneClick:InstalledAsset"), displayName));
}
}
catch
{

View file

@ -8,7 +8,7 @@ namespace ModAssistant
{
class OneClickInstaller
{
private static readonly string[] Protocols = new[] { "modelsaber", "beatsaver" };
private static readonly string[] Protocols = new[] { "modelsaber", "beatsaver", "bsplaylist" };
public static async Task InstallAsset(string link)
{
@ -23,6 +23,9 @@ namespace ModAssistant
case "beatsaver":
await BeatSaver(uri);
break;
case "bsplaylist":
await Playlist(uri);
break;
}
}
@ -37,6 +40,11 @@ namespace ModAssistant
await API.ModelSaber.GetModel(uri);
}
private static async Task Playlist(Uri uri)
{
await API.Playlists.DownloadAll(uri);
}
public static void Register(string Protocol, bool Background = false)
{
if (IsRegistered(Protocol) == true)

View file

@ -67,6 +67,7 @@
</ApplicationDefinition>
<Compile Include="Classes\External Interfaces\BeatSaver.cs" />
<Compile Include="Classes\External Interfaces\ModelSaber.cs" />
<Compile Include="Classes\External Interfaces\Playlists.cs" />
<Compile Include="Classes\External Interfaces\Utils.cs" />
<Compile Include="Classes\Http.cs" />
<Compile Include="Classes\HyperlinkExtensions.cs" />