@ -1,6 +1,7 @@
using System.Collections.Generic ;
using System.Collections.Generic ;
using System ;
using System ;
using System.Linq ;
using System.Linq ;
using System.Text.RegularExpressions ;
using NLog ;
using NLog ;
using NzbDrone.Core.Messaging.Commands ;
using NzbDrone.Core.Messaging.Commands ;
using NzbDrone.Core.MetadataSource ;
using NzbDrone.Core.MetadataSource ;
@ -8,6 +9,10 @@ using NzbDrone.Core.RootFolders;
using NzbDrone.Core.Tv ;
using NzbDrone.Core.Tv ;
using NzbDrone.Core.Configuration ;
using NzbDrone.Core.Configuration ;
using NzbDrone.Common.Extensions ;
using NzbDrone.Common.Extensions ;
using NzbDrone.Common.Instrumentation.Extensions ;
using NzbDrone.Core.DecisionEngine ;
using NzbDrone.Core.Download ;
using NzbDrone.Core.IndexerSearch ;
namespace NzbDrone.Core.NetImport
namespace NzbDrone.Core.NetImport
{
{
@ -25,14 +30,19 @@ namespace NzbDrone.Core.NetImport
private readonly ISearchForNewMovie _movieSearch ;
private readonly ISearchForNewMovie _movieSearch ;
private readonly IRootFolderService _rootFolder ;
private readonly IRootFolderService _rootFolder ;
private readonly IConfigService _configService ;
private readonly IConfigService _configService ;
private readonly ISearchForNzb _nzbSearchService ;
private readonly IProcessDownloadDecisions _processDownloadDecisions ;
public NetImportSearchService ( INetImportFactory netImportFactory , IMovieService movieService ,
public NetImportSearchService ( INetImportFactory netImportFactory , IMovieService movieService ,
ISearchForNewMovie movieSearch , IRootFolderService rootFolder , IConfigService configService , Logger logger )
ISearchForNewMovie movieSearch , IRootFolderService rootFolder , ISearchForNzb nzbSearchService ,
IProcessDownloadDecisions processDownloadDecisions , IConfigService configService , Logger logger )
{
{
_netImportFactory = netImportFactory ;
_netImportFactory = netImportFactory ;
_movieService = movieService ;
_movieService = movieService ;
_movieSearch = movieSearch ;
_movieSearch = movieSearch ;
_nzbSearchService = nzbSearchService ;
_processDownloadDecisions = processDownloadDecisions ;
_rootFolder = rootFolder ;
_rootFolder = rootFolder ;
_logger = logger ;
_logger = logger ;
_configService = configService ;
_configService = configService ;
@ -74,6 +84,8 @@ namespace NzbDrone.Core.NetImport
return movies ;
return movies ;
}
}
public void Execute ( NetImportSyncCommand message )
public void Execute ( NetImportSyncCommand message )
{
{
//if there are no lists that are enabled for automatic import then dont do anything
//if there are no lists that are enabled for automatic import then dont do anything
@ -84,13 +96,69 @@ namespace NzbDrone.Core.NetImport
}
}
var listedMovies = Fetch ( 0 , true ) ;
var listedMovies = Fetch ( 0 , true ) ;
CleanLibrary ( listedMovies ) ;
listedMovies = listedMovies . Where ( x = > ! _movieService . MovieExists ( x ) ) . ToList ( ) ;
if ( listedMovies . Any ( ) )
{
_logger . Info ( $"Found {listedMovies.Count()} movies on your auto enabled lists not in your library" ) ;
}
var importExclusions = new List < string > ( ) ;
if ( _configService . ImportExclusions ! = null )
{
// Replace `movie-title-tmdbid` with just tmdbid in exclusions
importExclusions = _configService . ImportExclusions . Split ( ',' ) . Select ( x = > Regex . Replace ( x , @"^.*\-(.*)$" , "$1" ) ) . ToList ( ) ;
// listedMovies = listedMovies.Where(ah => importExclusions.Any(h => ah.TmdbId.ToString() != h)).ToList();
}
var downloadedCount = 0 ;
foreach ( var movie in listedMovies )
{
var mapped = _movieSearch . MapMovieToTmdbMovie ( movie ) ;
if ( mapped ! = null & & ! importExclusions . Any ( x = > x = = mapped . TmdbId . ToString ( ) ) )
{
List < DownloadDecision > decisions ;
mapped . AddOptions = new AddMovieOptions { SearchForMovie = true } ;
_movieService . AddMovie ( mapped ) ;
// Search for movie
try
{
decisions = _nzbSearchService . MovieSearch ( mapped . Id , false ) ;
}
catch ( Exception ex )
{
_logger . Error ( ex , $"Unable to search in list for movie {mapped.Id}" ) ;
continue ;
}
var processed = _processDownloadDecisions . ProcessDecisions ( decisions ) ;
downloadedCount + = processed . Grabbed . Count ;
}
else
{
if ( mapped ! = null )
{
_logger . Info ( $"{mapped.Title} ({mapped.TitleSlug}) will not be added since it was found on the exclusions list" ) ;
}
}
}
_logger . ProgressInfo ( "Movie search completed. {0} reports downloaded." , downloadedCount ) ;
}
private void CleanLibrary ( List < Movie > movies )
{
if ( _configService . ListSyncLevel ! = "disabled" )
if ( _configService . ListSyncLevel ! = "disabled" )
{
{
var moviesInLibrary = _movieService . GetAllMovies ( ) ;
var moviesInLibrary = _movieService . GetAllMovies ( ) ;
foreach ( var movie in moviesInLibrary )
foreach ( var movie in moviesInLibrary )
{
{
bool foundMatch = false ;
bool foundMatch = false ;
foreach ( var listedMovie in listedMovies )
foreach ( var listedMovie in m ovies)
{
{
if ( movie . TmdbId = = listedMovie . TmdbId )
if ( movie . TmdbId = = listedMovie . TmdbId )
{
{
@ -101,7 +169,7 @@ namespace NzbDrone.Core.NetImport
}
}
if ( ! foundMatch )
if ( ! foundMatch )
{
{
switch ( _configService . ListSyncLevel )
switch ( _configService . ListSyncLevel )
{
{
case "logOnly" :
case "logOnly" :
_logger . Info ( "{0} was in your library, but not found in your lists --> You might want to unmonitor or remove it" , movie ) ;
_logger . Info ( "{0} was in your library, but not found in your lists --> You might want to unmonitor or remove it" , movie ) ;
@ -120,51 +188,10 @@ namespace NzbDrone.Core.NetImport
//TODO: for some reason the files are not deleted in this case... any idea why?
//TODO: for some reason the files are not deleted in this case... any idea why?
break ;
break ;
default :
default :
break ;
}
}
}
}
List < string > importExclusions = null ;
if ( _configService . ImportExclusions ! = String . Empty )
{
importExclusions = _configService . ImportExclusions . Split ( ',' ) . ToList ( ) ;
}
var movies = listedMovies . Where ( x = > ! _movieService . MovieExists ( x ) ) . ToList ( ) ;
if ( movies . Count > 0 )
{
_logger . Info ( "Found {0} movies on your auto enabled lists not in your library" , movies . Count ) ;
}
foreach ( var movie in movies )
{
bool shouldAdd = true ;
if ( importExclusions ! = null )
{
foreach ( var exclusion in importExclusions )
{
//var excludedTmdbId = exclusion.Substring(exclusion.LastIndexOf('-')+1);
int excludedTmdbId ;
if ( Int32 . TryParse ( exclusion . Substring ( exclusion . LastIndexOf ( '-' ) + 1 ) , out excludedTmdbId ) )
{
if ( excludedTmdbId = = movie . TmdbId )
{
_logger . Info ( "Movie: {0} was found but will not be added because {1} was found on your exclusion list" , movie , exclusion ) ;
shouldAdd = false ;
break ;
break ;
}
}
}
}
}
}
}
var mapped = _movieSearch . MapMovieToTmdbMovie ( movie ) ;
if ( ( mapped ! = null ) & & shouldAdd )
{
_movieService . AddMovie ( mapped ) ;
}
}
}
}
}
}
}