2017-09-04 02:20:56 +00:00
|
|
|
using System.Collections.Generic;
|
2014-02-20 16:29:41 +00:00
|
|
|
using System.IO;
|
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;
|
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);
|
2017-04-28 22:05:35 +00:00
|
|
|
LocalTrack GetLocalTrack(string filename, Artist artist);
|
2017-05-11 18:43:05 +00:00
|
|
|
LocalTrack GetLocalTrack(string filename, Artist artist, ParsedTrackInfo folderInfo);
|
2017-04-28 22:05:35 +00:00
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (parsedAlbumInfo == null || parsedAlbumInfo.ArtistName.IsNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
return _artistService.FindByName(title);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _artistService.FindByName(parsedAlbumInfo.ArtistName);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _artistService.FindByName(parsedTrackInfo.ArtistTitle);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
albumInfo = searchCriteria.Albums.SingleOrDefault(e => e.Title == albumTitle);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (albumInfo == null)
|
|
|
|
{
|
2017-11-16 02:24:33 +00:00
|
|
|
// TODO: Search by Title and Year instead of just Title when matching
|
2017-08-14 02:58:42 +00:00
|
|
|
albumInfo = _albumService.FindByTitle(artist.Id, parsedAlbumInfo.AlbumTitle);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-04-28 22:05:35 +00:00
|
|
|
public LocalTrack GetLocalTrack(string filename, Artist artist)
|
|
|
|
{
|
2017-05-11 18:43:05 +00:00
|
|
|
return GetLocalTrack(filename, artist, null);
|
2017-04-28 22:05:35 +00:00
|
|
|
}
|
|
|
|
|
2017-05-11 18:43:05 +00:00
|
|
|
public LocalTrack GetLocalTrack(string filename, Artist artist, ParsedTrackInfo folderInfo)
|
2017-04-28 22:05:35 +00:00
|
|
|
{
|
|
|
|
ParsedTrackInfo parsedTrackInfo;
|
|
|
|
|
2017-07-03 18:39:06 +00:00
|
|
|
|
2017-04-28 22:05:35 +00:00
|
|
|
if (folderInfo != null)
|
|
|
|
{
|
|
|
|
parsedTrackInfo = folderInfo.JsonClone();
|
2017-11-21 00:20:21 +00:00
|
|
|
parsedTrackInfo.Quality = QualityParser.ParseQuality(Path.GetFileName(filename), null, 0);
|
2017-04-28 22:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parsedTrackInfo = Parser.ParseMusicPath(filename);
|
|
|
|
}
|
|
|
|
|
2017-10-15 19:30:36 +00:00
|
|
|
if (parsedTrackInfo == null || parsedTrackInfo.AlbumTitle.IsNullOrWhiteSpace())
|
2017-04-28 22:05:35 +00:00
|
|
|
{
|
|
|
|
if (MediaFileExtensions.Extensions.Contains(Path.GetExtension(filename)))
|
|
|
|
{
|
|
|
|
_logger.Warn("Unable to parse track info from path {0}", filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-09-29 03:32:42 +00:00
|
|
|
var tracks = GetTracks(artist, parsedTrackInfo);
|
2017-09-13 03:28:45 +00:00
|
|
|
var album = _albumService.FindByTitle(artist.Id, parsedTrackInfo.AlbumTitle);
|
2017-04-28 22:05:35 +00:00
|
|
|
|
|
|
|
return new LocalTrack
|
|
|
|
{
|
|
|
|
Artist = artist,
|
2017-09-13 03:28:45 +00:00
|
|
|
Album = album,
|
2017-04-28 22:05:35 +00:00
|
|
|
Quality = parsedTrackInfo.Quality,
|
2017-09-04 02:20:56 +00:00
|
|
|
Language = parsedTrackInfo.Language,
|
2017-04-28 22:05:35 +00:00
|
|
|
Tracks = tracks,
|
|
|
|
Path = filename,
|
|
|
|
ParsedTrackInfo = parsedTrackInfo,
|
|
|
|
ExistingFile = artist.Path.IsParentPath(filename)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-09-29 03:32:42 +00:00
|
|
|
private List<Track> GetTracks(Artist artist, ParsedTrackInfo parsedTrackInfo)
|
2017-05-29 17:09:18 +00:00
|
|
|
{
|
|
|
|
var result = new List<Track>();
|
|
|
|
|
2017-09-15 03:33:13 +00:00
|
|
|
if (parsedTrackInfo.AlbumTitle.IsNullOrWhiteSpace())
|
|
|
|
{
|
2017-09-29 03:32:42 +00:00
|
|
|
_logger.Debug("Album title could not be parsed for {0}", parsedTrackInfo);
|
2017-09-15 03:33:13 +00:00
|
|
|
return new List<Track>();
|
|
|
|
}
|
|
|
|
|
|
|
|
var album = _albumService.FindByTitle(artist.Id, parsedTrackInfo.AlbumTitle);
|
2017-09-29 03:32:42 +00:00
|
|
|
_logger.Debug("Album {0} selected for {1}", album, parsedTrackInfo);
|
2017-09-15 03:33:13 +00:00
|
|
|
|
|
|
|
if (album == null)
|
|
|
|
{
|
2017-09-29 03:32:42 +00:00
|
|
|
_logger.Debug("Parsed album title not found in Db for {0}", parsedTrackInfo);
|
2017-09-15 03:33:13 +00:00
|
|
|
return new List<Track>();
|
|
|
|
}
|
|
|
|
|
|
|
|
Track trackInfo = null;
|
|
|
|
|
2017-09-29 03:32:42 +00:00
|
|
|
if (parsedTrackInfo.Title.IsNotNullOrWhiteSpace())
|
2017-09-15 03:33:13 +00:00
|
|
|
{
|
2017-11-16 02:24:33 +00:00
|
|
|
trackInfo = _trackService.FindTrackByTitle(artist.Id, album.Id, parsedTrackInfo.DiscNumber, parsedTrackInfo.Title);
|
2018-02-16 04:03:44 +00:00
|
|
|
|
2017-09-29 03:32:42 +00:00
|
|
|
if (trackInfo != null)
|
|
|
|
{
|
2018-02-16 04:03:44 +00:00
|
|
|
_logger.Debug("Track {0} selected for {1}", trackInfo, parsedTrackInfo);
|
2017-09-29 03:32:42 +00:00
|
|
|
result.Add(trackInfo);
|
2017-12-29 03:03:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-02-16 04:03:44 +00:00
|
|
|
_logger.Debug("Unable to find track for {0}", parsedTrackInfo);
|
2017-09-29 03:32:42 +00:00
|
|
|
}
|
2017-09-15 03:33:13 +00:00
|
|
|
}
|
|
|
|
|
2017-05-29 17:09:18 +00:00
|
|
|
return result;
|
2017-04-28 22:05:35 +00:00
|
|
|
}
|
2013-04-15 01:41:39 +00:00
|
|
|
}
|
2017-07-03 18:39:06 +00:00
|
|
|
}
|