jellyfin/MediaBrowser.Server.Implementations/Library/Resolvers/TV/SeriesResolver.cs

197 lines
7.0 KiB
C#
Raw Normal View History

2014-12-04 05:24:41 +00:00
using MediaBrowser.Common.IO;
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;
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;
2014-12-24 06:28:40 +00:00
using MediaBrowser.Naming.Common;
using MediaBrowser.Naming.TV;
2013-02-21 01:33:05 +00:00
using System;
2014-11-16 20:44:08 +00:00
using System.Collections.Generic;
2013-02-21 01:33:05 +00:00
using System.IO;
namespace MediaBrowser.Server.Implementations.Library.Resolvers.TV
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Class SeriesResolver
/// </summary>
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
2014-12-20 06:06:27 +00:00
if (args.Parent.IsRoot)
2013-02-21 01:33:05 +00:00
{
return null;
}
var collectionType = args.GetCollectionType();
// If there's a collection type and it's not tv, it can't be a series
2014-12-24 06:28:40 +00:00
if (!string.Equals(collectionType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase))
2014-12-20 06:06:27 +00:00
{
return null;
}
if (args.HasParent<Series>() || args.HasParent<Season>())
{
return null;
}
2014-07-26 17:30:15 +00:00
2014-12-24 06:28:40 +00:00
if (IsSeriesFolder(args.Path, 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>
/// <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-24 06:28:40 +00:00
public static bool IsSeriesFolder(string path, 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-24 06:28:40 +00:00
if (IsSeasonFolder(child.FullName))
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-24 06:28:40 +00:00
return true;
2014-11-16 20:44:08 +00:00
}
}
}
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>
/// <returns><c>true</c> if [is season folder] [the specified path]; otherwise, <c>false</c>.</returns>
2014-12-24 06:28:40 +00:00
private static bool IsSeasonFolder(string path)
2014-11-16 20:44:08 +00:00
{
2014-12-24 06:28:40 +00:00
var seasonNumber = new SeasonPathParser(new ExtendedNamingOptions(), new RegexProvider()).Parse(path, true).SeasonNumber;
2014-11-16 20:44:08 +00:00
2014-12-19 04:20:07 +00:00
return seasonNumber.HasValue;
2014-11-16 20:44:08 +00:00
}
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);
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>
/// <param name="path">The path.</param>
private void SetProviderIdFromPath(Series item, string path)
2013-02-21 01:33:05 +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);
}
}
}
}