Exemple #1
0
        public static void Scan(PlayerSettings settings, bool forceUpdateIfAlreadyExists)
        {
            using (DAL.DALManager mgr = new DAL.DALManager(settings.DatabasePath))
            {
                foreach (var relPath in GetAllFiles(settings.RootDirectory, "").Reverse())
                {
                    string absolutePath = System.IO.Path.Combine(settings.RootDirectory, relPath);

                    var  track = mgr.GetTrackByFilename(relPath);
                    bool isNew = false;
                    if (track == null)
                    {
                        isNew = true;
                        track = new Objects.Track()
                        {
                            Filename      = relPath,
                            AddedOn       = DateTime.Now,
                            LikeStatus    = LikeStatus.None,
                            NrPlayed      = 0,
                            NrPlayedToEnd = 0,
                            LastPlayed    = DateTime.MinValue
                        };
                    }


                    if (isNew || forceUpdateIfAlreadyExists)
                    {
                        UpdateTrackFromMP3Tag(absolutePath, track);


                        var albumName = track.Album + "";
                        if (string.IsNullOrEmpty(System.IO.Path.GetDirectoryName(relPath)))
                        {
                            // mp3s in the root, don't use album for these
                            albumName = "";
                        }
                        else
                        {
                            if (albumName == "")
                            {
                                albumName = new System.IO.DirectoryInfo(relPath).Parent.Name;
                            }
                        }

                        var album = mgr.GetAlbumByName(albumName);
                        if (album == null)
                        {
                            album = new Album()
                            {
                                Name = albumName,
                                // do not save art for empty album names
                                SmallCoverId = albumName == "" ? 0 : SaveSmallCoverForAlbum(settings.CoverDatabasePath, absolutePath)
                            };
                            mgr.Set <Album>(album);
                        }
                        track.AlbumId = album.Id;

                        Console.WriteLine($"Adding {relPath}");
                        mgr.Set <Track>(track);
                    }
                }

                // determine the artists of each album and update it
                var albums = mgr.GetAll <Album>();
                foreach (var album in albums)
                {
                    if (!string.IsNullOrEmpty(album.Name))
                    {
                        var tracksOfAlbum = mgr.FindTracks("", Domain.Objects.Playlist.ALL_ID, album.Id, 0, int.MaxValue);

                        if (tracksOfAlbum.Count > 0)
                        {
                            var occurringArtists  = tracksOfAlbum.GroupBy(t => t.Artists + "").ToDictionary(g => g.Key, g => g.Count());
                            var top3MostOccurring = occurringArtists.OrderByDescending(p => p.Value).Take(3).ToList();

                            // take the most occurring artist
                            if (top3MostOccurring[0].Key != album.Artists)
                            {
                                album.Artists = top3MostOccurring[0].Key;
                                mgr.Set(album);
                            }
                        }
                    }
                }
            }
        }