示例#1
0
        public void SetSongs(List <Song> levels)
        {
            //Now that songs are being set, hide the "downloading" text
            if (errorHappened)
            {
                return;                //If there was an error earlier, don't continue
            }
            songsTableView.gameObject.SetActive(true);
            _pageUpButton.gameObject.SetActive(true);
            _pageDownButton.gameObject.SetActive(true);
            _infoText.gameObject.SetActive(false);

            availableSongs = levels;

            if (songsTableView.dataSource != (TableView.IDataSource) this)
            {
                songsTableView.dataSource = this;
            }
            else
            {
                songsTableView.ReloadData();
            }

            songsTableView.ScrollToCellWithIdx(0, TableViewScroller.ScrollPositionType.Beginning, false);
            if (selectWhenLoaded != null)
            {
                int songIndex = availableSongs.IndexOf(availableSongs.Where(x => x.Beatmap?.level.levelID == selectWhenLoaded).First());
                songsTableView.SelectCellWithIdx(songIndex, true);
            }
            SongsDownloaded?.Invoke();
        }
示例#2
0
 private void ResetProgress()
 {
     SongsDownloaded.Clear();
     SongsToDownload.Clear();
     IsActive = true;
     IsError  = false;
 }
示例#3
0
        private bool DownloadTrack(Track song, string apiKey)
        {
            bool downloaded = false;

            if (IsActive)
            {
                using (WebClient client = new WebClient())
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(song.LocalPath));

                    if (song.IsHD)
                    {
                        string extension = null;

                        try
                        {
                            WebRequest request = WebRequest.Create(song.EffectiveDownloadUrl + string.Format("?client_id={0}", apiKey));

                            request.Method = "HEAD";
                            using (WebResponse response = request.GetResponse())
                            {
                                extension = Path.GetExtension(response.Headers["Content-Disposition"]
                                                              .Replace("attachment;filename=", "").Replace("\"", ""));
                            }
                        }
                        catch (Exception)
                        {
                            // the download link might be invalid
                            WebRequest request = WebRequest.Create(song.stream_url + string.Format("?client_id={0}", apiKey));

                            request.Method = "HEAD";
                            using (WebResponse response = request.GetResponse())
                            {
                                extension = Path.GetExtension(response.Headers["Content-Disposition"]
                                                              .Replace("attachment;filename=", "").Replace("\"", ""));
                            }
                        }

                        song.LocalPath += extension;
                    }
                    else
                    {
                        song.LocalPath += ".mp3";
                    }

                    client.DownloadFile(song.EffectiveDownloadUrl + string.Format("?client_id={0}", apiKey), song.LocalPath);


                    // metadata tagging
                    TagLib.File tagFile = TagLib.File.Create(song.LocalPath);
                    tagFile.Tag.Title = song.Title;
                    string artworkFilepath = null;

                    if (!String.IsNullOrEmpty(song.Username))
                    {
                        tagFile.Tag.AlbumArtists = new string[] { song.Username };
                        tagFile.Tag.Performers   = new string[] { song.Username };
                    }
                    if (!String.IsNullOrEmpty(song.genre))
                    {
                        tagFile.Tag.Genres = new string[] { song.genre };
                    }
                    if (!String.IsNullOrEmpty(song.description))
                    {
                        tagFile.Tag.Comment = song.description;
                    }
                    if (!String.IsNullOrEmpty(song.artwork_url))
                    {
                        // download artwork
                        artworkFilepath = Path.GetTempFileName();
                        using (WebClient web = new WebClient())
                        {
                            web.DownloadFile(song.artwork_url, artworkFilepath);
                        }

                        tagFile.Tag.Pictures = new[] { new TagLib.Picture(artworkFilepath) };
                    }

                    tagFile.Save();

                    if (artworkFilepath != null && File.Exists(artworkFilepath))
                    {
                        File.Delete(artworkFilepath);
                    }

                    lock (SongsDownloadedLock)
                    {
                        SongsDownloaded.Add(song);
                        downloaded = true;
                    }
                }
            }

            return(downloaded);
        }