public void GetBeatmaps(MapSort mapSort, int page = 0) { MainWindow.ToggleLoading(true, "Loading online beatmaps", $"Sorting by: {mapSort}"); _ = Task.Run(async() => { try { OnlineBeatmaps = await App.BeatSaverApi.GetOnlineBeatmaps(mapSort, page); } catch (Exception e) { string description = e.Message; if (e.InnerException != null && !e.Message.Contains(e.InnerException.Message)) { description += $" ({e.InnerException.Message})"; } await MainWindow.ShowMessageAsync("Online Beatmaps", description); userControl.radioButtonSearch.IsChecked = false; userControl.radioButtonHot.IsChecked = false; userControl.radioButtonRating.IsChecked = false; userControl.radioButtonLatest.IsChecked = false; userControl.radioButtonDownloads.IsChecked = false; userControl.radioButtonPlays.IsChecked = false; } }); }
public void GetBeatmaps(string query, int page = 0) { string description = null; if (CurrentMapSort == MapSort.Search) { description = $"Searching by name: {query}"; } else if (CurrentMapSort == MapSort.SearchKey) { description = $"Searching by key: {query}"; } else if (CurrentMapSort == MapSort.SearchHash) { description = $"Searching by hash: {query}"; } MainWindow.ToggleLoading(true, "Loading online beatmaps", description); _ = Task.Run(async() => { (bool isValid, OnlineBeatmaps onlineBeatmaps) = await App.BeatSaverApi.GetOnlineBeatmaps(query, CurrentMapSort, page); if (isValid) { OnlineBeatmaps = onlineBeatmaps; } else { await MainWindow.Dispatcher.Invoke(async() => { await MainWindow.ShowMessageAsync("Can't connect to BeatSaver", "Either you don't have any internet connection or BeatSaver is currently offline"); userControl.radioButtonSearch.IsChecked = false; userControl.radioButtonHot.IsChecked = false; userControl.radioButtonRating.IsChecked = false; userControl.radioButtonLatest.IsChecked = false; userControl.radioButtonDownloads.IsChecked = false; userControl.radioButtonPlays.IsChecked = false; }); } }); }
public async Task <OnlineBeatmaps> GetOnlineBeatmaps(MapSort mapSort, int page = 0) { try { using (WebClient webClient = new WebClient()) { string projectName = Assembly.GetEntryAssembly().GetName().Name; webClient.Headers.Add(HttpRequestHeader.UserAgent, projectName); string json = null; switch (mapSort) { case MapSort.Hot: json = await webClient.DownloadStringTaskAsync($"{beatSaverHotApi}/{page}"); break; case MapSort.Rating: json = await webClient.DownloadStringTaskAsync($"{beatSaverRatingApi}/{page}"); break; case MapSort.Latest: json = await webClient.DownloadStringTaskAsync($"{beatSaverLatestApi}/{page}"); break; case MapSort.Downloads: json = await webClient.DownloadStringTaskAsync($"{beatSaverDownloadsApi}/{page}"); break; case MapSort.Plays: json = await webClient.DownloadStringTaskAsync($"{beatSaverPlaysApi}/{page}"); break; default: break; } OnlineBeatmaps beatSaverMaps = JsonConvert.DeserializeObject <OnlineBeatmaps>(json); string[] songsDownloaded = Directory.GetDirectories(SongsPath); foreach (OnlineBeatmap song in beatSaverMaps.Maps) { song.Metadata.DurationTimeSpan = new TimeSpan(0, 0, song.Metadata.Duration); string folder = songsDownloaded.FirstOrDefault(x => new DirectoryInfo(x).Name.Split(" ")[0] == song.Key || new DirectoryInfo(x).Name.Split(" ")[0] == song.Hash); foreach (Characteristic characteristic in song.Metadata.Characteristics) { if (characteristic.Difficulties.Easy != null) { SetOnlineNoteInformation(song, characteristic.Difficulties.Easy); } if (characteristic.Difficulties.Normal != null) { SetOnlineNoteInformation(song, characteristic.Difficulties.Normal); } if (characteristic.Difficulties.Hard != null) { SetOnlineNoteInformation(song, characteristic.Difficulties.Hard); } if (characteristic.Difficulties.Expert != null) { SetOnlineNoteInformation(song, characteristic.Difficulties.Expert); } if (characteristic.Difficulties.ExpertPlus != null) { SetOnlineNoteInformation(song, characteristic.Difficulties.ExpertPlus); } } if (string.IsNullOrEmpty(folder)) { if (Downloading.Any(x => x.Key == song.Key)) { song.IsDownloading = true; } } else { song.IsDownloaded = true; song.Metadata.FolderPath = folder; } } return(beatSaverMaps); } } catch (WebException e) { throw new WebException("Can't connect to Beat Saver", e.InnerException); } catch (Exception) { throw; } }
public async Task <(bool isValid, OnlineBeatmaps onlineBeatmaps)> GetOnlineBeatmaps(string query, MapSort mapSort, int page = 0) { try { using (WebClient webClient = new WebClient()) { string projectName = Assembly.GetEntryAssembly().GetName().Name; webClient.Headers.Add(HttpRequestHeader.UserAgent, projectName); OnlineBeatmaps beatSaverMaps = null; if (mapSort == MapSort.Search) { string json = await webClient.DownloadStringTaskAsync($"{beatSaverSearchApi}/{page}?q={query}"); beatSaverMaps = JsonConvert.DeserializeObject <OnlineBeatmaps>(json); } else if (mapSort == MapSort.SearchKey) { string json = await webClient.DownloadStringTaskAsync($"{beatSaverDetailsKeyApi}/{query}"); beatSaverMaps = new OnlineBeatmaps { Maps = new OnlineBeatmap[] { JsonConvert.DeserializeObject <OnlineBeatmap>(json) } }; } else if (mapSort == MapSort.SearchHash) { string json = await webClient.DownloadStringTaskAsync($"{beatSaverDetailsHashApi}/{query}"); beatSaverMaps = new OnlineBeatmaps { Maps = new OnlineBeatmap[] { JsonConvert.DeserializeObject <OnlineBeatmap>(json) } }; } if (beatSaverMaps != null) { string[] songsDownloaded = Directory.GetDirectories(SongsPath); foreach (OnlineBeatmap song in beatSaverMaps.Maps) { song.Metadata.DurationTimeSpan = new TimeSpan(0, 0, song.Metadata.Duration); string folder = songsDownloaded.FirstOrDefault(x => new DirectoryInfo(x).Name.Split(" ")[0] == song.Key || new DirectoryInfo(x).Name.Split(" ")[0] == song.Hash); foreach (Characteristic characteristic in song.Metadata.Characteristics) { if (characteristic.Difficulties.Easy != null) { SetOnlineNoteInformation(song, characteristic.Difficulties.Easy); } if (characteristic.Difficulties.Normal != null) { SetOnlineNoteInformation(song, characteristic.Difficulties.Normal); } if (characteristic.Difficulties.Hard != null) { SetOnlineNoteInformation(song, characteristic.Difficulties.Hard); } if (characteristic.Difficulties.Expert != null) { SetOnlineNoteInformation(song, characteristic.Difficulties.Expert); } if (characteristic.Difficulties.ExpertPlus != null) { SetOnlineNoteInformation(song, characteristic.Difficulties.ExpertPlus); } } if (string.IsNullOrEmpty(folder)) { if (Downloading.Any(x => x.Key == song.Key)) { song.IsDownloading = true; } } else { song.IsDownloaded = true; song.Metadata.FolderPath = folder; } } } return(true, beatSaverMaps); } } catch (Exception e) { if (e.Message.Contains("(404) Not Found")) { return(true, null); } return(false, null); } }