2017-09-04 02:20:56 +00:00
|
|
|
using NLog;
|
2017-06-18 02:27:01 +00:00
|
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
using NzbDrone.Core.Music.Events;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2017-07-02 00:21:39 +00:00
|
|
|
using NzbDrone.Core.Datastore;
|
2018-11-16 17:46:46 +00:00
|
|
|
using NzbDrone.Core.Parser;
|
|
|
|
using NzbDrone.Common.Extensions;
|
2017-06-18 02:27:01 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Music
|
|
|
|
{
|
|
|
|
public interface IAlbumService
|
|
|
|
{
|
2017-11-16 02:24:33 +00:00
|
|
|
Album GetAlbum(int albumId);
|
2017-06-18 02:27:01 +00:00
|
|
|
List<Album> GetAlbums(IEnumerable<int> albumIds);
|
|
|
|
List<Album> GetAlbumsByArtist(int artistId);
|
2018-12-15 00:02:43 +00:00
|
|
|
List<Album> GetAlbumsByArtistMetadataId(int artistMetadataId);
|
|
|
|
Album AddAlbum(Album newAlbum, string albumArtistId);
|
2019-01-12 16:56:13 +00:00
|
|
|
Album FindById(string foreignId);
|
|
|
|
List<Album> FindById(List<string> foreignIds);
|
2017-08-14 02:58:42 +00:00
|
|
|
Album FindByTitle(int artistId, string title);
|
2018-10-20 22:09:12 +00:00
|
|
|
Album FindByTitleInexact(int artistId, string title);
|
2017-06-18 02:27:01 +00:00
|
|
|
void DeleteAlbum(int albumId, bool deleteFiles);
|
|
|
|
List<Album> GetAllAlbums();
|
|
|
|
Album UpdateAlbum(Album album);
|
|
|
|
List<Album> UpdateAlbums(List<Album> album);
|
2017-06-25 13:17:49 +00:00
|
|
|
void SetAlbumMonitored(int albumId, bool monitored);
|
2017-09-04 02:20:56 +00:00
|
|
|
void SetMonitored(IEnumerable<int> ids, bool monitored);
|
2017-07-02 00:21:39 +00:00
|
|
|
PagingSpec<Album> AlbumsWithoutFiles(PagingSpec<Album> pagingSpec);
|
2017-06-28 02:25:51 +00:00
|
|
|
List<Album> AlbumsBetweenDates(DateTime start, DateTime end, bool includeUnmonitored);
|
2018-02-10 04:03:12 +00:00
|
|
|
List<Album> ArtistAlbumsBetweenDates(Artist artist, DateTime start, DateTime end, bool includeUnmonitored);
|
2017-06-18 02:27:01 +00:00
|
|
|
void InsertMany(List<Album> albums);
|
|
|
|
void UpdateMany(List<Album> albums);
|
|
|
|
void DeleteMany(List<Album> albums);
|
|
|
|
void RemoveAddOptions(Album album);
|
2018-12-15 00:02:43 +00:00
|
|
|
Album FindAlbumByRelease(string albumReleaseId);
|
|
|
|
Album FindAlbumByTrackId(int trackId);
|
2018-04-08 06:25:34 +00:00
|
|
|
List<Album> GetArtistAlbumsWithFiles(Artist artist);
|
2017-06-18 02:27:01 +00:00
|
|
|
}
|
|
|
|
|
2017-07-03 18:39:06 +00:00
|
|
|
public class AlbumService : IAlbumService,
|
|
|
|
IHandleAsync<ArtistDeletedEvent>
|
2017-06-18 02:27:01 +00:00
|
|
|
{
|
|
|
|
private readonly IAlbumRepository _albumRepository;
|
2018-12-15 00:02:43 +00:00
|
|
|
private readonly IReleaseRepository _releaseRepository;
|
|
|
|
private readonly IArtistMetadataRepository _artistMetadataRepository;
|
2017-06-18 02:27:01 +00:00
|
|
|
private readonly IEventAggregator _eventAggregator;
|
|
|
|
private readonly ITrackService _trackService;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
public AlbumService(IAlbumRepository albumRepository,
|
2018-12-15 00:02:43 +00:00
|
|
|
IReleaseRepository releaseRepository,
|
|
|
|
IArtistMetadataRepository artistMetadataRepository,
|
2017-06-18 02:27:01 +00:00
|
|
|
IEventAggregator eventAggregator,
|
|
|
|
ITrackService trackService,
|
|
|
|
Logger logger)
|
|
|
|
{
|
|
|
|
_albumRepository = albumRepository;
|
2018-12-15 00:02:43 +00:00
|
|
|
_releaseRepository = releaseRepository;
|
|
|
|
_artistMetadataRepository = artistMetadataRepository;
|
2017-06-18 02:27:01 +00:00
|
|
|
_eventAggregator = eventAggregator;
|
|
|
|
_trackService = trackService;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2018-12-15 00:02:43 +00:00
|
|
|
public Album AddAlbum(Album newAlbum, string albumArtistId)
|
2017-06-18 02:27:01 +00:00
|
|
|
{
|
|
|
|
_albumRepository.Insert(newAlbum);
|
2018-12-15 00:02:43 +00:00
|
|
|
|
|
|
|
foreach (var release in newAlbum.AlbumReleases.Value)
|
|
|
|
{
|
|
|
|
release.AlbumId = newAlbum.Id;
|
|
|
|
}
|
|
|
|
_releaseRepository.InsertMany(newAlbum.AlbumReleases.Value);
|
|
|
|
|
|
|
|
newAlbum.ArtistMetadata = _artistMetadataRepository.FindById(albumArtistId);
|
|
|
|
newAlbum.ArtistMetadataId = newAlbum.ArtistMetadata.Value.Id;
|
|
|
|
|
|
|
|
_albumRepository.Update(newAlbum);
|
2018-01-18 02:28:47 +00:00
|
|
|
//_eventAggregator.PublishEvent(new AlbumAddedEvent(GetAlbum(newAlbum.Id)));
|
2017-06-18 02:27:01 +00:00
|
|
|
|
|
|
|
return newAlbum;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DeleteAlbum(int albumId, bool deleteFiles)
|
|
|
|
{
|
|
|
|
var album = _albumRepository.Get(albumId);
|
|
|
|
_albumRepository.Delete(albumId);
|
|
|
|
_eventAggregator.PublishEvent(new AlbumDeletedEvent(album, deleteFiles));
|
|
|
|
}
|
|
|
|
|
2017-07-03 18:39:06 +00:00
|
|
|
public Album FindById(string lidarrId)
|
2017-06-18 02:27:01 +00:00
|
|
|
{
|
2017-07-03 18:39:06 +00:00
|
|
|
return _albumRepository.FindById(lidarrId);
|
2017-06-18 02:27:01 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 16:56:13 +00:00
|
|
|
public List<Album> FindById(List<string> ids)
|
|
|
|
{
|
|
|
|
return _albumRepository.FindById(ids);
|
|
|
|
}
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
public Album FindByTitle(int artistId, string title)
|
|
|
|
{
|
|
|
|
return _albumRepository.FindByTitle(artistId, title);
|
|
|
|
}
|
2017-06-18 02:27:01 +00:00
|
|
|
|
2018-10-20 22:09:12 +00:00
|
|
|
public Album FindByTitleInexact(int artistId, string title)
|
2017-06-18 02:27:01 +00:00
|
|
|
{
|
2018-11-16 17:46:46 +00:00
|
|
|
var cleanTitle = title.CleanArtistName();
|
|
|
|
|
2018-12-15 00:02:43 +00:00
|
|
|
var albums = GetAlbumsByArtistMetadataId(artistId);
|
2018-11-16 17:46:46 +00:00
|
|
|
|
|
|
|
Func< Func<Album, string, double>, string, Tuple<Func<Album, string, double>, string>> tc = Tuple.Create;
|
|
|
|
var scoringFunctions = new List<Tuple<Func<Album, string, double>, string>> {
|
|
|
|
tc((a, t) => a.CleanTitle.FuzzyMatch(t), cleanTitle),
|
|
|
|
tc((a, t) => a.Title.FuzzyMatch(t), title),
|
|
|
|
tc((a, t) => a.CleanTitle.FuzzyMatch(t), title.RemoveBracketsAndContents().CleanArtistName()),
|
|
|
|
tc((a, t) => a.CleanTitle.FuzzyMatch(t), title.RemoveAfterDash().CleanArtistName()),
|
|
|
|
tc((a, t) => a.CleanTitle.FuzzyMatch(t), title.RemoveBracketsAndContents().RemoveAfterDash().CleanArtistName()),
|
|
|
|
tc((a, t) => t.FuzzyContains(a.CleanTitle), cleanTitle),
|
|
|
|
tc((a, t) => t.FuzzyContains(a.Title), title)
|
|
|
|
};
|
|
|
|
|
|
|
|
foreach (var func in scoringFunctions)
|
|
|
|
{
|
|
|
|
var album = FindByStringInexact(albums, func.Item1, func.Item2);
|
|
|
|
if (album != null)
|
|
|
|
{
|
|
|
|
return album;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Album FindByStringInexact(List<Album> albums, Func<Album, string, double> scoreFunction, string title)
|
|
|
|
{
|
|
|
|
const double fuzzThreshold = 0.7;
|
|
|
|
const double fuzzGap = 0.4;
|
|
|
|
|
|
|
|
var sortedAlbums = albums.Select(s => new
|
|
|
|
{
|
|
|
|
MatchProb = scoreFunction(s, title),
|
|
|
|
Album = s
|
|
|
|
})
|
|
|
|
.ToList()
|
|
|
|
.OrderByDescending(s => s.MatchProb)
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
if (!sortedAlbums.Any())
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.Trace("\nFuzzy album match on '{0}':\n{1}",
|
|
|
|
title,
|
|
|
|
string.Join("\n", sortedAlbums.Select(x => $"[{x.Album.Title}] {x.Album.CleanTitle}: {x.MatchProb}")));
|
|
|
|
|
|
|
|
if (sortedAlbums[0].MatchProb > fuzzThreshold
|
|
|
|
&& (sortedAlbums.Count == 1 || sortedAlbums[0].MatchProb - sortedAlbums[1].MatchProb > fuzzGap))
|
|
|
|
{
|
|
|
|
return sortedAlbums[0].Album;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2017-06-18 02:27:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<Album> GetAllAlbums()
|
|
|
|
{
|
|
|
|
return _albumRepository.All().ToList();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Album GetAlbum(int albumId)
|
|
|
|
{
|
|
|
|
return _albumRepository.Get(albumId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Album> GetAlbums(IEnumerable<int> albumIds)
|
|
|
|
{
|
|
|
|
return _albumRepository.Get(albumIds).ToList();
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Album> GetAlbumsByArtist(int artistId)
|
|
|
|
{
|
|
|
|
return _albumRepository.GetAlbums(artistId).ToList();
|
|
|
|
}
|
|
|
|
|
2018-12-15 00:02:43 +00:00
|
|
|
public List<Album> GetAlbumsByArtistMetadataId(int artistMetadataId)
|
|
|
|
{
|
|
|
|
return _albumRepository.GetAlbumsByArtistMetadataId(artistMetadataId).ToList();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Album FindAlbumByRelease(string albumReleaseId)
|
|
|
|
{
|
|
|
|
return _albumRepository.FindAlbumByRelease(albumReleaseId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Album FindAlbumByTrackId(int trackId)
|
2018-04-07 04:52:28 +00:00
|
|
|
{
|
2018-12-15 00:02:43 +00:00
|
|
|
return _albumRepository.FindAlbumByTrack(trackId);
|
2018-04-07 04:52:28 +00:00
|
|
|
}
|
|
|
|
|
2017-06-18 02:27:01 +00:00
|
|
|
public void RemoveAddOptions(Album album)
|
|
|
|
{
|
2018-12-15 00:02:43 +00:00
|
|
|
var rg = _albumRepository.Get(album.Id);
|
|
|
|
_albumRepository.SetFields(rg, s => s.AddOptions);
|
2017-06-18 02:27:01 +00:00
|
|
|
}
|
|
|
|
|
2017-07-02 00:21:39 +00:00
|
|
|
public PagingSpec<Album> AlbumsWithoutFiles(PagingSpec<Album> pagingSpec)
|
|
|
|
{
|
|
|
|
var albumResult = _albumRepository.AlbumsWithoutFiles(pagingSpec);
|
|
|
|
|
|
|
|
return albumResult;
|
|
|
|
}
|
|
|
|
|
2017-06-28 02:25:51 +00:00
|
|
|
public List<Album> AlbumsBetweenDates(DateTime start, DateTime end, bool includeUnmonitored)
|
|
|
|
{
|
|
|
|
var albums = _albumRepository.AlbumsBetweenDates(start.ToUniversalTime(), end.ToUniversalTime(), includeUnmonitored);
|
|
|
|
|
|
|
|
return albums;
|
|
|
|
}
|
|
|
|
|
2018-02-10 04:03:12 +00:00
|
|
|
public List<Album> ArtistAlbumsBetweenDates(Artist artist, DateTime start, DateTime end, bool includeUnmonitored)
|
|
|
|
{
|
|
|
|
var albums = _albumRepository.ArtistAlbumsBetweenDates(artist, start.ToUniversalTime(), end.ToUniversalTime(), includeUnmonitored);
|
|
|
|
|
|
|
|
return albums;
|
|
|
|
}
|
|
|
|
|
2018-04-08 06:25:34 +00:00
|
|
|
public List<Album> GetArtistAlbumsWithFiles(Artist artist)
|
|
|
|
{
|
|
|
|
return _albumRepository.GetArtistAlbumsWithFiles(artist);
|
|
|
|
}
|
|
|
|
|
2017-06-18 02:27:01 +00:00
|
|
|
public void InsertMany(List<Album> albums)
|
|
|
|
{
|
|
|
|
_albumRepository.InsertMany(albums);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UpdateMany(List<Album> albums)
|
|
|
|
{
|
|
|
|
_albumRepository.UpdateMany(albums);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DeleteMany(List<Album> albums)
|
|
|
|
{
|
|
|
|
_albumRepository.DeleteMany(albums);
|
2018-01-18 02:28:47 +00:00
|
|
|
|
|
|
|
foreach (var album in albums)
|
|
|
|
{
|
|
|
|
_eventAggregator.PublishEvent(new AlbumDeletedEvent(album, false));
|
|
|
|
}
|
2017-06-18 02:27:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Album UpdateAlbum(Album album)
|
|
|
|
{
|
2018-01-18 02:28:47 +00:00
|
|
|
var storedAlbum = GetAlbum(album.Id);
|
2017-06-18 02:27:01 +00:00
|
|
|
var updatedAlbum = _albumRepository.Update(album);
|
2018-12-15 00:02:43 +00:00
|
|
|
|
|
|
|
// If updatedAlbum has populated the Releases, populate in the storedAlbum too
|
|
|
|
if (updatedAlbum.AlbumReleases.IsLoaded)
|
|
|
|
{
|
|
|
|
storedAlbum.AlbumReleases.LazyLoad();
|
|
|
|
}
|
2017-06-18 02:27:01 +00:00
|
|
|
_eventAggregator.PublishEvent(new AlbumEditedEvent(updatedAlbum, storedAlbum));
|
|
|
|
|
|
|
|
return updatedAlbum;
|
|
|
|
}
|
|
|
|
|
2017-06-25 13:17:49 +00:00
|
|
|
public void SetAlbumMonitored(int albumId, bool monitored)
|
|
|
|
{
|
|
|
|
var album = _albumRepository.Get(albumId);
|
|
|
|
_albumRepository.SetMonitoredFlat(album, monitored);
|
|
|
|
|
|
|
|
_logger.Debug("Monitored flag for Album:{0} was set to {1}", albumId, monitored);
|
|
|
|
}
|
|
|
|
|
2017-09-04 02:20:56 +00:00
|
|
|
public void SetMonitored(IEnumerable<int> ids, bool monitored)
|
|
|
|
{
|
|
|
|
_albumRepository.SetMonitored(ids, monitored);
|
|
|
|
}
|
|
|
|
|
2018-02-10 04:52:45 +00:00
|
|
|
public List<Album> UpdateAlbums(List<Album> albums)
|
2017-06-18 02:27:01 +00:00
|
|
|
{
|
2018-02-10 04:52:45 +00:00
|
|
|
_logger.Debug("Updating {0} albums", albums.Count);
|
2017-06-18 02:27:01 +00:00
|
|
|
|
2018-02-10 04:52:45 +00:00
|
|
|
_albumRepository.UpdateMany(albums);
|
|
|
|
_logger.Debug("{0} albums updated", albums.Count);
|
2017-06-18 02:27:01 +00:00
|
|
|
|
2018-02-10 04:52:45 +00:00
|
|
|
return albums;
|
2017-06-18 02:27:01 +00:00
|
|
|
}
|
2017-07-03 18:39:06 +00:00
|
|
|
|
|
|
|
public void HandleAsync(ArtistDeletedEvent message)
|
|
|
|
{
|
2018-12-15 00:02:43 +00:00
|
|
|
var albums = GetAlbumsByArtistMetadataId(message.Artist.ArtistMetadataId);
|
|
|
|
DeleteMany(albums);
|
2017-07-03 18:39:06 +00:00
|
|
|
}
|
2017-06-18 02:27:01 +00:00
|
|
|
}
|
|
|
|
}
|