From 15b70ede7cd3f9411f39df471b8b29b6a69065ec Mon Sep 17 00:00:00 2001 From: Qstick Date: Mon, 12 Jun 2017 23:03:36 -0400 Subject: [PATCH] Track Mapping Fixes Fix Some Issues with Track Mapping --- .../Datastore/Migration/111_setup_music.cs | 4 ++- .../SkyHook/Resource/TrackResource.cs | 2 +- .../Music/RefreshTrackService.cs | 17 ++++------- src/NzbDrone.Core/Music/Track.cs | 16 +++++------ src/NzbDrone.Core/Music/TrackRepository.cs | 28 +++++++++---------- src/NzbDrone.Core/Music/TrackService.cs | 20 ++++++------- src/NzbDrone.Core/Parser/Model/LocalTrack.cs | 10 +------ 7 files changed, 42 insertions(+), 55 deletions(-) diff --git a/src/NzbDrone.Core/Datastore/Migration/111_setup_music.cs b/src/NzbDrone.Core/Datastore/Migration/111_setup_music.cs index 57decb35d..6b4010417 100644 --- a/src/NzbDrone.Core/Datastore/Migration/111_setup_music.cs +++ b/src/NzbDrone.Core/Datastore/Migration/111_setup_music.cs @@ -40,7 +40,7 @@ namespace NzbDrone.Core.Datastore.Migration .WithColumn("AddOptions").AsString().Nullable(); Create.TableForModel("Albums") - .WithColumn("ForeignArtistId").AsString().Unique() + .WithColumn("ForeignAlbumId").AsString().Unique() .WithColumn("ArtistId").AsInt32() .WithColumn("MBId").AsString().Indexed() .WithColumn("AMId").AsString().Nullable() @@ -68,12 +68,14 @@ namespace NzbDrone.Core.Datastore.Migration .WithColumn("AddOptions").AsString().Nullable(); Create.TableForModel("Tracks") + .WithColumn("ForeignTrackId").AsString().Unique() .WithColumn("ArtistId").AsInt32().Indexed() .WithColumn("AlbumId").AsInt32() .WithColumn("MBId").AsString().Indexed() .WithColumn("TrackNumber").AsInt32() .WithColumn("Title").AsString().Nullable() .WithColumn("Explicit").AsBoolean() + .WithColumn("Compilation").AsBoolean() .WithColumn("DiscNumber").AsInt32().Nullable() .WithColumn("TrackFileId").AsInt32().Nullable().Indexed() .WithColumn("Monitored").AsBoolean() diff --git a/src/NzbDrone.Core/MetadataSource/SkyHook/Resource/TrackResource.cs b/src/NzbDrone.Core/MetadataSource/SkyHook/Resource/TrackResource.cs index c2d4eddd2..eeb86ba48 100644 --- a/src/NzbDrone.Core/MetadataSource/SkyHook/Resource/TrackResource.cs +++ b/src/NzbDrone.Core/MetadataSource/SkyHook/Resource/TrackResource.cs @@ -19,7 +19,7 @@ namespace NzbDrone.Core.MetadataSource.SkyHook.Resource public string TrackName { get; set; } public int TrackNumber { get; set; } public bool Explicit { get; set; } - public List Artists { get; set; } + public List Artists { get; set; } } } diff --git a/src/NzbDrone.Core/Music/RefreshTrackService.cs b/src/NzbDrone.Core/Music/RefreshTrackService.cs index 4459ee503..3bd5925a5 100644 --- a/src/NzbDrone.Core/Music/RefreshTrackService.cs +++ b/src/NzbDrone.Core/Music/RefreshTrackService.cs @@ -33,7 +33,7 @@ namespace NzbDrone.Core.Music var successCount = 0; var failCount = 0; - var existingTracks = _trackService.GetTracksByArtist(artist.ForeignArtistId); + var existingTracks = _trackService.GetTracksByArtist(artist.Id); var albums = artist.Albums; var updateList = new List(); @@ -58,20 +58,13 @@ namespace NzbDrone.Core.Music newList.Add(trackToUpdate); } - trackToUpdate.SpotifyTrackId = track.SpotifyTrackId; + trackToUpdate.ForeignTrackId = track.ForeignTrackId; trackToUpdate.TrackNumber = track.TrackNumber; trackToUpdate.Title = track.Title ?? "Unknown"; trackToUpdate.AlbumId = track.AlbumId; trackToUpdate.Album = track.Album; - trackToUpdate.Explict = track.Explict; - if (track.ArtistSpotifyId.IsNullOrWhiteSpace()) - { - trackToUpdate.ArtistSpotifyId = artist.ForeignArtistId; - } else - { - trackToUpdate.ArtistSpotifyId = track.ArtistSpotifyId; - } - trackToUpdate.ArtistId = track.ArtistId; + trackToUpdate.Explicit = track.Explicit; + trackToUpdate.ArtistId = artist.Id; trackToUpdate.Compilation = track.Compilation; // TODO: Implement rest of [RefreshTrackService] fields @@ -119,7 +112,7 @@ namespace NzbDrone.Core.Music return false; } - var album = albums.SingleOrDefault(c => c.AlbumId == track.AlbumId); + var album = albums.SingleOrDefault(c => c.Id == track.AlbumId); return album == null || album.Monitored; } diff --git a/src/NzbDrone.Core/Music/Track.cs b/src/NzbDrone.Core/Music/Track.cs index 599328c58..8e281260a 100644 --- a/src/NzbDrone.Core/Music/Track.cs +++ b/src/NzbDrone.Core/Music/Track.cs @@ -17,20 +17,20 @@ namespace NzbDrone.Core.Music public const string RELEASE_DATE_FORMAT = "yyyy-MM-dd"; - public string SpotifyTrackId { get; set; } - public string AlbumId { get; set; } + public string ForeignTrackId { get; set; } + public int AlbumId { get; set; } public LazyLoaded Artist { get; set; } - public string ArtistSpotifyId { get; set; } - public long ArtistId { get; set; } // This is the DB Id of the Artist, not the SpotifyId + + public int ArtistId { get; set; } // This is the DB Id of the Artist, not the SpotifyId //public int CompilationId { get; set; } public bool Compilation { get; set; } public int TrackNumber { get; set; } public string Title { get; set; } - public bool Ignored { get; set; } - public bool Explict { get; set; } + //public bool Ignored { get; set; } + public bool Explicit { get; set; } public bool Monitored { get; set; } public int TrackFileId { get; set; } - public DateTime? ReleaseDate { get; set; } + //public DateTime? ReleaseDate { get; set; } public LazyLoaded TrackFile { get; set; } @@ -40,7 +40,7 @@ namespace NzbDrone.Core.Music public override string ToString() { - return string.Format("[{0}]{1}", SpotifyTrackId, Title.NullSafe()); + return string.Format("[{0}]{1}", ForeignTrackId, Title.NullSafe()); } } } diff --git a/src/NzbDrone.Core/Music/TrackRepository.cs b/src/NzbDrone.Core/Music/TrackRepository.cs index 8d707ec09..948ce5936 100644 --- a/src/NzbDrone.Core/Music/TrackRepository.cs +++ b/src/NzbDrone.Core/Music/TrackRepository.cs @@ -13,11 +13,11 @@ namespace NzbDrone.Core.Music { public interface ITrackRepository : IBasicRepository { - Track Find(string artistId, string albumId, int trackNumber); - List GetTracks(string artistId); - List GetTracks(string artistId, string albumId); + Track Find(int artistId, int albumId, int trackNumber); + List GetTracks(int artistId); + List GetTracks(int artistId, int albumId); List GetTracksByFileId(int fileId); - List TracksWithFiles(string artistId); + List TracksWithFiles(int artistId); PagingSpec TracksWithoutFiles(PagingSpec pagingSpec); PagingSpec TracksWhereCutoffUnmet(PagingSpec pagingSpec, List qualitiesBelowCutoff); void SetMonitoredFlat(Track episode, bool monitored); @@ -37,23 +37,23 @@ namespace NzbDrone.Core.Music _logger = logger; } - public Track Find(string artistId, string albumId, int trackNumber) + public Track Find(int artistId, int albumId, int trackNumber) { - return Query.Where(s => s.ArtistSpotifyId == artistId) + return Query.Where(s => s.ArtistId == artistId) .AndWhere(s => s.AlbumId == albumId) .AndWhere(s => s.TrackNumber == trackNumber) .SingleOrDefault(); } - public List GetTracks(string artistId) + public List GetTracks(int artistId) { - return Query.Where(s => s.ArtistSpotifyId == artistId).ToList(); + return Query.Where(s => s.ArtistId == artistId).ToList(); } - public List GetTracks(string artistId, string albumId) + public List GetTracks(int artistId, int albumId) { - return Query.Where(s => s.ArtistSpotifyId == artistId) + return Query.Where(s => s.ArtistId == artistId) .AndWhere(s => s.AlbumId == albumId) .ToList(); } @@ -63,10 +63,10 @@ namespace NzbDrone.Core.Music return Query.Where(e => e.TrackFileId == fileId).ToList(); } - public List TracksWithFiles(string artistId) + public List TracksWithFiles(int artistId) { return Query.Join(JoinType.Inner, e => e.TrackFile, (e, ef) => e.TrackFileId == ef.Id) - .Where(e => e.ArtistSpotifyId == artistId); + .Where(e => e.ArtistId == artistId); } public PagingSpec TracksWhereCutoffUnmet(PagingSpec pagingSpec, List qualitiesBelowCutoff) @@ -118,7 +118,7 @@ namespace NzbDrone.Core.Music private SortBuilder GetMissingEpisodesQuery(PagingSpec pagingSpec, DateTime currentTime) { - return Query.Join(JoinType.Inner, e => e.Artist, (e, s) => e.ArtistSpotifyId == s.ForeignArtistId) + return Query.Join(JoinType.Inner, e => e.Artist, (e, s) => e.ArtistId == s.Id) .Where(pagingSpec.FilterExpression) .AndWhere(e => e.TrackFileId == 0) .AndWhere(BuildAirDateUtcCutoffWhereClause(currentTime)) @@ -130,7 +130,7 @@ namespace NzbDrone.Core.Music private SortBuilder EpisodesWhereCutoffUnmetQuery(PagingSpec pagingSpec, List qualitiesBelowCutoff) { - return Query.Join(JoinType.Inner, e => e.Artist, (e, s) => e.ArtistSpotifyId == s.ForeignArtistId) + return Query.Join(JoinType.Inner, e => e.Artist, (e, s) => e.ArtistId == s.Id) .Join(JoinType.Left, e => e.TrackFile, (e, s) => e.TrackFileId == s.Id) .Where(pagingSpec.FilterExpression) .AndWhere(e => e.TrackFileId != 0) diff --git a/src/NzbDrone.Core/Music/TrackService.cs b/src/NzbDrone.Core/Music/TrackService.cs index 54a415404..834d3b054 100644 --- a/src/NzbDrone.Core/Music/TrackService.cs +++ b/src/NzbDrone.Core/Music/TrackService.cs @@ -15,12 +15,12 @@ namespace NzbDrone.Core.Music { Track GetTrack(int id); List GetTracks(IEnumerable ids); - Track FindTrack(string artistId, string albumId, int trackNumber); - Track FindTrackByTitle(string artistId, string albumId, string releaseTitle); - List GetTracksByArtist(string artistId); + Track FindTrack(int artistId, int albumId, int trackNumber); + Track FindTrackByTitle(int artistId, int albumId, string releaseTitle); + List GetTracksByArtist(int artistId); //List GetTracksByAlbum(string artistId, string albumId); //List GetTracksByAlbumTitle(string artistId, string albumTitle); - List TracksWithFiles(string artistId); + List TracksWithFiles(int artistId); //PagingSpec TracksWithoutFiles(PagingSpec pagingSpec); List GetTracksByFileId(int trackFileId); void UpdateTrack(Track track); @@ -55,22 +55,22 @@ namespace NzbDrone.Core.Music return _trackRepository.Get(ids).ToList(); } - public Track FindTrack(string artistId, string albumId, int episodeNumber) + public Track FindTrack(int artistId, int albumId, int episodeNumber) { return _trackRepository.Find(artistId, albumId, episodeNumber); } - public List GetTracksByArtist(string artistId) + public List GetTracksByArtist(int artistId) { return _trackRepository.GetTracks(artistId).ToList(); } - public List GetTracksByAlbum(string artistId, string albumId) + public List GetTracksByAlbum(int artistId, int albumId) { return _trackRepository.GetTracks(artistId, albumId); } - public Track FindTrackByTitle(string artistId, string albumId, string releaseTitle) + public Track FindTrackByTitle(int artistId, int albumId, string releaseTitle) { // TODO: can replace this search mechanism with something smarter/faster/better var normalizedReleaseTitle = Parser.Parser.NormalizeEpisodeTitle(releaseTitle).Replace(".", " "); @@ -96,7 +96,7 @@ namespace NzbDrone.Core.Music return null; } - public List TracksWithFiles(string artistId) + public List TracksWithFiles(int artistId) { return _trackRepository.TracksWithFiles(artistId); } @@ -154,7 +154,7 @@ namespace NzbDrone.Core.Music public void HandleAsync(ArtistDeletedEvent message) { - var tracks = GetTracksByArtist(message.Artist.ForeignArtistId); + var tracks = GetTracksByArtist(message.Artist.Id); _trackRepository.DeleteMany(tracks); } diff --git a/src/NzbDrone.Core/Parser/Model/LocalTrack.cs b/src/NzbDrone.Core/Parser/Model/LocalTrack.cs index 2f8b35588..359ce6285 100644 --- a/src/NzbDrone.Core/Parser/Model/LocalTrack.cs +++ b/src/NzbDrone.Core/Parser/Model/LocalTrack.cs @@ -19,20 +19,12 @@ namespace NzbDrone.Core.Parser.Model public long Size { get; set; } public ParsedTrackInfo ParsedTrackInfo { get; set; } public Artist Artist { get; set; } + public Album Album { get; set; } public List Tracks { get; set; } public QualityModel Quality { get; set; } public MediaInfoModel MediaInfo { get; set; } public bool ExistingFile { get; set; } - public string Album - { - get - { - return Tracks.Select(c => c.AlbumId).Distinct().Single(); - } - } - - public bool IsSpecial => Album != ""; public override string ToString() {