internal static void ConvertAPIList(NewAPISongList from, APISongList to, int startIdx) { for (int idx = 0; idx < from.maps.Length; idx++) { Song newSong = new Song(); SongV2 song = from.maps[idx]; newSong.title = song.title; newSong.artist = song.artist; newSong.author = song.author; for (int diffIdx = 0; diffIdx < song.difficulties.Length; diffIdx++) { if (song.difficulties[diffIdx] == "beginner") { newSong.beginner = true; } else if (song.difficulties[diffIdx] == "moderate") { newSong.standard = true; } else if (song.difficulties[diffIdx] == "advanced") { newSong.advanced = true; } else if (song.difficulties[diffIdx] == "expert") { newSong.expert = true; } } newSong.download_url = string.Format(downloadUrlFormat, song.id); newSong.upload_time = song.created_at; newSong.update_time = song.updated_at; newSong.video_url = song.embed_url; newSong.filename = song.filename; newSong.song_id = song.filename.Remove(song.filename.Length - 7); // remove the .audica from the filename to get the hash-less ID newSong.preview_url = string.Format(previewUrlFormat, song.id); to.songs[startIdx + idx] = newSong; } }
/// <summary> /// Coroutine that searches for songs using the web API /// </summary> /// <param name="search">Query text, e.g. song name, artist or mapper (partial matches possible)</param> /// <param name="onSearchComplete">Called with search result once search is completed, use to process search result</param> /// <param name="difficulty">Only find songs with given difficulty</param> /// <param name="curated">Only find songs that are curated</param> /// <param name="sortByPlayCount">Sort result by play count</param> /// <param name="page">Page to return (see APISongList.total_pages after initial search (page = 1) to check if multiple pages exist)</param> /// <param name="total">Bypasses all query and filter limitations and just returns entire song list (or max page size)</param> public static IEnumerator DoSongWebSearch(string search, Action <string, APISongList> onSearchComplete, DifficultyFilter difficulty, bool curated = false, bool sortByPlayCount = false, int page = 1, bool total = false, bool searchIsFilename = false) { if (UseNewAPI) { if (total) { string webSearch = "https://beta.maudica.com/api/maps?per_page=100&page={0}"; // initial search to initialize result list WWW www = new WWW(string.Format(webSearch, 1)); yield return(www); NewAPISongList list = JSON.Load(www.text).Make <NewAPISongList>(); APISongList result = new APISongList(); result.song_count = list.count; result.page = 1; result.pagesize = list.count; result.total_pages = 1; result.songs = new Song[list.count]; int numPages = (int)Math.Ceiling((double)list.count / 100); int currPage = 1; ConvertAPIList(list, result, 0); while (currPage <= numPages) { currPage++; www = new WWW(string.Format(webSearch, currPage)); yield return(www); list = JSON.Load(www.text).Make <NewAPISongList>(); ConvertAPIList(list, result, 100 * (currPage - 1)); } onSearchComplete(search, result); } else { string webSearch; string concatURL; if (searchIsFilename) { webSearch = search == null || search == "" ? "" : "&filename=" + WebUtility.UrlEncode(search); concatURL = newApiUrl + webSearch; } else { webSearch = search == null || search == "" ? "" : "&search=" + WebUtility.UrlEncode(search); string webPage = page == 1 ? "" : "&page=" + page.ToString(); string webDifficulty = difficulty == DifficultyFilter.All ? "" : "&difficulties%5B%5D=" + DifficultyToNewAPIValue(difficulty); string webDownloads = sortByPlayCount ? "&sort=downloads" : ""; concatURL = newApiUrl + webSearch + webDifficulty + webPage + webDownloads; } WWW www = new WWW(concatURL); yield return(www); NewAPISongList list = JSON.Load(www.text).Make <NewAPISongList>(); // convert to existing SongList format, then return APISongList result = new APISongList(); result.song_count = list.count; result.page = page; result.pagesize = 14; result.total_pages = (int)Math.Ceiling(result.song_count / (double)result.pagesize); result.songs = new Song[list.maps.Length]; ConvertAPIList(list, result, 0); onSearchComplete(search, result); } } else { string webSearch = search == null || search == "" ? "" : "&search=" + WebUtility.UrlEncode(search); string webPage = page == 1 ? "" : "&page=" + page.ToString(); string diff = difficulty.ToString(); string webDifficulty = diff == "All" ? "" : "&" + diff.ToLower() + "=true"; string webCurated = curated ? "&curated=true" : ""; string webPlaycount = sortByPlayCount ? "&sort=leaderboards" : ""; string concatURL = !total ? apiURL + webSearch + webDifficulty + webPage + webCurated + webPlaycount : "http://www.audica.wiki:5000/api/customsongs?pagesize=all"; WWW www = new WWW(concatURL); yield return(www); onSearchComplete(search, JSON.Load(www.text).Make <APISongList>()); } }