2014-12-04 05:24:41 +00:00
using MediaBrowser.Common.IO ;
2014-11-16 20:44:08 +00:00
using MediaBrowser.Controller.Entities ;
2013-09-18 02:43:34 +00:00
using MediaBrowser.Controller.Entities.Audio ;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Controller.Entities.TV ;
using MediaBrowser.Controller.Library ;
2014-11-16 20:44:08 +00:00
using MediaBrowser.Controller.Providers ;
2013-03-03 16:53:58 +00:00
using MediaBrowser.Controller.Resolvers ;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Model.Entities ;
2014-07-30 03:31:35 +00:00
using MediaBrowser.Model.Logging ;
2013-02-21 01:33:05 +00:00
using System ;
2014-11-16 20:44:08 +00:00
using System.Collections.Generic ;
using System.Globalization ;
2013-02-21 01:33:05 +00:00
using System.IO ;
2014-11-16 20:44:08 +00:00
using System.Linq ;
using System.Text.RegularExpressions ;
2013-02-21 01:33:05 +00:00
2013-03-03 06:58:04 +00:00
namespace MediaBrowser.Server.Implementations.Library.Resolvers.TV
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Class SeriesResolver
/// </summary>
2013-03-03 06:58:04 +00:00
public class SeriesResolver : FolderResolver < Series >
2013-02-21 01:33:05 +00:00
{
2014-07-26 17:30:15 +00:00
private readonly IFileSystem _fileSystem ;
2014-07-30 03:31:35 +00:00
private readonly ILogger _logger ;
2014-11-16 20:44:08 +00:00
private readonly ILibraryManager _libraryManager ;
2014-07-26 17:30:15 +00:00
2014-11-16 20:44:08 +00:00
public SeriesResolver ( IFileSystem fileSystem , ILogger logger , ILibraryManager libraryManager )
2014-07-26 17:30:15 +00:00
{
_fileSystem = fileSystem ;
2014-07-30 03:31:35 +00:00
_logger = logger ;
2014-11-16 20:44:08 +00:00
_libraryManager = libraryManager ;
2014-07-26 17:30:15 +00:00
}
2013-02-21 01:33:05 +00:00
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
public override ResolverPriority Priority
{
get
{
return ResolverPriority . Second ;
}
}
/// <summary>
/// Resolves the specified args.
/// </summary>
/// <param name="args">The args.</param>
/// <returns>Series.</returns>
protected override Series Resolve ( ItemResolveArgs args )
{
if ( args . IsDirectory )
{
// Avoid expensive tests against VF's and all their children by not allowing this
if ( args . Parent = = null | | args . Parent . IsRoot )
{
return null ;
}
// Optimization to avoid running these tests against Seasons
2014-12-15 05:16:23 +00:00
if ( args . HasParent < Series > ( ) | | args . HasParent < Season > ( ) | | args . HasParent < MusicArtist > ( ) | | args . HasParent < MusicAlbum > ( ) )
2013-02-21 01:33:05 +00:00
{
return null ;
}
2013-09-04 17:07:35 +00:00
var collectionType = args . GetCollectionType ( ) ;
2013-07-27 18:24:48 +00:00
2014-07-31 02:09:23 +00:00
var isTvShowsFolder = string . Equals ( collectionType , CollectionType . TvShows ,
StringComparison . OrdinalIgnoreCase ) ;
2013-07-27 18:24:48 +00:00
// If there's a collection type and it's not tv, it can't be a series
if ( ! string . IsNullOrEmpty ( collectionType ) & &
2014-07-31 02:09:23 +00:00
! isTvShowsFolder & &
2013-10-26 22:01:21 +00:00
! string . Equals ( collectionType , CollectionType . BoxSets , StringComparison . OrdinalIgnoreCase ) )
2013-07-27 18:24:48 +00:00
{
return null ;
}
2014-07-26 17:30:15 +00:00
2014-12-15 05:16:23 +00:00
if ( IsSeriesFolder ( args . Path , collectionType , args . FileSystemChildren , args . DirectoryService , _fileSystem , _logger , _libraryManager ) )
2013-02-21 01:33:05 +00:00
{
2014-11-29 19:51:30 +00:00
return new Series
{
Path = args . Path ,
2014-12-04 05:24:41 +00:00
Name = Path . GetFileName ( args . Path )
2014-11-29 19:51:30 +00:00
} ;
2013-02-21 01:33:05 +00:00
}
}
return null ;
}
2014-11-16 20:44:08 +00:00
/// <summary>
/// Determines whether [is series folder] [the specified path].
/// </summary>
/// <param name="path">The path.</param>
2014-12-15 05:16:23 +00:00
/// <param name="collectionType">Type of the collection.</param>
2014-11-16 20:44:08 +00:00
/// <param name="fileSystemChildren">The file system children.</param>
/// <param name="directoryService">The directory service.</param>
/// <param name="fileSystem">The file system.</param>
2014-12-11 06:20:28 +00:00
/// <param name="logger">The logger.</param>
/// <param name="libraryManager">The library manager.</param>
2014-11-16 20:44:08 +00:00
/// <returns><c>true</c> if [is series folder] [the specified path]; otherwise, <c>false</c>.</returns>
2014-12-15 05:16:23 +00:00
public static bool IsSeriesFolder ( string path , string collectionType , IEnumerable < FileSystemInfo > fileSystemChildren , IDirectoryService directoryService , IFileSystem fileSystem , ILogger logger , ILibraryManager libraryManager )
2014-11-16 20:44:08 +00:00
{
foreach ( var child in fileSystemChildren )
{
var attributes = child . Attributes ;
if ( ( attributes & FileAttributes . Hidden ) = = FileAttributes . Hidden )
{
//logger.Debug("Igoring series file or folder marked hidden: {0}", child.FullName);
continue ;
}
// Can't enforce this because files saved by Bitcasa are always marked System
//if ((attributes & FileAttributes.System) == FileAttributes.System)
//{
// logger.Debug("Igoring series subfolder marked system: {0}", child.FullName);
// continue;
//}
if ( ( attributes & FileAttributes . Directory ) = = FileAttributes . Directory )
{
2014-12-15 05:16:23 +00:00
if ( IsSeasonFolder ( child . FullName , collectionType , directoryService , fileSystem ) )
2014-11-16 20:44:08 +00:00
{
//logger.Debug("{0} is a series because of season folder {1}.", path, child.FullName);
return true ;
}
}
else
{
var fullName = child . FullName ;
if ( libraryManager . IsVideoFile ( fullName ) | | IsVideoPlaceHolder ( fullName ) )
{
2014-12-15 05:16:23 +00:00
var isTvShowsFolder = string . Equals ( collectionType , CollectionType . TvShows , StringComparison . OrdinalIgnoreCase ) ;
if ( GetEpisodeNumberFromFile ( fullName , isTvShowsFolder ) . HasValue )
2014-11-16 20:44:08 +00:00
{
return true ;
}
}
}
}
logger . Debug ( "{0} is not a series folder." , path ) ;
return false ;
}
/// <summary>
/// Determines whether [is place holder] [the specified path].
/// </summary>
/// <param name="path">The path.</param>
/// <returns><c>true</c> if [is place holder] [the specified path]; otherwise, <c>false</c>.</returns>
/// <exception cref="System.ArgumentNullException">path</exception>
private static bool IsVideoPlaceHolder ( string path )
{
if ( string . IsNullOrEmpty ( path ) )
{
throw new ArgumentNullException ( "path" ) ;
}
var extension = Path . GetExtension ( path ) ;
return string . Equals ( extension , ".disc" , StringComparison . OrdinalIgnoreCase ) ;
}
/// <summary>
/// Determines whether [is season folder] [the specified path].
/// </summary>
/// <param name="path">The path.</param>
2014-12-15 05:16:23 +00:00
/// <param name="collectionType">Type of the collection.</param>
2014-11-16 20:44:08 +00:00
/// <param name="directoryService">The directory service.</param>
/// <param name="fileSystem">The file system.</param>
/// <returns><c>true</c> if [is season folder] [the specified path]; otherwise, <c>false</c>.</returns>
2014-12-15 05:16:23 +00:00
private static bool IsSeasonFolder ( string path , string collectionType , IDirectoryService directoryService , IFileSystem fileSystem )
2014-11-16 20:44:08 +00:00
{
2014-12-15 05:16:23 +00:00
var seasonNumber = GetSeasonNumberFromPath ( path , collectionType ) ;
2014-11-16 20:44:08 +00:00
var hasSeasonNumber = seasonNumber ! = null ;
if ( ! hasSeasonNumber )
{
return false ;
}
//// It's a season folder if it's named as such and does not contain any audio files, apart from theme.mp3
//foreach (var fileSystemInfo in directoryService.GetFileSystemEntries(path))
//{
// var attributes = fileSystemInfo.Attributes;
// if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
// {
// continue;
// }
// // Can't enforce this because files saved by Bitcasa are always marked System
// //if ((attributes & FileAttributes.System) == FileAttributes.System)
// //{
// // continue;
// //}
// if ((attributes & FileAttributes.Directory) == FileAttributes.Directory)
// {
// //if (IsBadFolder(fileSystemInfo.Name))
// //{
// // return false;
// //}
// }
// else
// {
// if (EntityResolutionHelper.IsAudioFile(fileSystemInfo.FullName) &&
// !string.Equals(fileSystem.GetFileNameWithoutExtension(fileSystemInfo), BaseItem.ThemeSongFilename))
// {
// return false;
// }
// }
//}
return true ;
}
/// <summary>
/// A season folder must contain one of these somewhere in the name
/// </summary>
private static readonly string [ ] SeasonFolderNames =
{
"season" ,
"sæson" ,
"temporada" ,
"saison" ,
"staffel" ,
"series" ,
"сезон"
} ;
/// <summary>
/// Used to detect paths that represent episodes, need to make sure they don't also
/// match movie titles like "2001 A Space..."
/// Currently we limit the numbers here to 2 digits to try and avoid this
/// </summary>
private static readonly Regex [ ] EpisodeExpressions =
{
new Regex (
@".*(\\|\/)[sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3})[^\\\/]*$" ,
RegexOptions . Compiled ) ,
new Regex (
@".*(\\|\/)[sS](?<seasonnumber>\d{1,4})[x,X]?[eE](?<epnumber>\d{1,3})[^\\\/]*$" ,
RegexOptions . Compiled ) ,
new Regex (
@".*(\\|\/)(?<seriesname>((?![sS]?\d{1,4}[xX]\d{1,3})[^\\\/])*)?([sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3}))[^\\\/]*$" ,
RegexOptions . Compiled ) ,
new Regex (
@".*(\\|\/)(?<seriesname>[^\\\/]*)[sS](?<seasonnumber>\d{1,4})[xX\.]?[eE](?<epnumber>\d{1,3})[^\\\/]*$" ,
RegexOptions . Compiled )
} ;
private static readonly Regex [ ] MultipleEpisodeExpressions =
{
new Regex (
@".*(\\|\/)[sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3})((-| - )\d{1,4}[eExX](?<endingepnumber>\d{1,3}))+[^\\\/]*$" ,
RegexOptions . Compiled ) ,
new Regex (
@".*(\\|\/)[sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3})((-| - )\d{1,4}[xX][eE](?<endingepnumber>\d{1,3}))+[^\\\/]*$" ,
RegexOptions . Compiled ) ,
new Regex (
@".*(\\|\/)[sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3})((-| - )?[xXeE](?<endingepnumber>\d{1,3}))+[^\\\/]*$" ,
RegexOptions . Compiled ) ,
new Regex (
@".*(\\|\/)[sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3})(-[xE]?[eE]?(?<endingepnumber>\d{1,3}))+[^\\\/]*$" ,
RegexOptions . Compiled ) ,
new Regex (
@".*(\\|\/)(?<seriesname>((?![sS]?\d{1,4}[xX]\d{1,3})[^\\\/])*)?([sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3}))((-| - )\d{1,4}[xXeE](?<endingepnumber>\d{1,3}))+[^\\\/]*$" ,
RegexOptions . Compiled ) ,
new Regex (
@".*(\\|\/)(?<seriesname>((?![sS]?\d{1,4}[xX]\d{1,3})[^\\\/])*)?([sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3}))((-| - )\d{1,4}[xX][eE](?<endingepnumber>\d{1,3}))+[^\\\/]*$" ,
RegexOptions . Compiled ) ,
new Regex (
@".*(\\|\/)(?<seriesname>((?![sS]?\d{1,4}[xX]\d{1,3})[^\\\/])*)?([sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3}))((-| - )?[xXeE](?<endingepnumber>\d{1,3}))+[^\\\/]*$" ,
RegexOptions . Compiled ) ,
new Regex (
@".*(\\|\/)(?<seriesname>((?![sS]?\d{1,4}[xX]\d{1,3})[^\\\/])*)?([sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3}))(-[xX]?[eE]?(?<endingepnumber>\d{1,3}))+[^\\\/]*$" ,
RegexOptions . Compiled ) ,
new Regex (
@".*(\\|\/)(?<seriesname>[^\\\/]*)[sS](?<seasonnumber>\d{1,4})[xX\.]?[eE](?<epnumber>\d{1,3})((-| - )?[xXeE](?<endingepnumber>\d{1,3}))+[^\\\/]*$" ,
RegexOptions . Compiled ) ,
new Regex (
@".*(\\|\/)(?<seriesname>[^\\\/]*)[sS](?<seasonnumber>\d{1,4})[xX\.]?[eE](?<epnumber>\d{1,3})(-[xX]?[eE]?(?<endingepnumber>\d{1,3}))+[^\\\/]*$" ,
RegexOptions . Compiled )
} ;
/// <summary>
/// To avoid the following matching movies they are only valid when contained in a folder which has been matched as a being season, or the media type is TV series
/// </summary>
private static readonly Regex [ ] EpisodeExpressionsWithoutSeason =
{
new Regex (
@".*[\\\/](?<epnumber>\d{1,3})(-(?<endingepnumber>\d{2,3}))*\.\w+$" ,
RegexOptions . Compiled ) ,
// "01.avi"
new Regex (
@".*(\\|\/)(?<epnumber>\d{1,3})(-(?<endingepnumber>\d{2,3}))*\s?-\s?[^\\\/]*$" ,
RegexOptions . Compiled ) ,
// "01 - blah.avi", "01-blah.avi"
new Regex (
@".*(\\|\/)(?<epnumber>\d{1,3})(-(?<endingepnumber>\d{2,3}))*\.[^\\\/]+$" ,
RegexOptions . Compiled ) ,
// "01.blah.avi"
new Regex (
@".*[\\\/][^\\\/]* - (?<epnumber>\d{1,3})(-(?<endingepnumber>\d{2,3}))*[^\\\/]*$" ,
RegexOptions . Compiled ) ,
// "blah - 01.avi", "blah 2 - 01.avi", "blah - 01 blah.avi", "blah 2 - 01 blah", "blah - 01 - blah.avi", "blah 2 - 01 - blah"
} ;
2014-12-15 05:16:23 +00:00
public static int? GetSeasonNumberFromPath ( string path )
{
return GetSeasonNumberFromPath ( path , CollectionType . TvShows ) ;
}
2014-11-16 20:44:08 +00:00
/// <summary>
/// Gets the season number from path.
/// </summary>
/// <param name="path">The path.</param>
2014-12-15 05:16:23 +00:00
/// <param name="collectionType">Type of the collection.</param>
2014-11-16 20:44:08 +00:00
/// <returns>System.Nullable{System.Int32}.</returns>
2014-12-15 05:16:23 +00:00
public static int? GetSeasonNumberFromPath ( string path , string collectionType )
2014-11-16 20:44:08 +00:00
{
var filename = Path . GetFileName ( path ) ;
2014-12-15 05:16:23 +00:00
if ( string . Equals ( collectionType , CollectionType . TvShows , StringComparison . OrdinalIgnoreCase ) )
2014-11-16 20:44:08 +00:00
{
2014-12-15 05:16:23 +00:00
if ( string . Equals ( filename , "specials" , StringComparison . OrdinalIgnoreCase ) )
{
return 0 ;
}
2014-11-16 20:44:08 +00:00
}
int val ;
if ( int . TryParse ( filename , NumberStyles . Integer , CultureInfo . InvariantCulture , out val ) )
{
return val ;
}
if ( filename . StartsWith ( "s" , StringComparison . OrdinalIgnoreCase ) )
{
var testFilename = filename . Substring ( 1 ) ;
if ( int . TryParse ( testFilename , NumberStyles . Integer , CultureInfo . InvariantCulture , out val ) )
{
return val ;
}
}
// Look for one of the season folder names
foreach ( var name in SeasonFolderNames )
{
var index = filename . IndexOf ( name , StringComparison . OrdinalIgnoreCase ) ;
if ( index ! = - 1 )
{
return GetSeasonNumberFromPathSubstring ( filename . Substring ( index + name . Length ) ) ;
}
}
return null ;
}
/// <summary>
/// Extracts the season number from the second half of the Season folder name (everything after "Season", or "Staffel")
/// </summary>
/// <param name="path">The path.</param>
/// <returns>System.Nullable{System.Int32}.</returns>
private static int? GetSeasonNumberFromPathSubstring ( string path )
{
var numericStart = - 1 ;
var length = 0 ;
// Find out where the numbers start, and then keep going until they end
for ( var i = 0 ; i < path . Length ; i + + )
{
if ( char . IsNumber ( path , i ) )
{
if ( numericStart = = - 1 )
{
numericStart = i ;
}
length + + ;
}
else if ( numericStart ! = - 1 )
{
break ;
}
}
if ( numericStart = = - 1 )
{
return null ;
}
return int . Parse ( path . Substring ( numericStart , length ) , CultureInfo . InvariantCulture ) ;
}
/// <summary>
/// Episodes the number from file.
/// </summary>
/// <param name="fullPath">The full path.</param>
/// <param name="considerSeasonlessNames">if set to <c>true</c> [is in season].</param>
/// <returns>System.String.</returns>
public static int? GetEpisodeNumberFromFile ( string fullPath , bool considerSeasonlessNames )
{
string fl = fullPath . ToLower ( ) ;
foreach ( var r in EpisodeExpressions )
{
Match m = r . Match ( fl ) ;
if ( m . Success )
return ParseEpisodeNumber ( m . Groups [ "epnumber" ] . Value ) ;
}
if ( considerSeasonlessNames )
{
var match = EpisodeExpressionsWithoutSeason . Select ( r = > r . Match ( fl ) )
. FirstOrDefault ( m = > m . Success ) ;
if ( match ! = null )
{
return ParseEpisodeNumber ( match . Groups [ "epnumber" ] . Value ) ;
}
}
return null ;
}
public static int? GetEndingEpisodeNumberFromFile ( string fullPath )
{
var fl = fullPath . ToLower ( ) ;
foreach ( var r in MultipleEpisodeExpressions )
{
var m = r . Match ( fl ) ;
if ( m . Success & & ! string . IsNullOrEmpty ( m . Groups [ "endingepnumber" ] . Value ) )
return ParseEpisodeNumber ( m . Groups [ "endingepnumber" ] . Value ) ;
}
foreach ( var r in EpisodeExpressionsWithoutSeason )
{
var m = r . Match ( fl ) ;
if ( m . Success & & ! string . IsNullOrEmpty ( m . Groups [ "endingepnumber" ] . Value ) )
return ParseEpisodeNumber ( m . Groups [ "endingepnumber" ] . Value ) ;
}
return null ;
}
/// <summary>
/// Seasons the number from episode file.
/// </summary>
/// <param name="fullPath">The full path.</param>
/// <returns>System.String.</returns>
public static int? GetSeasonNumberFromEpisodeFile ( string fullPath )
{
string fl = fullPath . ToLower ( ) ;
foreach ( var r in EpisodeExpressions )
{
Match m = r . Match ( fl ) ;
if ( m . Success )
{
Group g = m . Groups [ "seasonnumber" ] ;
if ( g ! = null )
{
var val = g . Value ;
if ( ! string . IsNullOrWhiteSpace ( val ) )
{
int num ;
if ( int . TryParse ( val , NumberStyles . Integer , UsCulture , out num ) )
{
return num ;
}
}
}
return null ;
}
}
return null ;
}
public static string GetSeriesNameFromEpisodeFile ( string fullPath )
{
var fl = fullPath . ToLower ( ) ;
foreach ( var r in EpisodeExpressions )
{
var m = r . Match ( fl ) ;
if ( m . Success )
{
var g = m . Groups [ "seriesname" ] ;
if ( g ! = null )
{
var val = g . Value ;
if ( ! string . IsNullOrWhiteSpace ( val ) )
{
return val ;
}
}
return null ;
}
}
return null ;
}
private static readonly CultureInfo UsCulture = new CultureInfo ( "en-US" ) ;
private static int? ParseEpisodeNumber ( string val )
{
int num ;
if ( ! string . IsNullOrEmpty ( val ) & & int . TryParse ( val , NumberStyles . Integer , UsCulture , out num ) )
{
return num ;
}
return null ;
}
2013-02-21 01:33:05 +00:00
/// <summary>
/// Sets the initial item values.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="args">The args.</param>
protected override void SetInitialItemValues ( Series item , ItemResolveArgs args )
{
base . SetInitialItemValues ( item , args ) ;
2013-03-03 06:58:04 +00:00
SetProviderIdFromPath ( item , args . Path ) ;
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Sets the provider id from path.
/// </summary>
/// <param name="item">The item.</param>
2013-04-10 16:04:13 +00:00
/// <param name="path">The path.</param>
2013-03-03 06:58:04 +00:00
private void SetProviderIdFromPath ( Series item , string path )
2013-02-21 01:33:05 +00:00
{
2013-03-03 06:58:04 +00:00
var justName = Path . GetFileName ( path ) ;
2013-02-21 01:33:05 +00:00
var id = justName . GetAttributeValue ( "tvdbid" ) ;
if ( ! string . IsNullOrEmpty ( id ) )
{
item . SetProviderId ( MetadataProviders . Tvdb , id ) ;
}
}
}
}