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 }); } } }
private void UpdateCurrentTrackPlayed(string currentTrackId, bool playedToEnd) { // todo update current track details before moving on using (DAL.DALManager mgr = new DAL.DALManager(settings.Value.DatabasePath)) { var track = mgr.Get <Domain.Objects.Track>(int.Parse(currentTrackId)); if (track == null) {// silently skip Console.Error.WriteLine("Unable to update current track nr played because id '" + currentTrackId + "' was not found"); return; } if (track.Id == Player.Instance.CurrentPlaylist.CurrentTrack.Id && Player.Instance.CurrentPlaylist.CurrentTrackStartedPlaying) { bool wasPlayedToEnd = playedToEnd; if (playedToEnd) { // but is it really?, it's possible that it was just seeking to the end var elapsedDuration = (DateTime.UtcNow - Player.Instance.CurrentPlaylist.CurrentTrackStartedPlayingOn).TotalSeconds; if (track.Duration != 0 && elapsedDuration / track.Duration < 0.5) { // less than 50% of the track duration was passed, assume it was seeked to the end and don't update nr of played to the end wasPlayedToEnd = false; } } // only if the track actually started playing track.NrPlayed++; if (wasPlayedToEnd) { track.NrPlayedToEnd++; } // save a scrobble Domain.Objects.Scrobble scrobble = new Domain.Objects.Scrobble() { TrackId = track.Id, On = DateTime.UtcNow, PlayedToEnd = wasPlayedToEnd }; mgr.Set(scrobble); // and notify last fm if played to end if (wasPlayedToEnd) { lastFmManager.ScrobbleToLastFM(Player.Instance.CurrentPlaylist.CurrentTrackStartedPlayingOn, track); } mgr.Set(track); } } }
private static int SaveSmallCoverForAlbum(string coverDbPath, string path) { byte[] data = null; string mimeType; using (var tlfile = TagLib.File.Create(path)) { TagLib.Tag tag123 = tlfile.Tag; if (tag123.Pictures != null && tag123.Pictures.Length > 0) { data = tag123.Pictures[0].Data.ToArray(); mimeType = tag123.Pictures[0].MimeType; } else { // check if cover file exists string dir = System.IO.Path.GetDirectoryName(path); string coverpath = System.IO.Path.Combine(dir, "cover.jpg"); string mimetype = "image/jpg"; if (!System.IO.File.Exists(coverpath)) { coverpath = System.IO.Path.Combine(dir, "cover.png"); mimetype = "image/png"; } if (System.IO.File.Exists(coverpath)) { data = System.IO.File.ReadAllBytes(coverpath); mimeType = mimetype; } } } Cover cover; try { if (data != null && data.Length > 0) { var resizedImageBytes = ImageResizer.ResizeImageTo(data, 200, 200, false); cover = new Cover() { Data = resizedImageBytes }; using (DAL.DALManager mgr = new DAL.DALManager(coverDbPath)) mgr.Set(cover); return(cover.Id); } } catch (Exception ex) { Console.Error.WriteLine("Unable to save cover for " + path + ": " + ex.GetType().FullName + " - " + ex.Message); } return(0); }
public SetLikeStatusRequestResult SetLikeStatus([FromBody] SetLikeStatusRequest request) { try { using (DAL.DALManager mgr = new DAL.DALManager(settings.Value.DatabasePath)) { var track = mgr.Get <Domain.Objects.Track>(int.Parse(request.TrackId)); if (track == null) { return new SetLikeStatusRequestResult() { Success = false, Message = "Track not found " } } ; var oldLikeStatus = track.LikeStatus; var newLikeStatus = request.LikeStatus; track.LikeStatus = request.LikeStatus; mgr.Set(track); // update last fm try { if (oldLikeStatus != LikeStatus.Liked && newLikeStatus == LikeStatus.Liked) { lastFmManager.LoveTrackOnLastFM(Player.Instance.CurrentPlaylist.CurrentTrackStartedPlayingOn, track, true); } else if (oldLikeStatus == LikeStatus.Liked && newLikeStatus != LikeStatus.Liked) { lastFmManager.LoveTrackOnLastFM(Player.Instance.CurrentPlaylist.CurrentTrackStartedPlayingOn, track, false); } } catch (Exception ex) { Console.Error.WriteLine("Unable to update love status on Last FM: " + ex.Message); } } return(GetPlayerStateResultFromPlayer <SetLikeStatusRequestResult>()); } catch (Exception ex) { return(GetErrorResultFromException <SetLikeStatusRequestResult>(ex)); } }
public Result AddPlaylist([FromBody] AddPlaylistRequest request) { try { if (request.Playlist.Id == (Domain.Objects.Playlist.ALL_ID + "") || request.Playlist.Id == (Domain.Objects.Playlist.LIKED_ID + "")) { return new Result() { Success = false, Message = "Can't change the [All] or [Liked] playlist" } } ; using (DAL.DALManager mgr = new DAL.DALManager(settings.Value.DatabasePath)) { var playlist = request.Playlist.Id == "" ? null : mgr.Get <Domain.Objects.Playlist>(int.Parse(request.Playlist.Id)); if (playlist == null) { playlist = new Domain.Objects.Playlist(); } playlist.Name = request.Playlist.Name; mgr.Set(playlist); return(new Result() { Success = true }); } } catch (Exception ex) { return(GetErrorResultFromException <Result>(ex)); } }
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); } } } } } }
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)); } }