public static async Task <Song> FetchByHash(string hash) { using (WebClient w = new WebClient()) { try { string response = await w.DownloadStringTaskAsync($"{BeatSaverAPI}/search/hash/{hash}"); JsonResponseSearch json = JsonConvert.DeserializeObject <JsonResponseSearch>(response); if (json.Total > 0) { return(json.Songs[0]); } else { Logger.Instance.Log($"Song wih hash {hash} not found!"); return(null); } } catch (Exception e) { #if DEBUG Logger.Instance.Exception(e); #endif return(null); } } }
public static async Task <SongInfo> GetRandomSong() { using (WebClient w = new WebClient()) { w.Headers.Add("user-agent", $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}/{System.Reflection.Assembly.GetExecutingAssembly().GetName().Version}"); Random rand = new Random(); int maxId = 11000; try { string response = await w.DownloadStringTaskAsync($"{BeatSaverAPI}/latest"); JsonResponseSearch json = JsonConvert.DeserializeObject <JsonResponseSearch>(response); if (json.Songs.Length > 0) { maxId = Convert.ToInt32(json.Songs[0].Key, 16); } bool found = false; do { try { response = await w.DownloadStringTaskAsync($"{BeatSaverAPI}/detail/{rand.Next(1, maxId).ToString("x")}"); found = true; } catch (WebException wex) { if (((HttpWebResponse)wex.Response).StatusCode != HttpStatusCode.NotFound) { throw; } } } while (!found); Song jsonSong = JsonConvert.DeserializeObject <Song>(response); return(new SongInfo() { levelId = jsonSong.Hash.ToUpper(), songName = jsonSong.Name, key = jsonSong.Key }); } catch (Exception e) { #if DEBUG Logger.Instance.Exception(e); #endif return(null); } } }
public static async Task <SongInfo> GetRandomSong() { using (WebClient w = new WebClient()) { Random rand = new Random(); int maxId = 11000; try { string response = await w.DownloadStringTaskAsync($"{BeatSaverAPI}/new"); JsonResponseSearch json = JsonConvert.DeserializeObject <JsonResponseSearch>(response); if (json.Total > 0) { maxId = json.Songs[0].Id; } bool found = false; do { try { response = await w.DownloadStringTaskAsync($"{BeatSaverAPI}/detail/{rand.Next(1, maxId)}"); found = true; } catch (WebException wex) { if (((HttpWebResponse)wex.Response).StatusCode != HttpStatusCode.NotFound) { throw; } } } while (!found); JsonResponseDetails jsonDetails = JsonConvert.DeserializeObject <JsonResponseDetails>(response); return(new SongInfo() { levelId = jsonDetails.Song.HashMD5.ToUpper(), songName = jsonDetails.Song.Name, key = jsonDetails.Song.Key }); } catch (Exception e) { #if DEBUG Logger.Instance.Exception(e); #endif return(null); } } }