Exemplo n.º 1
0
 internal void ParsePlayListSong(SongModel song, HtmlNode tr)
 {
     var name = tr.SelectSingleNode("./td[@class='song_name']");
     foreach (var item in name.ChildNodes)
     {
         if(item.Name == "a")
         {
             if (item.Element("b") != null)
             {
                 var mvlink = item.GetAttributeValue("href", "/0");
                 song.MV = MVModel.GetNew(mvlink.Substring(mvlink.LastIndexOf('/') + 1));
             }
             else if (item.GetAttributeValue("class", "") == "show_zhcn")
                 song.Description = item.InnerText;
             else
                 song.Name = item.InnerText;
         }
         else 
         {
             string t = item.InnerText.Trim();
             if (t.Length > 0)
                 song.TrackArtist = t;
         }
     }
 }
Exemplo n.º 2
0
        public IHttpActionResult Update(int id, SongModel song)
        {
            if (!this.ModelState.IsValid)
            {
                return BadRequest(this.ModelState);
            }

            var songToUpdate = this.data.Songs
                                    .All()
                                    .FirstOrDefault(s => s.SongId == id);

            if (songToUpdate == null)
            {
                return BadRequest("The song with id: " + id + " does not exists.");
            }

            songToUpdate.Title = song.Title;
            songToUpdate.Year = song.Year;
            songToUpdate.Producer = song.Producer;
            songToUpdate.ArtistId = song.ArtistId;
            this.data.SaveChanges();

            song.SongId = songToUpdate.SongId;
            return Ok(song);
        }
Exemplo n.º 3
0
        public IHttpActionResult Create(SongModel song)
        {
            if (!this.ModelState.IsValid)
            {
                return BadRequest(this.ModelState);
            }

            if (this.data.Artists.All().FirstOrDefault(a => a.ArtistId == song.ArtistId) == null)
            {
                return BadRequest("The song can not be added to this artist, because the artist with id: " + song.SongId + " does not exists.");
            }

            var newSong = new Song()
            {
                Title = song.Title,
                Year = song.Year,
                Producer = song.Producer,
                ArtistId = song.ArtistId
            };

            this.data.Songs.Add(newSong);
            this.data.SaveChanges();

            song.SongId = newSong.SongId;
            return Ok(song);
        }
Exemplo n.º 4
0
        public IHttpActionResult Add(SongModel songModel)
        {
            if (!this.ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var newAtist = SongModel.ToSong(songModel);

            this.data.Songs.Add(newAtist);
            this.data.SaveChanges();

            songModel.ID = newAtist.ID;
            return Ok(songModel);
        }
Exemplo n.º 5
0
        public static SongModel Convert(Song song)
        {
            SongModel model = new SongModel
            {
                SongId = song.SongId,
                SongTitle = song.SongTitle,
                SongYear = song.SongYear,
                SongGenre = song.SongGenre,
                Description = song.Description,
                Artist = new ArtistModel
                {
                    ArtistId = song.SongArtist.ArtistId,
                    Name = song.SongArtist.Name,
                    DateOfBirth = song.SongArtist.DateOfBirth,
                    Country = song.SongArtist.Country
                }
            };

            return model;
        }
Exemplo n.º 6
0
 public void Add(SongModel model)
 {
     _session.Add(model);
 }
Exemplo n.º 7
0
        public SongModel LookupSongById(int songId)
        {
            if (songLookupDictionary.ContainsKey(songId))
            {
                return songLookupDictionary[songId];
            }
            else
            {
                SongTable songTable = DatabaseManager.Current.LookupSongById(songId);

                if (songTable == null)
                {
                    return null;
                }
                else
                {
                    SongModel songModel = new SongModel(songTable);
                    _allSongs.Add(songModel);
                    songLookupDictionary.Add(songModel.SongId, songModel);

                    return songModel;
                }
            }
        }
Exemplo n.º 8
0
        public SongModel AddNewSong(string artist, string album, string albumArtist, string title, string path, SongOriginSource origin, long duration, uint rating, uint trackNumber)
        {
            ArtistModel artistModel = LookupArtistByName(artist);

            ArtistModel albumArtistModel = LookupArtistByName(albumArtist);

            AlbumModel albumModel = LookupAlbumByName(album, albumArtistModel.ArtistId);

            SongModel currentTableEntry = LookupSongByPath(path);

            if (currentTableEntry == null)
            {
                SongTable newSong = new SongTable(albumModel.AlbumId, artistModel.ArtistId, duration, 0, title, origin, 0, rating, path, trackNumber);
                DatabaseManager.Current.AddSong(newSong);

                SongModel songModel = new SongModel(newSong);
                _allSongs.Add(songModel);
                songLookupDictionary.Add(songModel.SongId, songModel);

                return songModel;
            }

            return null;
        }
Exemplo n.º 9
0
        private SongModel LookupSongByPath(string path)
        {
            SongTable songTable = DatabaseManager.Current.LookupSongByPath(path);

            if (songTable == null) return null;

            if (songLookupDictionary.ContainsKey(songTable.SongId)) return songLookupDictionary[songTable.SongId];

            SongModel songModel = new SongModel(songTable);
            _allSongs.Add(songModel);
            songLookupDictionary.Add(songModel.SongId, songModel);

            return songModel;
        }
Exemplo n.º 10
0
        private void LoadCollection()
        {
            PerfTracer perfTracer = new PerfTracer("LibraryModel Loading");

            IEnumerable<SongTable> allSongs = DatabaseManager.Current.FetchSongs();
            foreach (SongTable songEntry in allSongs)
            {
                SongModel songModel = new SongModel(songEntry);
                _allSongs.Add(songModel);
                songLookupDictionary.Add(songModel.SongId, songModel);                
            }

            perfTracer.Trace("Songs Added");

            IEnumerable<AlbumTable> allAlbums = DatabaseManager.Current.FetchAlbums();
            foreach (AlbumTable albumEntry in allAlbums)
            {
                AlbumModel albumModel = new AlbumModel(albumEntry);
                _allAlbums.Add(albumModel);
                albumLookupDictionary.Add(albumModel.AlbumId, albumModel);
            }

            perfTracer.Trace("Albums Added");

            IEnumerable<ArtistTable> allArtists = DatabaseManager.Current.FetchArtists();
            foreach (ArtistTable artistEntry in allArtists)
            {
                ArtistModel artistModel = new ArtistModel(artistEntry);
                _allArtists.Add(artistModel);
                artistLookupDictionary.Add(artistModel.ArtistId, artistModel);
            }

            perfTracer.Trace("Artists Added");

            IEnumerable<PlaylistTable> allPlaylists = DatabaseManager.Current.FetchPlaylists();
            foreach (PlaylistTable playlistEntry in allPlaylists)
            {
                PlaylistModel playlistModel = new PlaylistModel(playlistEntry);
                Playlists.Add(playlistModel);
                playlistLookupDictionary.Add(playlistModel.PlaylistId, playlistModel);

                playlistModel.Populate();
            }

            perfTracer.Trace("Playlists Added");

            IEnumerable<MixTable> allMixes = DatabaseManager.Current.FetchMixes();
            foreach (MixTable mixEntry in allMixes)
            {
                MixModel mixModel = new MixModel(mixEntry);
                Mixes.Add(mixModel);
                mixLookupDictionary.Add(mixModel.MixId, mixModel);

                mixModel.Populate();
            }

            perfTracer.Trace("Mixes Added");
        }
Exemplo n.º 11
0
        private SongViewModel LookupSong(SongModel song)
        {
            if (song == null) return null;

            if (SongLookupMap.ContainsKey(song.SongId))
            {
                return SongLookupMap[song.SongId];
            }
            else
            {
                ArtistViewModel artist = LookupArtistById(song.ArtistId);

                AlbumViewModel album = LookupAlbumById(song.AlbumId);

                SongViewModel newSongViewModel = new SongViewModel(song, artist, album);

                SongLookupMap.Add(newSongViewModel.SongId, newSongViewModel);
                SongCollection.Add(newSongViewModel, newSongViewModel.SortName);
                FlatSongCollection.Add(newSongViewModel);

                if (LibraryLoaded)
                {
                    NotifyPropertyChanged(Properties.IsEmpty);
                }

                return newSongViewModel;
            }
        }
Exemplo n.º 12
0
        /// <returns>是否成功开始播放</returns>
        private bool PlayTrackInternal(SongModel song)
        {
            PlaybackOperated?.Invoke(song, null);
            // Start the background task if it wasn't running
            //if (!IsBackgroundTaskRunning || MediaPlayerState.Closed == CurrentPlayer.CurrentState)
            if (!IsBackgroundTaskRunning)
            {
                // First update the persisted start track
                SettingsService.Playback.Write("TrackId", song.MediaUri.ToString());
                SettingsService.Playback.Write("Position", new TimeSpan().ToString());

                // Start task
                StartBackgroundAudioTask();
                return false;
            }
            else
            {
                //TODO: 增加如果播放不成功则刷新地址
                if (song.MediaUri == null)
                    ExtensionMethods.InvokeAndWait(async () => song.MediaUri = new Uri(await Net.DataApi.GetDownloadLink(song, false)));
                MessageService.SendMediaMessageToBackground(MediaMessageTypes.SetSong, song);
                MessageService.SendMediaMessageToBackground(MediaMessageTypes.StartPlayback);
                return true;
                
            }
        }
Exemplo n.º 13
0
 /// <summary>
 /// 播放指定歌曲
 /// </summary>
 /// <param name="song">需要播放的歌曲,如果不在列表中的话将加入列表</param>
 public async void PlayTrack(SongModel song)
 {
     if (_isPlayingRadio)
         throw new InvalidOperationException("在播放歌曲前应停止电台播放");
     if (string.IsNullOrEmpty(song.Album?.Art?.Host))
         await Net.WebApi.Instance.GetSongInfo(song);
     if (!PlaylistService.Instance.Contains(song))
     {
         PlaylistService.Instance.Add(song);
         PlayTrack(PlaylistService.Instance.Count - 1);
     }
     else
         PlayTrack(PlaylistService.Instance.IndexOf(song));
 }
Exemplo n.º 14
0
        public IHttpActionResult Update(int id, SongModel songModel)
        {
            if (!this.ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var existingSong = this.data.Songs.Get(id);
            if (existingSong == null)
            {
                return BadRequest(BabRequestMessage);
            }

            SongModel.ToSong(songModel, existingSong);

            this.data.Songs.Update(existingSong);
            this.data.SaveChanges();

            return Ok(songModel);
        }
Exemplo n.º 15
0
        public IAsyncAction GetSongInfo(SongModel song, bool cover = true)
        {
            if (song.XiamiID == 0)
                throw new ArgumentException("SongModel未设置ID");
            return Run(async token =>
            {
                try
                {
                    LogService.DebugWrite($"Get info of Song {song.XiamiID}", nameof(WebApi));

                    var gettask = HttpHelper.GetAsync($"http://www.xiami.com/song/{song.XiamiID}");
                    token.Register(() => gettask.Cancel());
                    var content = await gettask;
                    HtmlDocument doc = new HtmlDocument();
                    doc.LoadHtml(content);
                    var body = doc.DocumentNode.SelectSingleNode("/html/body/div[@id='page']");
                    List<Task> process = new List<Task>();
                    process.Add(Task.Run(() =>
                    {
                        if (song.RelatedLovers == null || cover)
                            song.RelatedLovers = new PageItemsCollection<UserModel>(ParseSongRelateUsers(body.SelectSingleNode(".//div[@id='song_fans_block']/div/ul")).ToList());
                    }, token));
                    process.Add(Task.Run(() =>
                    {
                        if (song.RelateHotSongs == null || cover)
                            song.RelateHotSongs = ParseSongRelateSongs(body.SelectSingleNode(".//div[@id='relate_song']/div/table")).ToList();
                    }, token));
                    process.Add(Task.Run(() =>
                    {
                        if (song.Tags == null || cover)
                            song.Tags = ParseTags(body.SelectSingleNode(".//div[@id='song_tags_block']/div")).ToList();
                    }, token));

                    var title = body.SelectSingleNode(".//h1");
                    if (song.Name == null || cover)
                        song.Name = title.FirstChild.InnerText;
                    var mva = title.SelectSingleNode("./a");
                    if ((mva != null) && (song.MV == null || cover))
                        song.MV = MVModel.GetNew(ParseXiamiIDString(mva.GetAttributeValue("href", "/0")));
                    if (song.Description == null || cover)
                        if (title.LastChild.Name == "span")
                            song.Description = title.LastChild.InnerText;
                    var loveop = body.SelectSingleNode(".//ul/li[1]");
                    song.IsLoved = loveop.GetAttributeValue("style", "") == "display:none";
                    if (loveop.ParentNode.ParentNode.ParentNode.InnerText.IndexOf("单曲下架") != -1) song.Available = false;
                    var detail = body.SelectSingleNode(".//table");
                    foreach (var item in detail.SelectNodes("./tr"))
                    {
                        switch (item.ChildNodes[1].InnerText)
                        {
                            case "所属专辑:":
                                var linknode = item.SelectSingleNode(".//a");
                                var id = uint.Parse(linknode.GetAttributeValue("href", "/album/0").Substring(7));
                                if ((song.Album == null) || cover)
                                {
                                    var album = song.Album ?? AlbumModel.GetNew(id);
                                    album.Name = linknode.InnerText;
                                    if (album.Art.Host == "")
                                    {
                                        var art = detail.ParentNode.SelectSingleNode(".//img").GetAttributeValue("src", AlbumModel.SmallDefaultUri);
                                        album.Art = new Uri(art.Replace("_2", "_1"));
                                        album.ArtFull = new Uri(art.Replace("_2", ""));
                                    }
                                    song.Album = album;
                                }
                                break;
                            case "演唱者:":
                                song.TrackArtist = item.SelectSingleNode(".//a").InnerText;
                                break;
                            case "作词:":
                                song.Lyricist = item.SelectSingleNode(".//div").InnerText;
                                break;
                            case "作曲:":
                                song.Composer = item.SelectSingleNode(".//div").InnerText;
                                break;
                            case "编曲:":
                                song.Arranger = item.SelectSingleNode(".//div").InnerText;
                                break;
                        }
                    }


                    await Task.WhenAll(process);
                    LogService.DebugWrite($"Finish Getting info of Song {song.XiamiID}", nameof(WebApi));
                }
                catch (Exception e)
                {
                    LogService.ErrorWrite(e, nameof(WebApi));
                    throw e;
                }
            });
        }
Exemplo n.º 16
0
 public void Delete(SongModel model)
 {
     _session.Delete(model);
 }
Exemplo n.º 17
0
 public void Edit(SongModel model)
 {
     var record = _session.Single<Song>(x=>x.Id == model.Id);
     record.Update(model);
     _session.CommitChanges();
 }
Exemplo n.º 18
0
        //TODO: 判断歌曲是否被喜爱
        /// <summary>
        /// 通过SongId获取歌曲的信息(不含取媒体地址)
        /// </summary>
        /// <param name="cover">是否覆盖已存在的Album和Artist信息</param> 
        public IAsyncAction GetSongInfo(SongModel song, bool cover = false)
        {
            if (song.XiamiID == 0)
                throw new ArgumentException("SongModel未设置ID");
            return Run(async token =>
            {
                try
                {
                    LogService.DebugWrite($"Get info of Song {song.XiamiID}", nameof(WapApi));

                    var gettask = HttpHelper.GetAsync($"http://www.xiami.com/app/xiating/song?id={song.XiamiID}");
                    token.Register(() => gettask.Cancel());
                    var content = await gettask;
                    HtmlDocument doc = new HtmlDocument();
                    doc.LoadHtml(content);
                    HtmlNode root = doc.DocumentNode;
                    var logo = root.SelectSingleNode("//img[1]");
                    var detail = root.SelectSingleNode("//ul[1]");
                    var detailgrade = root.SelectSingleNode("//div[1]/ul[1]");
                    if (song.Name == null)
                        song.Name = logo.GetAttributeValue("title", "UnKnown");
                    song.PlayCount = int.Parse(detailgrade.SelectSingleNode(".//span[1]").InnerText);
                    song.ShareCount = int.Parse(detailgrade.SelectSingleNode("./li[3]/span[1]").InnerText);

                    var additionnodes = detail.SelectNodes("./li[position()>2]");
                    foreach (var node in additionnodes)
                        switch (node.FirstChild.InnerText)
                        {
                            case "作词:":
                                song.Lyricist = node.LastChild.InnerText;
                                break;
                            case "作曲:":
                                song.Composer = node.LastChild.InnerText;
                                break;
                            case "编曲:":
                                song.Arranger = node.LastChild.InnerText;
                                break;
                        }

                    if ((song.Album == null) || cover)
                    {
                        var albumtag = detail.SelectSingleNode("./li[1]/a[1]");
                        var idtext = albumtag.GetAttributeValue("href", "/app/xiating/album?id=0");
                        var addrlength = "/app/xiating/album?id=".Length;
                        uint albumID = uint.Parse(idtext.Substring(addrlength, idtext.IndexOf("&", addrlength) - addrlength));
                        AlbumModel album = song.Album ?? AlbumModel.GetNew(albumID);
                        if (album.Art.Host == "")
                        {
                            var art = logo.GetAttributeValue("src", AlbumModel.SmallDefaultUri);
                            album.Art = new Uri(art.Replace("_2", "_1"));
                            album.ArtFull = new Uri(art.Replace("_2", ""));
                        }
                        album.Name = albumtag.InnerText;
                        song.Album = album;
                    }

                    if ((song.Album?.Artist == null) || cover)
                    {
                        var artisttag = detail.SelectSingleNode("./li[2]/a[1]");
                        var idtext = artisttag.GetAttributeValue("href", "/app/xiating/artist?id=0");
                        var addrlength = "/app/xiating/artist?id=".Length;
                        uint artistID = uint.Parse(idtext.Substring(addrlength, idtext.IndexOf("&", addrlength) - addrlength));
                        ArtistModel artist = song.Album?.Artist ?? ArtistModel.GetNew(artistID);
                        artist.Name = artisttag.InnerText;
                        song.Album.Artist = artist;
                    }

                    LogService.DebugWrite($"Finish Getting info of Song {song.Name}", nameof(WapApi));
                }
                catch (Exception e)
                {
                    LogService.ErrorWrite(e, nameof(WapApi));
                    throw e;
                }
            });
        }