2014-07-26 17:30:15 +00:00
using MediaBrowser.Common.IO ;
using MediaBrowser.Controller.Entities ;
2014-02-14 04:00:13 +00:00
using MediaBrowser.Controller.Providers ;
2013-06-01 22:35:19 +00:00
using MediaBrowser.Controller.Resolvers ;
2013-02-21 01:33:05 +00:00
using System ;
using System.Collections.Generic ;
2013-05-21 03:16:43 +00:00
using System.Globalization ;
2013-05-17 15:29:22 +00:00
using System.IO ;
2013-02-21 01:33:05 +00:00
using System.Linq ;
2013-02-22 01:26:35 +00:00
using System.Text.RegularExpressions ;
2014-07-30 03:31:35 +00:00
using MediaBrowser.Model.Logging ;
2013-02-21 01:33:05 +00:00
2013-03-03 06:58:04 +00:00
namespace MediaBrowser.Controller.Library
2013-02-21 01:33:05 +00:00
{
2013-02-21 20:26:35 +00:00
/// <summary>
/// Class TVUtils
/// </summary>
2013-02-21 01:33:05 +00:00
public static class TVUtils
{
2013-02-21 20:26:35 +00:00
/// <summary>
/// The TVDB API key
/// </summary>
2013-05-17 15:29:22 +00:00
public static readonly string TvdbApiKey = "B89CE93890E9419B" ;
2013-02-21 20:26:35 +00:00
/// <summary>
/// The banner URL
/// </summary>
2013-02-21 01:33:05 +00:00
public static readonly string BannerUrl = "http://www.thetvdb.com/banners/" ;
/// <summary>
/// A season folder must contain one of these somewhere in the name
/// </summary>
2014-03-12 19:56:12 +00:00
private static readonly string [ ] SeasonFolderNames =
{
"season" ,
"sæson" ,
"temporada" ,
"saison" ,
"staffel" ,
"series" ,
"сезон"
} ;
2013-02-21 01:33:05 +00:00
/// <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>
2014-03-12 19:56:12 +00:00
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 )
} ;
2013-02-21 01:33:05 +00:00
/// <summary>
2014-06-19 20:35:35 +00:00
/// 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
2013-02-21 01:33:05 +00:00
/// </summary>
2014-06-19 20:35:35 +00:00
private static readonly Regex [ ] EpisodeExpressionsWithoutSeason =
2014-03-12 19:56:12 +00:00
{
new Regex (
2014-07-03 04:01:36 +00:00
@".*[\\\/](?<epnumber>\d{1,3})(-(?<endingepnumber>\d{2,3}))*\.\w+$" ,
2014-03-12 19:56:12 +00:00
RegexOptions . Compiled ) ,
2014-06-19 20:35:35 +00:00
// "01.avi"
2014-03-12 19:56:12 +00:00
new Regex (
2014-07-03 04:01:36 +00:00
@".*(\\|\/)(?<epnumber>\d{1,3})(-(?<endingepnumber>\d{2,3}))*\s?-\s?[^\\\/]*$" ,
2014-03-12 19:56:12 +00:00
RegexOptions . Compiled ) ,
2014-06-19 20:35:35 +00:00
// "01 - blah.avi", "01-blah.avi"
new Regex (
2014-07-03 04:01:36 +00:00
@".*(\\|\/)(?<epnumber>\d{1,3})(-(?<endingepnumber>\d{2,3}))*\.[^\\\/]+$" ,
2014-03-12 19:56:12 +00:00
RegexOptions . Compiled ) ,
2014-06-19 20:35:35 +00:00
// "01.blah.avi"
2014-03-12 19:56:12 +00:00
new Regex (
2014-07-03 04:01:36 +00:00
@".*[\\\/][^\\\/]* - (?<epnumber>\d{1,3})(-(?<endingepnumber>\d{2,3}))*[^\\\/]*$" ,
2014-06-19 20:35:35 +00:00
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-03-12 19:56:12 +00:00
} ;
2013-02-21 01:33:05 +00:00
2013-02-21 20:26:35 +00:00
/// <summary>
/// Gets the season number from path.
/// </summary>
/// <param name="path">The path.</param>
/// <returns>System.Nullable{System.Int32}.</returns>
2013-02-21 01:33:05 +00:00
public static int? GetSeasonNumberFromPath ( string path )
{
2013-06-07 16:07:16 +00:00
var filename = Path . GetFileName ( path ) ;
2014-08-05 23:59:24 +00:00
if ( string . Equals ( filename , "specials" , StringComparison . OrdinalIgnoreCase ) )
2013-12-22 18:58:51 +00:00
{
return 0 ;
}
2014-07-05 01:22:26 +00:00
int val ;
if ( int . TryParse ( filename , NumberStyles . Integer , CultureInfo . InvariantCulture , out val ) )
{
return val ;
}
2014-07-30 03:31:35 +00:00
if ( filename . StartsWith ( "s" , StringComparison . OrdinalIgnoreCase ) )
{
var testFilename = filename . Substring ( 1 ) ;
if ( int . TryParse ( testFilename , NumberStyles . Integer , CultureInfo . InvariantCulture , out val ) )
{
return val ;
}
}
2013-02-21 01:33:05 +00:00
// Look for one of the season folder names
foreach ( var name in SeasonFolderNames )
{
2013-06-07 16:07:16 +00:00
var index = filename . IndexOf ( name , StringComparison . OrdinalIgnoreCase ) ;
2013-02-21 01:33:05 +00:00
if ( index ! = - 1 )
{
2013-06-09 19:58:51 +00:00
return GetSeasonNumberFromPathSubstring ( filename . Substring ( index + name . Length ) ) ;
2013-02-21 01:33:05 +00:00
}
}
return null ;
}
/// <summary>
/// Extracts the season number from the second half of the Season folder name (everything after "Season", or "Staffel")
/// </summary>
2013-02-21 20:26:35 +00:00
/// <param name="path">The path.</param>
/// <returns>System.Nullable{System.Int32}.</returns>
2013-02-21 01:33:05 +00:00
private static int? GetSeasonNumberFromPathSubstring ( string path )
{
2014-03-12 19:56:12 +00:00
var numericStart = - 1 ;
var length = 0 ;
2013-02-21 01:33:05 +00:00
// Find out where the numbers start, and then keep going until they end
2013-05-17 15:29:22 +00:00
for ( var i = 0 ; i < path . Length ; i + + )
2013-02-21 01:33:05 +00:00
{
if ( char . IsNumber ( path , i ) )
{
if ( numericStart = = - 1 )
{
numericStart = i ;
}
length + + ;
}
else if ( numericStart ! = - 1 )
{
break ;
}
}
if ( numericStart = = - 1 )
{
return null ;
}
2014-07-30 03:31:35 +00:00
return int . Parse ( path . Substring ( numericStart , length ) , CultureInfo . InvariantCulture ) ;
2013-02-21 01:33:05 +00:00
}
2013-02-21 20:26:35 +00:00
/// <summary>
/// Determines whether [is season folder] [the specified path].
/// </summary>
/// <param name="path">The path.</param>
2014-02-14 04:00:13 +00:00
/// <param name="directoryService">The directory service.</param>
2014-07-26 17:30:15 +00:00
/// <param name="fileSystem">The file system.</param>
2013-02-21 20:26:35 +00:00
/// <returns><c>true</c> if [is season folder] [the specified path]; otherwise, <c>false</c>.</returns>
2014-07-26 17:30:15 +00:00
private static bool IsSeasonFolder ( string path , IDirectoryService directoryService , IFileSystem fileSystem )
2013-02-21 01:33:05 +00:00
{
2014-07-30 03:31:35 +00:00
var seasonNumber = GetSeasonNumberFromPath ( path ) ;
var hasSeasonNumber = seasonNumber ! = null ;
if ( ! hasSeasonNumber )
{
return false ;
}
2013-06-01 22:35:19 +00:00
// It's a season folder if it's named as such and does not contain any audio files, apart from theme.mp3
2014-07-30 03:31:35 +00:00
foreach ( var fileSystemInfo in directoryService . GetFileSystemEntries ( path ) )
{
var attributes = fileSystemInfo . Attributes ;
if ( ( attributes & FileAttributes . Hidden ) = = FileAttributes . Hidden )
{
continue ;
}
2014-07-31 02:09:23 +00:00
// Can't enforce this because files saved by Bitcasa are always marked System
//if ((attributes & FileAttributes.System) == FileAttributes.System)
//{
// continue;
//}
2014-07-30 03:31:35 +00:00
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 ;
2013-02-21 01:33:05 +00:00
}
2013-02-21 20:26:35 +00:00
/// <summary>
/// Determines whether [is series folder] [the specified path].
/// </summary>
/// <param name="path">The path.</param>
2014-07-30 03:31:35 +00:00
/// <param name="considerSeasonlessEntries">if set to <c>true</c> [consider seasonless entries].</param>
2013-02-21 20:26:35 +00:00
/// <param name="fileSystemChildren">The file system children.</param>
2014-07-20 04:46:29 +00:00
/// <param name="directoryService">The directory service.</param>
2014-07-30 03:31:35 +00:00
/// <param name="fileSystem">The file system.</param>
2013-02-21 20:26:35 +00:00
/// <returns><c>true</c> if [is series folder] [the specified path]; otherwise, <c>false</c>.</returns>
2014-07-30 03:31:35 +00:00
public static bool IsSeriesFolder ( string path , bool considerSeasonlessEntries , IEnumerable < FileSystemInfo > fileSystemChildren , IDirectoryService directoryService , IFileSystem fileSystem , ILogger logger )
2013-02-21 01:33:05 +00:00
{
// A folder with more than 3 non-season folders in will not becounted as a series
var nonSeriesFolders = 0 ;
foreach ( var child in fileSystemChildren )
{
2013-04-28 14:18:17 +00:00
var attributes = child . Attributes ;
2013-06-11 20:35:54 +00:00
if ( ( attributes & FileAttributes . Hidden ) = = FileAttributes . Hidden )
2013-02-21 01:33:05 +00:00
{
2014-08-10 22:13:17 +00:00
//logger.Debug("Igoring series file or folder marked hidden: {0}", child.FullName);
2013-02-21 01:33:05 +00:00
continue ;
}
2014-07-31 02:09:23 +00:00
// 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;
//}
2013-06-11 20:35:54 +00:00
if ( ( attributes & FileAttributes . Directory ) = = FileAttributes . Directory )
2013-02-21 01:33:05 +00:00
{
2014-07-26 17:30:15 +00:00
if ( IsSeasonFolder ( child . FullName , directoryService , fileSystem ) )
2013-02-21 01:33:05 +00:00
{
2014-07-30 03:31:35 +00:00
logger . Debug ( "{0} is a series because of season folder {1}." , path , child . FullName ) ;
2013-02-21 01:33:05 +00:00
return true ;
}
2014-07-30 03:31:35 +00:00
if ( IsBadFolder ( child . Name ) )
2014-07-20 04:46:29 +00:00
{
2014-07-30 03:31:35 +00:00
logger . Debug ( "Invalid folder under series: {0}" , child . FullName ) ;
2014-07-20 04:46:29 +00:00
nonSeriesFolders + + ;
}
2013-02-21 01:33:05 +00:00
if ( nonSeriesFolders > = 3 )
{
2014-07-30 03:31:35 +00:00
logger . Debug ( "{0} not a series due to 3 or more invalid folders." , path ) ;
2013-02-21 01:33:05 +00:00
return false ;
}
}
else
{
2013-06-20 02:21:50 +00:00
var fullName = child . FullName ;
2014-03-03 05:11:03 +00:00
if ( EntityResolutionHelper . IsVideoFile ( fullName ) | | EntityResolutionHelper . IsVideoPlaceHolder ( fullName ) )
2013-02-21 01:33:05 +00:00
{
2014-07-30 03:31:35 +00:00
if ( GetEpisodeNumberFromFile ( fullName , considerSeasonlessEntries ) . HasValue )
2014-03-03 05:11:03 +00:00
{
return true ;
}
2013-02-21 01:33:05 +00:00
}
}
}
2014-07-30 03:31:35 +00:00
logger . Debug ( "{0} is not a series folder." , path ) ;
2013-02-21 01:33:05 +00:00
return false ;
}
2014-07-30 03:31:35 +00:00
private static bool IsBadFolder ( string name )
{
if ( string . Equals ( name , BaseItem . ThemeSongsFolderName , StringComparison . OrdinalIgnoreCase ) )
{
return false ;
}
if ( string . Equals ( name , BaseItem . ThemeVideosFolderName , StringComparison . OrdinalIgnoreCase ) )
{
return false ;
}
if ( string . Equals ( name , BaseItem . TrailerFolderName , StringComparison . OrdinalIgnoreCase ) )
{
return false ;
}
return ! EntityResolutionHelper . IgnoreFolders . Contains ( name , StringComparer . OrdinalIgnoreCase ) ;
}
2013-02-21 20:26:35 +00:00
/// <summary>
/// Episodes the number from file.
/// </summary>
/// <param name="fullPath">The full path.</param>
2014-06-19 20:35:35 +00:00
/// <param name="considerSeasonlessNames">if set to <c>true</c> [is in season].</param>
2013-02-21 20:26:35 +00:00
/// <returns>System.String.</returns>
2014-06-19 20:35:35 +00:00
public static int? GetEpisodeNumberFromFile ( string fullPath , bool considerSeasonlessNames )
2013-02-21 01:33:05 +00:00
{
string fl = fullPath . ToLower ( ) ;
foreach ( var r in EpisodeExpressions )
{
Match m = r . Match ( fl ) ;
if ( m . Success )
2013-05-19 17:05:33 +00:00
return ParseEpisodeNumber ( m . Groups [ "epnumber" ] . Value ) ;
2013-02-21 01:33:05 +00:00
}
2014-06-19 20:35:35 +00:00
if ( considerSeasonlessNames )
2013-02-21 01:33:05 +00:00
{
2014-06-19 20:35:35 +00:00
var match = EpisodeExpressionsWithoutSeason . Select ( r = > r . Match ( fl ) )
2013-02-21 01:33:05 +00:00
. FirstOrDefault ( m = > m . Success ) ;
if ( match ! = null )
{
2013-11-26 03:16:44 +00:00
return ParseEpisodeNumber ( match . Groups [ "epnumber" ] . Value ) ;
2013-02-21 01:33:05 +00:00
}
}
2013-05-19 17:05:33 +00:00
return null ;
}
2013-05-24 02:05:31 +00:00
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 ) ;
}
2014-07-03 04:01:36 +00:00
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 ) ;
}
2013-05-24 02:05:31 +00:00
return null ;
}
2013-05-19 17:05:33 +00:00
private static readonly CultureInfo UsCulture = new CultureInfo ( "en-US" ) ;
2013-06-20 02:21:50 +00:00
2013-05-19 17:05:33 +00:00
private static int? ParseEpisodeNumber ( string val )
{
int num ;
if ( ! string . IsNullOrEmpty ( val ) & & int . TryParse ( val , NumberStyles . Integer , UsCulture , out num ) )
{
return num ;
}
2013-02-21 01:33:05 +00:00
return null ;
}
2013-02-21 20:26:35 +00:00
/// <summary>
/// Seasons the number from episode file.
/// </summary>
/// <param name="fullPath">The full path.</param>
/// <returns>System.String.</returns>
2013-05-21 03:16:43 +00:00
public static int? GetSeasonNumberFromEpisodeFile ( string fullPath )
2013-02-21 01:33:05 +00:00
{
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 )
2013-05-21 03:16:43 +00:00
{
var val = g . Value ;
if ( ! string . IsNullOrWhiteSpace ( val ) )
{
int num ;
if ( int . TryParse ( val , NumberStyles . Integer , UsCulture , out num ) )
{
return num ;
}
}
}
2013-02-21 01:33:05 +00:00
return null ;
}
}
return null ;
}
2014-01-19 06:36:21 +00:00
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 ;
}
2013-02-21 20:26:35 +00:00
/// <summary>
/// Gets the air days.
/// </summary>
/// <param name="day">The day.</param>
/// <returns>List{DayOfWeek}.</returns>
2013-02-21 01:33:05 +00:00
public static List < DayOfWeek > GetAirDays ( string day )
{
if ( ! string . IsNullOrWhiteSpace ( day ) )
{
if ( day . Equals ( "Daily" , StringComparison . OrdinalIgnoreCase ) )
{
return new List < DayOfWeek >
{
DayOfWeek . Sunday ,
DayOfWeek . Monday ,
DayOfWeek . Tuesday ,
DayOfWeek . Wednesday ,
DayOfWeek . Thursday ,
DayOfWeek . Friday ,
DayOfWeek . Saturday
} ;
}
DayOfWeek value ;
if ( Enum . TryParse ( day , true , out value ) )
{
return new List < DayOfWeek >
{
value
} ;
}
2013-04-10 15:56:36 +00:00
return new List < DayOfWeek > ( ) ;
2013-02-21 01:33:05 +00:00
}
return null ;
}
}
}