예제 #1
0
        public static async Task <Song> FetchByID(string id)
        {
            using (WebClient w = new WebClient())
            {
                try
                {
                    string response = await w.DownloadStringTaskAsync($"{BeatSaverAPI}/detail/{id}");

                    JsonResponseDetails json = JsonConvert.DeserializeObject <JsonResponseDetails>(response);
                    return(json.Song);
                }
                catch (WebException e)
                {
                    Logger.Instance.Warning($"Unable to fetch song with key {id}! Error code: {(int)((HttpWebResponse)e.Response).StatusCode}");
                    return(null);
                }
                catch (Exception e)
                {
#if DEBUG
                    Logger.Instance.Exception($"Unable to fetch song with key {id}! Exception: {e}");
#endif
                    return(null);
                }
            }
        }
예제 #2
0
        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);
                }
            }
        }