Ejemplo n.º 1
0
        public void UploadSong(string fileExtension, string fileName, string pathSong, string songId, string absoluteSongCoverPath, string userId)
        {
            if (string.IsNullOrEmpty(fileName) == false)
            {
                fileName = fileName.Replace("\\p{Cntrl}", "");
            }

            var songPathToDb = FilePathContainer.SongVirtualPath + songId + fileExtension;
            var songAlbumPicturePathToDb = FilePathContainer.SongAlbumCoverPathRelative + songId + FilePathContainer.SongAlbumCoverFileFormat;
            var song = new Song
            {
                AddDate = DateTime.Now,
                SongId = songId,
                SongPath = songPathToDb,
                FileName = fileName,
            };
            var audioFile = TagLib.File.Create(pathSong);

            song.Year = audioFile.Tag.Year.ToString();
            song.BitRate = audioFile.Properties.AudioBitrate;
            song.Duration = audioFile.Properties.Duration;
            song.DiscCount = (int)audioFile.Tag.DiscCount;
            song.Composers = audioFile.Tag.Composers.ConvertStringArrayToString().ToUtf8();
            song.Disc = (int)audioFile.Tag.Disc;
            song.Performers = audioFile.Tag.Performers.ConvertStringArrayToString().ToUtf8();

            song = Recognise(pathSong, song);
            bool recogniseFailed = CheckRecogniseFailed(song);
            if (recogniseFailed)
            {
                song.Artist = string.Join(",", audioFile.Tag.AlbumArtists);
                song.Genre = string.Join(",", audioFile.Tag.Genres);
                song.Album = audioFile.Tag.Album.ToUtf8();
                song.Title = audioFile.Tag.Title.ToUtf8();
            }

            if (audioFile.Tag.Pictures.Any())
            {
                song.SongAlbumCoverPath = songAlbumPicturePathToDb;
                SongPictureGetter.GetAndSavePictureByTag(audioFile.Tag, absoluteSongCoverPath, songId);
            }
            else
            {
                var pictureInfo = SongPictureGetter.CheckContent(song.Artist, song.Album, song.Title);
                if (pictureInfo != null)
                {
                    song.SongAlbumCoverPath = pictureInfo.PicturePath;
                }
            }

            var vkaudioService = new VkAudioService();
            song.Lyrics = vkaudioService.GetSongLyrics(song.Title, song.Artist);

            //var saveSongCoverPath = absoluteSongCoverPath;

            //var title = audioFile.Tag.Title.ToUtf8();
            //var artist = audioFile.Tag.AlbumArtists.ConvertStringArrayToString().ToUtf8();

            //if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(artist))
            //{

            //}

            //string lyrics = string.Empty;
            //string titleFromVk = string.Empty;
            //string artistVk = string.Empty;

            //if (songInfoFromVk != null)
            //{
            //    lyrics = songInfoFromVk.Lyrics.ToUtf8();
            //    titleFromVk = songInfoFromVk.Title.ToUtf8();
            //    artistVk = songInfoFromVk.Title.ToUtf8();
            //}

            //songAlbumPicturePathToDb = SongAlbumPicturePathToDb(audioFile, saveSongCoverPath, songId, songAlbumPicturePathToDb, titleFromVk, artistVk, fileName, ref content);
            //GetSongTags(audioFile.Tag, titleFromVk, artistVk);

            //    ModelConverters.ToSongFromTagModel(audioFile, songId, songPathToDb, songAlbumPicturePathToDb);

            _musicRepository.AddSong(song, userId);
        }
Ejemplo n.º 2
0
        //public SongInfo GetSongInfo(string songTitle)
        //{
        //    int totalCount;
        //    var audios = _vkApi.Audio.Search(songTitle, out totalCount, true, findLyrics: true);
        //    var foundAudio = audios.FirstOrDefault();
        //    var info = new SongInfo();
        //    if (foundAudio != null)
        //    {
        //        info.Artist = foundAudio.Artist;
        //        info.Genre = foundAudio.Genre.ToString();
        //        info.Lyrics = GetSongLyrics(foundAudio.LyricsId);
        //        info.SongPath = foundAudio.Url.AbsoluteUri;
        //        info.Title = foundAudio.Title;
        //        GetLyricsFromAllAudios(audios, info);
        //        return info;
        //    }
        //    return null;
        //}
        public List<SongInfo> GetSongs(bool findLyrics)
        {
            List<SongInfo> songs = new List<SongInfo>();
            if (_vkApi == null)
            {
                return songs;
            }

            var userId = _vkApi.UserId;

            var lyricGetter = new VkAudioService(_vkApi);
            if (userId != null)
            {
                var vkSongs = _vkApi.Audio.Get((long)userId);
                //vkSongs = vkSongs.Take(5);
                foreach (var vkSong in vkSongs)
                {
                    var songInfo = new SongInfo
                    {
                        Artist = vkSong.Artist,
                        Genre = vkSong.Genre.ToString(),
                        SongId = Guid.NewGuid().ToString(),
                        SongPath = vkSong.Url.AbsoluteUri,

                        Title = vkSong.Title,
                    };
                    songInfo.Duration = TimeSpan.FromSeconds(vkSong.Duration);
                    if (findLyrics)
                    {
                        songInfo.Lyrics = lyricGetter.GetSongLyrics(vkSong.LyricsId);
                    }

                    songs.Add(songInfo);
                }
            }
            return songs;
        }