2013-06-02 00:35:19 +02:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Resolvers;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2013-05-21 05:16:43 +02:00
|
|
|
|
using System.Globalization;
|
2013-05-17 17:29:22 +02:00
|
|
|
|
using System.IO;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
using System.Linq;
|
2013-02-22 02:26:35 +01:00
|
|
|
|
using System.Text.RegularExpressions;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-03-03 07:58:04 +01:00
|
|
|
|
namespace MediaBrowser.Controller.Library
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-02-21 21:26:35 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class TVUtils
|
|
|
|
|
/// </summary>
|
2013-02-21 02:33:05 +01:00
|
|
|
|
public static class TVUtils
|
|
|
|
|
{
|
2013-02-21 21:26:35 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The TVDB API key
|
|
|
|
|
/// </summary>
|
2013-05-17 17:29:22 +02:00
|
|
|
|
public static readonly string TvdbApiKey = "B89CE93890E9419B";
|
2013-02-21 21:26:35 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The banner URL
|
|
|
|
|
/// </summary>
|
2013-02-21 02:33:05 +01: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>
|
|
|
|
|
private static readonly string[] SeasonFolderNames = new[]
|
|
|
|
|
{
|
|
|
|
|
"season",
|
|
|
|
|
"sæson",
|
|
|
|
|
"temporada",
|
|
|
|
|
"saison",
|
|
|
|
|
"staffel"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// <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[]
|
|
|
|
|
{
|
|
|
|
|
new Regex(
|
|
|
|
|
@".*\\[s|S]?(?<seasonnumber>\d{1,2})[x|X](?<epnumber>\d{1,3})[^\\]*$",
|
|
|
|
|
RegexOptions.Compiled),
|
|
|
|
|
new Regex(
|
2013-05-24 04:05:31 +02:00
|
|
|
|
@".*\\[s|S](?<seasonnumber>\d{1,2})[x,X]?[e|E](?<epnumber>\d{1,3})[^\\]*$",
|
2013-02-21 02:33:05 +01:00
|
|
|
|
RegexOptions.Compiled),
|
|
|
|
|
new Regex(
|
2013-05-24 04:05:31 +02:00
|
|
|
|
@".*\\(?<seriesname>((?![s|S]?\d{1,2}[x|X]\d{1,3})[^\\])*)?([s|S]?(?<seasonnumber>\d{1,2})[x|X](?<epnumber>\d{1,3}))[^\\]*$",
|
2013-02-21 02:33:05 +01:00
|
|
|
|
RegexOptions.Compiled),
|
|
|
|
|
new Regex(
|
|
|
|
|
@".*\\(?<seriesname>[^\\]*)[s|S](?<seasonnumber>\d{1,2})[x|X|\.]?[e|E](?<epnumber>\d{1,3})[^\\]*$",
|
|
|
|
|
RegexOptions.Compiled)
|
2013-05-24 04:05:31 +02:00
|
|
|
|
};
|
|
|
|
|
private static readonly Regex[] MultipleEpisodeExpressions = new[]
|
|
|
|
|
{
|
|
|
|
|
new Regex(
|
|
|
|
|
@".*\\[s|S]?(?<seasonnumber>\d{1,2})[x|X](?<epnumber>\d{1,3})([ |-]{1,3}\d{1,2}[e|E|x|X](?<endingepnumber>\d{1,3}))+[^\\]*$",
|
|
|
|
|
RegexOptions.Compiled),
|
|
|
|
|
new Regex(
|
|
|
|
|
@".*\\[s|S]?(?<seasonnumber>\d{1,2})[x|X](?<epnumber>\d{1,3})([ |-]{1,3}\d{1,2}[x|X][e|E](?<endingepnumber>\d{1,3}))+[^\\]*$",
|
|
|
|
|
RegexOptions.Compiled),
|
|
|
|
|
new Regex(
|
|
|
|
|
@".*\\[s|S]?(?<seasonnumber>\d{1,2})[x|X](?<epnumber>\d{1,3})([ |-]{0,3}[x|X|e|E](?<endingepnumber>\d{1,3}))+[^\\]*$",
|
|
|
|
|
RegexOptions.Compiled),
|
|
|
|
|
new Regex(
|
|
|
|
|
@".*\\[s|S]?(?<seasonnumber>\d{1,2})[x|X](?<epnumber>\d{1,3})([ |-]{1,3}[x|E]?[e|E]?(?<endingepnumber>\d{1,3}))+[^\\]*$",
|
|
|
|
|
RegexOptions.Compiled),
|
|
|
|
|
new Regex(
|
|
|
|
|
@".*\\(?<seriesname>((?![s|S]?\d{1,2}[x|X]\d{1,3})[^\\])*)?([s|S]?(?<seasonnumber>\d{1,2})[x|X](?<epnumber>\d{1,3}))([ |-]{1,3}\d{1,2}[x|X|e|E](?<endingepnumber>\d{1,3}))+[^\\]*$",
|
|
|
|
|
RegexOptions.Compiled),
|
|
|
|
|
new Regex(
|
|
|
|
|
@".*\\(?<seriesname>((?![s|S]?\d{1,2}[x|X]\d{1,3})[^\\])*)?([s|S]?(?<seasonnumber>\d{1,2})[x|X](?<epnumber>\d{1,3}))([ |-]{1,3}\d{1,2}[x|X][e|E](?<endingepnumber>\d{1,3}))+[^\\]*$",
|
|
|
|
|
RegexOptions.Compiled),
|
|
|
|
|
new Regex(
|
|
|
|
|
@".*\\(?<seriesname>((?![s|S]?\d{1,2}[x|X]\d{1,3})[^\\])*)?([s|S]?(?<seasonnumber>\d{1,2})[x|X](?<epnumber>\d{1,3}))([ |-]{0,3}[x|X|e|E](?<endingepnumber>\d{1,3}))+[^\\]*$",
|
|
|
|
|
RegexOptions.Compiled),
|
|
|
|
|
new Regex(
|
|
|
|
|
@".*\\(?<seriesname>((?![s|S]?\d{1,2}[x|X]\d{1,3})[^\\])*)?([s|S]?(?<seasonnumber>\d{1,2})[x|X](?<epnumber>\d{1,3}))([ |-]{1,3}[x|X]?[e|E]?(?<endingepnumber>\d{1,3}))+[^\\]*$",
|
|
|
|
|
RegexOptions.Compiled),
|
|
|
|
|
new Regex(
|
|
|
|
|
@".*\\(?<seriesname>[^\\]*)[s|S](?<seasonnumber>\d{1,2})[x|X|\.]?[e|E](?<epnumber>\d{1,3})([ |-]{0,3}[x|X|e|E](?<endingepnumber>\d{1,3}))+[^\\]*$",
|
|
|
|
|
RegexOptions.Compiled),
|
|
|
|
|
new Regex(
|
|
|
|
|
@".*\\(?<seriesname>[^\\]*)[s|S](?<seasonnumber>\d{1,2})[x|X|\.]?[e|E](?<epnumber>\d{1,3})([ |-]{1,3}[x|X]?[e|E]?(?<endingepnumber>\d{1,3}))+[^\\]*$",
|
|
|
|
|
RegexOptions.Compiled)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// To avoid the following matching movies they are only valid when contained in a folder which has been matched as a being season
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static readonly Regex[] EpisodeExpressionsInASeasonFolder = new[]
|
|
|
|
|
{
|
|
|
|
|
new Regex(
|
|
|
|
|
@".*\\(?<epnumber>\d{1,2})\s?-\s?[^\\]*$",
|
|
|
|
|
RegexOptions.Compiled),
|
|
|
|
|
// 01 - blah.avi, 01-blah.avi
|
|
|
|
|
new Regex(
|
|
|
|
|
@".*\\(?<epnumber>\d{1,2})[^\d\\]*[^\\]*$",
|
|
|
|
|
RegexOptions.Compiled),
|
|
|
|
|
// 01.avi, 01.blah.avi "01 - 22 blah.avi"
|
|
|
|
|
new Regex(
|
|
|
|
|
@".*\\(?<seasonnumber>\d)(?<epnumber>\d{1,2})[^\d\\]+[^\\]*$",
|
|
|
|
|
RegexOptions.Compiled),
|
|
|
|
|
// 01.avi, 01.blah.avi
|
|
|
|
|
new Regex(
|
|
|
|
|
@".*\\\D*\d+(?<epnumber>\d{2})",
|
|
|
|
|
RegexOptions.Compiled)
|
|
|
|
|
// hell0 - 101 - hello.avi
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2013-02-21 21:26:35 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the season number from path.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <returns>System.Nullable{System.Int32}.</returns>
|
2013-02-21 02:33:05 +01:00
|
|
|
|
public static int? GetSeasonNumberFromPath(string path)
|
|
|
|
|
{
|
2013-06-07 18:07:16 +02:00
|
|
|
|
var filename = Path.GetFileName(path);
|
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
// Look for one of the season folder names
|
|
|
|
|
foreach (var name in SeasonFolderNames)
|
|
|
|
|
{
|
2013-06-07 18:07:16 +02:00
|
|
|
|
var index = filename.IndexOf(name, StringComparison.OrdinalIgnoreCase);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
if (index != -1)
|
|
|
|
|
{
|
2013-06-09 21:58:51 +02:00
|
|
|
|
return GetSeasonNumberFromPathSubstring(filename.Substring(index + name.Length));
|
2013-02-21 02:33:05 +01: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 21:26:35 +01:00
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <returns>System.Nullable{System.Int32}.</returns>
|
2013-02-21 02:33:05 +01:00
|
|
|
|
private static int? GetSeasonNumberFromPathSubstring(string path)
|
|
|
|
|
{
|
|
|
|
|
int numericStart = -1;
|
|
|
|
|
int length = 0;
|
|
|
|
|
|
|
|
|
|
// Find out where the numbers start, and then keep going until they end
|
2013-05-17 17:29:22 +02:00
|
|
|
|
for (var i = 0; i < path.Length; i++)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 21:26:35 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines whether [is season folder] [the specified path].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <returns><c>true</c> if [is season folder] [the specified path]; otherwise, <c>false</c>.</returns>
|
2013-04-28 16:18:17 +02:00
|
|
|
|
private static bool IsSeasonFolder(string path)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-06-02 00:35:19 +02:00
|
|
|
|
// It's a season folder if it's named as such and does not contain any audio files, apart from theme.mp3
|
|
|
|
|
return GetSeasonNumberFromPath(path) != null && !new DirectoryInfo(path).EnumerateFiles().Any(i => EntityResolutionHelper.IsAudioFile(i.FullName) && !string.Equals(Path.GetFileNameWithoutExtension(i.Name), BaseItem.ThemeSongFilename));
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 21:26:35 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines whether [is series folder] [the specified path].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <param name="fileSystemChildren">The file system children.</param>
|
|
|
|
|
/// <returns><c>true</c> if [is series folder] [the specified path]; otherwise, <c>false</c>.</returns>
|
2013-04-28 07:29:27 +02:00
|
|
|
|
public static bool IsSeriesFolder(string path, IEnumerable<FileSystemInfo> fileSystemChildren)
|
2013-02-21 02:33:05 +01: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 16:18:17 +02:00
|
|
|
|
var attributes = child.Attributes;
|
|
|
|
|
|
2013-06-11 22:35:54 +02:00
|
|
|
|
if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-11 22:35:54 +02:00
|
|
|
|
if ((attributes & FileAttributes.System) == FileAttributes.System)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((attributes & FileAttributes.Directory) == FileAttributes.Directory)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-04-28 07:29:27 +02:00
|
|
|
|
if (IsSeasonFolder(child.FullName))
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nonSeriesFolders++;
|
|
|
|
|
|
|
|
|
|
if (nonSeriesFolders >= 3)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-05-19 19:05:33 +02:00
|
|
|
|
if (EntityResolutionHelper.IsVideoFile(child.FullName) && GetEpisodeNumberFromFile(child.FullName, false).HasValue)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 21:26:35 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Episodes the number from file.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="fullPath">The full path.</param>
|
|
|
|
|
/// <param name="isInSeason">if set to <c>true</c> [is in season].</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
2013-05-19 19:05:33 +02:00
|
|
|
|
public static int? GetEpisodeNumberFromFile(string fullPath, bool isInSeason)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
string fl = fullPath.ToLower();
|
|
|
|
|
foreach (var r in EpisodeExpressions)
|
|
|
|
|
{
|
|
|
|
|
Match m = r.Match(fl);
|
|
|
|
|
if (m.Success)
|
2013-05-19 19:05:33 +02:00
|
|
|
|
return ParseEpisodeNumber(m.Groups["epnumber"].Value);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
if (isInSeason)
|
|
|
|
|
{
|
|
|
|
|
var match = EpisodeExpressionsInASeasonFolder.Select(r => r.Match(fl))
|
|
|
|
|
.FirstOrDefault(m => m.Success);
|
|
|
|
|
|
|
|
|
|
if (match != null)
|
|
|
|
|
{
|
2013-05-19 19:05:33 +02:00
|
|
|
|
return ParseEpisodeNumber(match.Value);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-19 19:05:33 +02:00
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-24 04:05:31 +02: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);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-19 19:05:33 +02:00
|
|
|
|
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;
|
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 21:26:35 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Seasons the number from episode file.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="fullPath">The full path.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
2013-05-21 05:16:43 +02:00
|
|
|
|
public static int? GetSeasonNumberFromEpisodeFile(string fullPath)
|
2013-02-21 02:33:05 +01: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 05:16:43 +02: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 02:33:05 +01:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 21:26:35 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the air days.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="day">The day.</param>
|
|
|
|
|
/// <returns>List{DayOfWeek}.</returns>
|
2013-02-21 02:33:05 +01: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 17:56:36 +02:00
|
|
|
|
return new List<DayOfWeek>();
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|