private void ProcessAlbum(VKSaverTrack track, string albumName)
        {
            string dbKey = $"{track.Artist}-{albumName}";

            VKSaverAlbum album = null;

            if (!_albums.TryGetValue(dbKey, out album))
            {
                album = new VKSaverAlbum
                {
                    DbKey  = dbKey,
                    Name   = albumName,
                    Tracks = new List <VKSaverTrack>()
                };
                _albums[dbKey] = album;
            }

            VKSaverArtist artist = _artists[track.Artist];

            if (artist.Albums == null)
            {
                artist.Albums = new List <VKSaverAlbum>();
            }

            if (artist.Albums.FirstOrDefault(a => a.DbKey == dbKey) == null)
            {
                artist.Albums.Add(album);
            }

            album.Tracks.Add(track);
        }
        private async Task <VKSaverTrack> ProcessMp3File(StorageFile file)
        {
            var properties = await file.Properties.GetMusicPropertiesAsync();

            var track = new VKSaverTrack();

            if (String.IsNullOrWhiteSpace(properties.Title))
            {
                track.Title = file.DisplayName.Trim();
            }
            else
            {
                track.Title = properties.Title.Trim();
            }

            if (!String.IsNullOrWhiteSpace(properties.Artist))
            {
                ProcessArtist(track, properties.Artist.Trim());
            }
            else
            {
                ProcessArtist(track, UNKNOWN_ARTIST_NAME);
            }

            if (!String.IsNullOrWhiteSpace(properties.Album))
            {
                ProcessAlbum(track, properties.Album.Trim());
            }
            else
            {
                ProcessAlbum(track, UNKNOWN_ALBUM_NAME);
            }

            if (properties.Genre.Count > 0)
            {
                foreach (string genre in properties.Genre)
                {
                    ProcessGenre(track, genre.Trim());
                }
            }
            else
            {
                ProcessGenre(track, UNKNOWN_GENRE_NAME);
            }

            track.Duration = properties.Duration;
            return(track);
        }
Example #3
0
        private async Task <List <object> > RemoveTrack(VKSaverTrack track)
        {
            var result = new List <object>();

            var genre = await _database.GetItemWithChildrens <VKSaverGenre>(track.GenreKey);

            if (genre.Tracks.Count == 1)
            {
                result.Add(genre);
                await _database.RemoveItem(genre);
            }

            var folder = await _database.GetItemWithChildrens <VKSaverFolder>(track.FolderKey);

            if (folder.Tracks.Count == 1)
            {
                result.Add(folder);
                await _database.RemoveItem(folder);
            }

            var album = await _database.GetItemWithChildrens <VKSaverAlbum>(track.AlbumKey);

            if (album.Tracks.Count == 1)
            {
                var artist = await _database.GetItemWithChildrens <VKSaverArtist>(album.ArtistName);

                if (artist.Albums.Count == 1)
                {
                    result.Add(artist);
                    await _database.RemoveItem(artist);
                }

                result.Add(album);
                await _database.RemoveItem(album);
            }

            if (track.VKInfoKey != null)
            {
                await _database.RemoveItem <VKSaverAudioVKInfo>(track.VKInfoKey);
            }

            result.Add(track);
            await _database.RemoveItem(track);

            _libraryDatabaseService.NeedReloadLibraryView = true;
            return(result);
        }
        private void ProcessGenre(VKSaverTrack track, string genreName)
        {
            VKSaverGenre genre = null;

            if (!_genres.TryGetValue(genreName, out genre))
            {
                genre = new VKSaverGenre
                {
                    DbKey  = genreName,
                    Name   = genreName,
                    Tracks = new List <VKSaverTrack>()
                };
                _genres[genreName] = genre;
            }

            track.GenreKey = genreName;
            genre.Tracks.Add(track);
        }
        private void ProcessArtist(VKSaverTrack track, string artistName)
        {
            VKSaverArtist artist = null;

            if (!_artists.TryGetValue(artistName, out artist))
            {
                artist = new VKSaverArtist
                {
                    DbKey  = artistName,
                    Name   = artistName,
                    Tracks = new List <VKSaverTrack>()
                };
                _artists[artistName] = artist;
            }

            track.Artist = artistName;
            artist.Tracks.Add(track);
        }
        private async Task <VKSaverTrack> ProcessVKSaverAudioFile(StorageFile file)
        {
            using (var audioFile = new VKSaverAudioFile(file))
            {
                var metadata = await audioFile.GetMetadataAsync();

                var track = new VKSaverTrack
                {
                    Title = metadata.Track.Title.Trim()
                };

                ProcessArtist(track, metadata.Track.Artist.Trim());
                ProcessAlbum(track, UNKNOWN_ALBUM_NAME);
                ProcessGenre(track, metadata.VK.Genre == (AudioGenres)0 ? UNKNOWN_GENRE_NAME : metadata.VK.Genre.ToString());
                ProcessVKInfo(track, metadata.VK);

                track.Duration = TimeSpan.FromTicks(metadata.Track.Duration);
                return(track);
            }
        }
        private async Task <VKSaverTrack> GetTrackInfo(StorageFile file)
        {
            VKSaverTrack track = null;

            try
            {
                if (file.FileType == ".mp3")
                {
                    track = await ProcessMp3File(file);
                }
                else if (file.FileType == MusicCacheService.FILES_EXTENSION) // Кэшированный файл ВКачай.
                {
                    track = await ProcessVKSaverAudioFile(file);
                }
            }
            catch (Exception) { }

            if (track != null)
            {
                track.Source = MusicFilesPathHelper.GetCapatibleSource(file.Path);
            }

            return(track);
        }
 private void ProcessVKInfo(VKSaverTrack track, VKSaverAudioVKInfo info)
 {
     info.DbKey      = $"{info.OwnerID} {info.ID}";
     track.VKInfoKey = info.DbKey;
     _vksmInfo.Add(info);
 }
        public async Task InsertDownloadedTrack(VKSaverAudio audio, string folderPath, string filePath)
        {
            string artistName          = null;
            string capatibleFolderPath = MusicFilesPathHelper.GetCapatibleSource(folderPath);
            string genreKey            = audio.VK.Genre == 0 ? UNKNOWN_GENRE_NAME : audio.VK.Genre.ToString();
            string vkInfoKey           = $"{audio.VK.OwnerID} {audio.VK.ID}";

            if (String.IsNullOrEmpty(audio.Track.Artist))
            {
                artistName = UNKNOWN_ARTIST_NAME;
            }
            else
            {
                artistName = audio.Track.Artist;
            }

            string albumKey = $"{artistName}-{UNKNOWN_ALBUM_NAME}";

            var dbFolder = await _database.FindItem <VKSaverFolder>(capatibleFolderPath);

            if (dbFolder == null)
            {
                dbFolder = new VKSaverFolder
                {
                    Path   = capatibleFolderPath,
                    Name   = Path.GetFileName(folderPath),
                    Tracks = new List <VKSaverTrack>()
                };
                await _database.InsertItem(dbFolder);
            }
            else
            {
                dbFolder = null;
            }

            var dbAlbum = await _database.FindItem <VKSaverAlbum>(albumKey);

            if (dbAlbum == null)
            {
                dbAlbum = new VKSaverAlbum
                {
                    DbKey      = albumKey,
                    Name       = UNKNOWN_ALBUM_NAME,
                    ArtistName = artistName,
                    Tracks     = new List <VKSaverTrack>()
                };
                await _database.InsertItem(dbAlbum);
            }
            else
            {
                dbAlbum = null;
            }

            var dbArtist = await _database.FindItem <VKSaverArtist>(artistName);

            if (dbArtist == null)
            {
                dbArtist = new VKSaverArtist
                {
                    DbKey  = artistName,
                    Name   = artistName,
                    Tracks = new List <VKSaverTrack>(),
                    Albums = new List <VKSaverAlbum>()
                };
                await _database.InsertItem(dbArtist);
            }
            else if (dbAlbum != null)
            {
                dbArtist = await _database.GetItemWithChildrens <VKSaverArtist>(artistName);
            }
            else
            {
                dbArtist = null;
            }

            var dbGenre = await _database.FindItem <VKSaverGenre>(genreKey);

            if (dbGenre == null)
            {
                dbGenre = new VKSaverGenre
                {
                    DbKey  = genreKey,
                    Name   = genreKey,
                    Tracks = new List <VKSaverTrack>()
                };
                await _database.InsertItem(dbGenre);
            }
            else
            {
                dbGenre = null;
            }

            var dbVkInfo = await _database.FindItem <VKSaverAudioVKInfo>(vkInfoKey);

            if (dbVkInfo == null)
            {
                dbVkInfo       = audio.VK;
                dbVkInfo.DbKey = vkInfoKey;
                await _database.InsertItem(dbVkInfo);
            }
            else
            {
                dbVkInfo = null;
            }

            var track = new VKSaverTrack
            {
                Title     = audio.Track.Title.Trim(),
                Artist    = artistName,
                Duration  = TimeSpan.FromTicks(audio.Track.Duration),
                Source    = MusicFilesPathHelper.GetCapatibleSource(filePath),
                GenreKey  = genreKey,
                FolderKey = capatibleFolderPath,
                AlbumKey  = albumKey,
                VKInfoKey = vkInfoKey
            };

            await _database.InsertItem(track);

            if (dbAlbum != null)
            {
                dbAlbum.Tracks.Add(track);
                await _database.UpdateItemChildrens(dbAlbum);
            }

            if (dbArtist != null)
            {
                if (dbAlbum != null)
                {
                    dbArtist.Albums.Add(dbAlbum);
                }

                dbArtist.Tracks.Add(track);
                await _database.UpdateItemChildrens(dbArtist);
            }

            if (dbFolder != null)
            {
                dbFolder.Tracks.Add(track);
                await _database.UpdateItemChildrens(dbFolder);
            }

            if (dbGenre != null)
            {
                dbGenre.Tracks.Add(track);
                await _database.UpdateItemChildrens(dbGenre);
            }

            if (dbVkInfo != null)
            {
                await _database.UpdateItemChildrens(dbVkInfo);
            }

            NeedReloadLibraryView = true;
        }