2017-09-04 02:20:56 +00:00
|
|
|
using System.Collections.Generic;
|
2013-04-15 01:41:39 +00:00
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
2014-12-02 06:26:25 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
2013-09-15 03:49:58 +00:00
|
|
|
using NzbDrone.Core.IndexerSearch.Definitions;
|
2016-09-21 03:40:53 +00:00
|
|
|
using NzbDrone.Core.MediaFiles;
|
2013-04-15 01:41:39 +00:00
|
|
|
using NzbDrone.Core.Parser.Model;
|
2017-04-28 22:05:35 +00:00
|
|
|
using NzbDrone.Core.Music;
|
|
|
|
using System;
|
2019-02-16 14:49:24 +00:00
|
|
|
using System.IO;
|
2013-04-15 01:41:39 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Parser
|
|
|
|
{
|
|
|
|
public interface IParsingService
|
|
|
|
{
|
2017-09-13 03:28:45 +00:00
|
|
|
Artist GetArtist(string title);
|
|
|
|
Artist GetArtistFromTag(string file);
|
2017-08-14 02:58:42 +00:00
|
|
|
RemoteAlbum Map(ParsedAlbumInfo parsedAlbumInfo, SearchCriteriaBase searchCriteria = null);
|
|
|
|
RemoteAlbum Map(ParsedAlbumInfo parsedAlbumInfo, int artistId, IEnumerable<int> albumIds);
|
|
|
|
List<Album> GetAlbums(ParsedAlbumInfo parsedAlbumInfo, Artist artist, SearchCriteriaBase searchCriteria = null);
|
2017-04-28 22:05:35 +00:00
|
|
|
|
|
|
|
// Music stuff here
|
2017-12-13 02:25:30 +00:00
|
|
|
Album GetLocalAlbum(string filename, Artist artist);
|
2013-04-15 01:41:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class ParsingService : IParsingService
|
|
|
|
{
|
2017-04-28 22:05:35 +00:00
|
|
|
private readonly IArtistService _artistService;
|
2017-08-14 02:58:42 +00:00
|
|
|
private readonly IAlbumService _albumService;
|
2017-04-28 22:05:35 +00:00
|
|
|
private readonly ITrackService _trackService;
|
2017-12-13 02:25:30 +00:00
|
|
|
private readonly IMediaFileService _mediaFileService;
|
2013-04-15 01:41:39 +00:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2017-10-18 02:55:02 +00:00
|
|
|
public ParsingService(ITrackService trackService,
|
2017-08-14 02:58:42 +00:00
|
|
|
IArtistService artistService,
|
|
|
|
IAlbumService albumService,
|
2017-12-13 02:25:30 +00:00
|
|
|
IMediaFileService mediaFileService,
|
2013-09-14 00:41:14 +00:00
|
|
|
Logger logger)
|
2013-04-15 01:41:39 +00:00
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
_albumService = albumService;
|
|
|
|
_artistService = artistService;
|
2017-05-11 18:43:05 +00:00
|
|
|
_trackService = trackService;
|
2017-12-13 02:25:30 +00:00
|
|
|
_mediaFileService = mediaFileService;
|
2013-04-15 01:41:39 +00:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2017-09-13 03:28:45 +00:00
|
|
|
public Artist GetArtist(string title)
|
|
|
|
{
|
|
|
|
var parsedAlbumInfo = Parser.ParseAlbumTitle(title);
|
2018-11-16 17:46:46 +00:00
|
|
|
|
|
|
|
if (parsedAlbumInfo != null && !parsedAlbumInfo.ArtistName.IsNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
title = parsedAlbumInfo.ArtistName;
|
|
|
|
}
|
2017-09-13 03:28:45 +00:00
|
|
|
|
2018-11-16 17:46:46 +00:00
|
|
|
var artistInfo = _artistService.FindByName(title);
|
|
|
|
|
|
|
|
if (artistInfo == null)
|
2017-09-13 03:28:45 +00:00
|
|
|
{
|
2018-11-16 17:46:46 +00:00
|
|
|
_logger.Debug("Trying inexact artist match for {0}", title);
|
|
|
|
artistInfo = _artistService.FindByNameInexact(title);
|
2017-09-13 03:28:45 +00:00
|
|
|
}
|
|
|
|
|
2018-11-16 17:46:46 +00:00
|
|
|
return artistInfo;
|
2017-09-13 03:28:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Artist GetArtistFromTag(string file)
|
|
|
|
{
|
|
|
|
var parsedTrackInfo = Parser.ParseMusicPath(file);
|
|
|
|
|
|
|
|
var artist = new Artist();
|
|
|
|
|
|
|
|
if (parsedTrackInfo.ArtistMBId.IsNotNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
artist = _artistService.FindById(parsedTrackInfo.ArtistMBId);
|
|
|
|
|
|
|
|
if (artist != null)
|
|
|
|
{
|
|
|
|
return artist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parsedTrackInfo == null || parsedTrackInfo.ArtistTitle.IsNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-11-16 17:46:46 +00:00
|
|
|
artist = _artistService.FindByName(parsedTrackInfo.ArtistTitle);
|
|
|
|
|
|
|
|
if (artist == null)
|
|
|
|
{
|
|
|
|
_logger.Debug("Trying inexact artist match for {0}", parsedTrackInfo.ArtistTitle);
|
|
|
|
artist = _artistService.FindByNameInexact(parsedTrackInfo.ArtistTitle);
|
|
|
|
}
|
2017-09-13 03:28:45 +00:00
|
|
|
|
2018-11-16 17:46:46 +00:00
|
|
|
return artist;
|
2017-09-13 03:28:45 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
public RemoteAlbum Map(ParsedAlbumInfo parsedAlbumInfo, SearchCriteriaBase searchCriteria = null)
|
|
|
|
{
|
|
|
|
var remoteAlbum = new RemoteAlbum
|
|
|
|
{
|
|
|
|
ParsedAlbumInfo = parsedAlbumInfo,
|
|
|
|
};
|
|
|
|
|
|
|
|
var artist = GetArtist(parsedAlbumInfo, searchCriteria);
|
|
|
|
|
|
|
|
if (artist == null)
|
|
|
|
{
|
|
|
|
return remoteAlbum;
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteAlbum.Artist = artist;
|
|
|
|
remoteAlbum.Albums = GetAlbums(parsedAlbumInfo, artist, searchCriteria);
|
|
|
|
|
|
|
|
return remoteAlbum;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Album> GetAlbums(ParsedAlbumInfo parsedAlbumInfo, Artist artist, SearchCriteriaBase searchCriteria = null)
|
|
|
|
{
|
|
|
|
var albumTitle = parsedAlbumInfo.AlbumTitle;
|
|
|
|
var result = new List<Album>();
|
|
|
|
|
|
|
|
if (parsedAlbumInfo.AlbumTitle == null)
|
|
|
|
{
|
|
|
|
return new List<Album>();
|
|
|
|
}
|
|
|
|
|
|
|
|
Album albumInfo = null;
|
|
|
|
|
2018-02-10 04:03:12 +00:00
|
|
|
if (parsedAlbumInfo.Discography)
|
|
|
|
{
|
|
|
|
if (parsedAlbumInfo.DiscographyStart > 0)
|
|
|
|
{
|
|
|
|
return _albumService.ArtistAlbumsBetweenDates(artist,
|
|
|
|
new DateTime(parsedAlbumInfo.DiscographyStart, 1, 1),
|
|
|
|
new DateTime(parsedAlbumInfo.DiscographyEnd, 12, 31), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parsedAlbumInfo.DiscographyEnd > 0)
|
|
|
|
{
|
|
|
|
return _albumService.ArtistAlbumsBetweenDates(artist,
|
|
|
|
new DateTime(1800, 1, 1),
|
|
|
|
new DateTime(parsedAlbumInfo.DiscographyEnd, 12, 31), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _albumService.GetAlbumsByArtist(artist.Id);
|
|
|
|
}
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
if (searchCriteria != null)
|
|
|
|
{
|
2019-03-10 22:20:24 +00:00
|
|
|
albumInfo = searchCriteria.Albums.ExclusiveOrDefault(e => e.Title == albumTitle);
|
2017-08-14 02:58:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (albumInfo == null)
|
|
|
|
{
|
2017-11-16 02:24:33 +00:00
|
|
|
// TODO: Search by Title and Year instead of just Title when matching
|
2018-12-15 00:02:43 +00:00
|
|
|
albumInfo = _albumService.FindByTitle(artist.ArtistMetadataId, parsedAlbumInfo.AlbumTitle);
|
2017-08-14 02:58:42 +00:00
|
|
|
}
|
|
|
|
|
2018-11-16 17:46:46 +00:00
|
|
|
if (albumInfo == null)
|
|
|
|
{
|
|
|
|
_logger.Debug("Trying inexact album match for {0}", parsedAlbumInfo.AlbumTitle);
|
|
|
|
albumInfo = _albumService.FindByTitleInexact(artist.Id, parsedAlbumInfo.AlbumTitle);
|
|
|
|
}
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
if (albumInfo != null)
|
|
|
|
{
|
|
|
|
result.Add(albumInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_logger.Debug("Unable to find {0}", parsedAlbumInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public RemoteAlbum Map(ParsedAlbumInfo parsedAlbumInfo, int artistId, IEnumerable<int> albumIds)
|
|
|
|
{
|
|
|
|
return new RemoteAlbum
|
|
|
|
{
|
|
|
|
ParsedAlbumInfo = parsedAlbumInfo,
|
|
|
|
Artist = _artistService.GetArtist(artistId),
|
|
|
|
Albums = _albumService.GetAlbums(albumIds)
|
|
|
|
};
|
2015-01-19 23:53:31 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
private Artist GetArtist(ParsedAlbumInfo parsedAlbumInfo, SearchCriteriaBase searchCriteria)
|
|
|
|
{
|
|
|
|
Artist artist = null;
|
|
|
|
|
|
|
|
if (searchCriteria != null)
|
|
|
|
{
|
2017-11-02 01:23:29 +00:00
|
|
|
if (searchCriteria.Artist.CleanName == parsedAlbumInfo.ArtistName.CleanArtistName())
|
2017-08-14 02:58:42 +00:00
|
|
|
{
|
|
|
|
return searchCriteria.Artist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
artist = _artistService.FindByName(parsedAlbumInfo.ArtistName);
|
|
|
|
|
2018-11-16 17:46:46 +00:00
|
|
|
if (artist == null)
|
|
|
|
{
|
|
|
|
_logger.Debug("Trying inexact artist match for {0}", parsedAlbumInfo.ArtistName);
|
|
|
|
artist = _artistService.FindByNameInexact(parsedAlbumInfo.ArtistName);
|
|
|
|
}
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
if (artist == null)
|
|
|
|
{
|
2017-10-25 01:25:29 +00:00
|
|
|
_logger.Debug("No matching artist {0}", parsedAlbumInfo.ArtistName);
|
2017-08-14 02:58:42 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return artist;
|
|
|
|
}
|
|
|
|
|
2017-12-13 02:25:30 +00:00
|
|
|
public Album GetLocalAlbum(string filename, Artist artist)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (Path.HasExtension(filename))
|
|
|
|
{
|
|
|
|
filename = Path.GetDirectoryName(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = artist.Path.GetRelativePath(filename);
|
|
|
|
|
|
|
|
var tracksInAlbum = _mediaFileService.GetFilesByArtist(artist.Id)
|
|
|
|
.FindAll(s => Path.GetDirectoryName(s.RelativePath) == filename)
|
|
|
|
.DistinctBy(s => s.AlbumId)
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
return tracksInAlbum.Count == 1 ? _albumService.GetAlbum(tracksInAlbum.First().AlbumId) : null;
|
|
|
|
}
|
2013-04-15 01:41:39 +00:00
|
|
|
}
|
2017-07-03 18:39:06 +00:00
|
|
|
}
|