Example #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);
                            }
                        }
                    }
                }
            }
        }
Example #2
0
        public GetPlaylistsResult GetPlaylists(GetPlaylistsRequest request)
        {
            try
            {
                List <Domain.Objects.Playlist> playlists;
                int        forItemSize;
                List <int> playlistIds;

                using (DAL.DALManager mgr = new DAL.DALManager(settings.Value.DatabasePath))
                {
                    playlists = mgr.GetAll <Domain.Objects.Playlist>();


                    playlists.Insert(0, new Domain.Objects.Playlist()
                    {
                        Id = Domain.Objects.Playlist.LIKED_ID, Name = "[Liked]", NrOfTracks = mgr.GetLikedTrackCount()
                    });

                    if (!request.AsSelector)  // only show [all] when it's not listed for adding a track to the playlist
                    {
                        playlists.Insert(0, new Domain.Objects.Playlist()
                        {
                            Id = Domain.Objects.Playlist.ALL_ID, Name = "[All]", NrOfTracks = mgr.GetTrackCount()
                        });
                    }

                    if (request.ForItemIsTrack)
                    {
                        playlistIds = new List <int>(mgr.GetPlaylistIdsForTrack(request.ForItemId));
                        forItemSize = 1;
                    }
                    else
                    {
                        playlistIds = new List <int>(mgr.GetPlaylistIdsForAlbum(request.ForItemId));
                        forItemSize = mgr.FindTracks("", Domain.Objects.Playlist.ALL_ID, request.ForItemId, 0, int.MaxValue).Count;
                    }
                }

                if (request.AsSelector)
                {
                    return(new GetPlaylistsResult()
                    {
                        Success = true,
                        Playlists = playlists.Select(p =>
                        {
                            var itm = new PlaylistForAddingItem()
                            {
                                Id = p.Id + "",
                                IsCurrent = Player.Instance.CurrentPlaylist.Id == p.Id,
                                Name = p.Name,
                                NrOfTracks = p.NrOfTracks,

                                AlreadyOnPlaylistCount = playlistIds.Count(id => p.Id == id),
                                ForItemSize = forItemSize
                            };

                            return itm;
                        }).ToArray()
                    });
                }
                else
                {
                    return(new GetPlaylistsResult()
                    {
                        Success = true,
                        Playlists = playlists.Select(p => new Playlist()
                        {
                            Id = p.Id + "",
                            IsCurrent = Player.Instance.CurrentPlaylist.Id == p.Id,
                            Name = p.Name,
                            NrOfTracks = p.NrOfTracks
                        }).ToArray()
                    });
                }
            }
            catch (Exception ex)
            {
                return(GetErrorResultFromException <GetPlaylistsResult>(ex));
            }
        }