Exemple #1
0
        private Result RemoveFromLikedPlaylist(RemoveFromPlaylistRequest request)
        {
            using (DAL.DALManager mgr = new DAL.DALManager(settings.Value.DatabasePath))
            {
                if (request.Item.IsTrack)
                {
                    var track = mgr.Get <Domain.Objects.Track>(request.Item.Id);
                    if (track == null)
                    {
                        return new Result()
                               {
                                   Success = false, Message = "Track not found"
                               }
                    }
                    ;
                    track.LikeStatus = LikeStatus.None;
                    mgr.Set(track);
                    lastFmManager.LoveTrackOnLastFM(DateTime.MinValue, track, false);
                    return(new Result()
                    {
                        Success = true
                    });
                }
                else
                {
                    var album = mgr.Get <Domain.Objects.Album>(request.Item.Id);
                    if (album == null)
                    {
                        return new Result()
                               {
                                   Success = false, Message = "Album not found"
                               }
                    }
                    ;

                    var tracks = mgr.FindTracks(null, Domain.Objects.Playlist.LIKED_ID, album.Id, 0, int.MaxValue);
                    foreach (var trackItem in tracks)
                    {
                        var track = mgr.Get <Domain.Objects.Track>(trackItem.Id);

                        if (track == null)
                        {
                            return new Result()
                                   {
                                       Success = false, Message = "Track not found"
                                   }
                        }
                        ;

                        track.LikeStatus = LikeStatus.None;
                        mgr.Set(track);
                        lastFmManager.LoveTrackOnLastFM(DateTime.MinValue, track, true);
                    }
                    return(new Result()
                    {
                        Success = true
                    });
                }
            }
        }
Exemple #2
0
        public GetTracksResult GetTracks(GetTracksRequest request)
        {
            try
            {
                int forPlaylistId;
                if (string.IsNullOrEmpty(request.ForPlaylistId))
                {
                    // no playlist filtering
                    forPlaylistId = Domain.Objects.Playlist.ALL_ID;
                }
                else
                {
                    forPlaylistId = int.Parse(request.ForPlaylistId);
                }

                List <AlbumOrTrackItem> albumOrTracks;
                int totalCount;
                using (DAL.DALManager mgr = new DAL.DALManager(settings.Value.DatabasePath))
                {
                    albumOrTracks = mgr.FindTracks(request.Filter, forPlaylistId, int.Parse(request.AlbumId), request.Offset, request.Size);
                    totalCount    = mgr.FindTracksCount(request.Filter, forPlaylistId, int.Parse(request.AlbumId));
                }

                // set the cover urls
                foreach (var item in albumOrTracks)
                {
                    item.ArtImage = item.CoverId > 0 ? Url.Action("GetCover", new { id = item.CoverId }) : "";
                }

                return(new GetTracksResult()
                {
                    Success = true,
                    Items = albumOrTracks.ToArray(),
                    TotalCount = totalCount
                });
            }
            catch (Exception ex)
            {
                return(GetErrorResultFromException <GetTracksResult>(ex));
            }
        }
        public PlayNowResult PlayAlbumOrTrackNow([FromBody] PlayAlbumOrTrackRequest request)
        {
            // TODO
            int playlistId;

            if (string.IsNullOrEmpty(request.PlaylistId))
            {
                playlistId = Domain.Objects.Playlist.ALL_ID; // all
            }
            else
            {
                playlistId = int.Parse(request.PlaylistId);
            }

            using (DAL.DALManager mgr = new DAL.DALManager(settings.Value.DatabasePath))
            {
                if (playlistId != Player.Instance.CurrentPlaylist.Id)
                {
                    PlayPlaylistNow(new PlayPlaylistRequest()
                    {
                        PlaylistId = playlistId + ""
                    });
                }

                // find in the current playlist and change track

                if (request.Item.IsTrack)
                {
                    var track = mgr.Get <Domain.Objects.Track>(request.Item.Id);

                    if (track == null)
                    {
                        return new PlayNowResult()
                               {
                                   Success = false, Message = "Track not found "
                               }
                    }
                    ;

                    Player.Instance.PlayTracks(new HashSet <int>()
                    {
                        request.Item.Id
                    });
                }
                else
                {
                    var album = mgr.Get <Domain.Objects.Album>(request.Item.Id);
                    if (album == null)
                    {
                        return new PlayNowResult()
                               {
                                   Success = false, Message = "Album not found"
                               }
                    }
                    ;

                    var tracks = mgr.FindTracks(null, playlistId, album.Id, 0, int.MaxValue);

                    Player.Instance.PlayTracks(new HashSet <int>(tracks.Select(t => t.Id)));
                }
            }

            return(GetPlayerStateResultFromPlayer <PlayNowResult>());
        }
Exemple #4
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);
                            }
                        }
                    }
                }
            }
        }
Exemple #5
0
        public Result RemoveFromPlaylist([FromBody] RemoveFromPlaylistRequest request)
        {
            try
            {
                if (request.PlaylistId == (Domain.Objects.Playlist.ALL_ID + ""))
                {
                    return new Result()
                           {
                               Success = false, Message = "Can't remove tracks from the [All] or [Liked] playlist"
                           }
                }
                ;
                else if (request.PlaylistId == (Domain.Objects.Playlist.LIKED_ID + ""))
                {
                    return(RemoveFromLikedPlaylist(request));
                }

                using (DAL.DALManager mgr = new DAL.DALManager(settings.Value.DatabasePath))
                {
                    var playlist = mgr.Get <Domain.Objects.Playlist>(int.Parse(request.PlaylistId));

                    if (playlist == null)
                    {
                        return new Result()
                               {
                                   Success = false, Message = "Playlist not found"
                               }
                    }
                    ;

                    int nrOfItemsRemoved = 0;
                    if (request.Item.IsTrack)
                    {
                        var track = mgr.Get <Domain.Objects.Track>(request.Item.Id);

                        if (track == null)
                        {
                            return new Result()
                                   {
                                       Success = false, Message = "Track not found"
                                   }
                        }
                        ;

                        var playlistTrack = mgr.GetPlaylistTrack(playlist.Id, track.Id);
                        if (playlistTrack == null)
                        {
                            return new Result()
                                   {
                                       Success = false, Message = "Track is was not in the playlist"
                                   }
                        }
                        ;

                        mgr.RemoveFromPlaylist(playlist.Id, track.Id);
                        nrOfItemsRemoved++;
                    }
                    else
                    {
                        var album = mgr.Get <Domain.Objects.Album>(request.Item.Id);
                        if (album == null)
                        {
                            return new Result()
                                   {
                                       Success = false, Message = "Album not found"
                                   }
                        }
                        ;

                        var tracks = mgr.FindTracks(null, Domain.Objects.Playlist.ALL_ID, album.Id, 0, int.MaxValue);
                        foreach (var track in tracks)
                        {
                            var playlistTrack = mgr.GetPlaylistTrack(playlist.Id, track.Id);

                            if (playlistTrack != null)
                            {
                                mgr.RemoveFromPlaylist(playlist.Id, track.Id);
                                nrOfItemsRemoved++;
                            }
                        }
                    }
                    playlist.NrOfTracks -= nrOfItemsRemoved;
                    mgr.Set <Domain.Objects.Playlist>(playlist);

                    return(new Result()
                    {
                        Success = true
                    });
                }
            }
            catch (Exception ex)
            {
                return(GetErrorResultFromException <Result>(ex));
            }
        }
Exemple #6
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));
            }
        }