jellyfin-server/MediaBrowser.Api/UserLibrary/ItemsService.cs

621 lines
22 KiB
C#
Raw Normal View History

using MediaBrowser.Controller.Collections;
2014-04-22 17:25:54 +00:00
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
2013-04-24 14:05:47 +00:00
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.TV;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Controller.Library;
2013-04-14 00:17:25 +00:00
using MediaBrowser.Controller.Localization;
2014-07-02 18:34:08 +00:00
using MediaBrowser.Controller.Net;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Model.Entities;
2013-03-10 05:36:39 +00:00
using MediaBrowser.Model.Querying;
2013-12-07 15:52:38 +00:00
using ServiceStack;
2013-02-21 01:33:05 +00:00
using System;
using System.Collections.Generic;
2013-12-28 23:09:24 +00:00
using System.Globalization;
2013-02-21 01:33:05 +00:00
using System.Linq;
2014-10-06 23:58:46 +00:00
using System.Threading.Tasks;
2013-02-21 01:33:05 +00:00
namespace MediaBrowser.Api.UserLibrary
{
/// <summary>
/// Class GetItems
/// </summary>
2014-03-28 19:58:18 +00:00
[Route("/Items", "GET", Summary = "Gets items based on a query.")]
[Route("/Users/{UserId}/Items", "GET", Summary = "Gets items based on a query.")]
2013-03-11 05:06:55 +00:00
public class GetItems : BaseItemsRequest, IReturn<ItemsResult>
2013-02-21 01:33:05 +00:00
{
}
/// <summary>
/// Class ItemsService
/// </summary>
2014-07-02 18:34:08 +00:00
[Authenticated]
2013-03-16 05:52:33 +00:00
public class ItemsService : BaseApiService
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// The _user manager
/// </summary>
private readonly IUserManager _userManager;
2013-10-02 16:08:58 +00:00
private readonly IUserDataManager _userDataRepository;
/// <summary>
/// The _library manager
/// </summary>
private readonly ILibraryManager _libraryManager;
private readonly ILocalizationManager _localization;
2013-03-09 05:15:51 +00:00
2013-09-04 17:02:19 +00:00
private readonly IDtoService _dtoService;
2014-04-22 17:25:54 +00:00
private readonly ICollectionManager _collectionManager;
2013-09-04 17:02:19 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="ItemsService" /> class.
/// </summary>
/// <param name="userManager">The user manager.</param>
2013-03-28 22:00:58 +00:00
/// <param name="libraryManager">The library manager.</param>
/// <param name="userDataRepository">The user data repository.</param>
2014-04-22 17:25:54 +00:00
/// <param name="localization">The localization.</param>
/// <param name="dtoService">The dto service.</param>
/// <param name="collectionManager">The collection manager.</param>
public ItemsService(IUserManager userManager, ILibraryManager libraryManager, IUserDataManager userDataRepository, ILocalizationManager localization, IDtoService dtoService, ICollectionManager collectionManager)
{
_userManager = userManager;
_libraryManager = libraryManager;
_userDataRepository = userDataRepository;
2013-06-13 18:17:59 +00:00
_localization = localization;
2013-09-04 17:02:19 +00:00
_dtoService = dtoService;
2014-04-22 17:25:54 +00:00
_collectionManager = collectionManager;
}
2013-02-21 01:33:05 +00:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
2014-10-06 23:58:46 +00:00
public async Task<object> Get(GetItems request)
2013-02-21 01:33:05 +00:00
{
2014-10-06 23:58:46 +00:00
var result = await GetItems(request).ConfigureAwait(false);
2013-02-21 01:33:05 +00:00
return ToOptimizedSerializedResultUsingCache(result);
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Gets the items.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>Task{ItemsResult}.</returns>
2014-10-06 23:58:46 +00:00
private async Task<ItemsResult> GetItems(GetItems request)
2013-02-21 01:33:05 +00:00
{
2014-04-27 03:42:05 +00:00
var parentItem = string.IsNullOrEmpty(request.ParentId) ? null : _libraryManager.GetItemById(request.ParentId);
2015-05-29 23:51:33 +00:00
var user = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(request.UserId) : null;
2013-02-21 01:33:05 +00:00
2014-10-06 23:58:46 +00:00
var result = await GetItemsToSerialize(request, user, parentItem).ConfigureAwait(false);
2013-02-21 01:33:05 +00:00
2015-01-24 19:03:55 +00:00
var dtoOptions = GetDtoOptions(request);
2013-04-05 15:58:06 +00:00
2013-02-21 01:33:05 +00:00
return new ItemsResult
{
2015-07-14 19:04:16 +00:00
TotalRecordCount = result.Item1.TotalRecordCount,
Items = _dtoService.GetBaseItemDtos(result.Item1.Items, dtoOptions, user).ToArray()
2013-02-21 01:33:05 +00:00
};
}
/// <summary>
/// Gets the items to serialize.
/// </summary>
/// <param name="request">The request.</param>
/// <param name="user">The user.</param>
2014-04-27 03:42:05 +00:00
/// <param name="parentItem">The parent item.</param>
2013-02-21 01:33:05 +00:00
/// <returns>IEnumerable{BaseItem}.</returns>
2014-10-06 23:58:46 +00:00
private async Task<Tuple<QueryResult<BaseItem>, bool>> GetItemsToSerialize(GetItems request, User user, BaseItem parentItem)
2013-02-21 01:33:05 +00:00
{
2014-01-22 17:05:23 +00:00
var item = string.IsNullOrEmpty(request.ParentId) ?
2014-03-03 02:17:12 +00:00
user == null ? _libraryManager.RootFolder : user.RootFolder :
2014-04-27 03:42:05 +00:00
parentItem;
2013-02-21 01:33:05 +00:00
2015-11-15 22:30:47 +00:00
if (string.Equals(request.IncludeItemTypes, "Playlist", StringComparison.OrdinalIgnoreCase))
{
item = user == null ? _libraryManager.RootFolder : user.RootFolder;
}
2013-02-21 01:33:05 +00:00
// Default list type = children
2013-03-16 01:40:34 +00:00
if (!string.IsNullOrEmpty(request.Ids))
{
2015-07-14 19:04:16 +00:00
request.Recursive = true;
2015-08-19 04:08:03 +00:00
var query = GetItemsQuery(request, user);
var result = await ((Folder)item).GetItems(query).ConfigureAwait(false);
if (string.IsNullOrWhiteSpace(request.SortBy))
{
var ids = query.ItemIds.ToList();
// Try to preserve order
result.Items = result.Items.OrderBy(i => ids.IndexOf(i.Id.ToString("N"))).ToArray();
}
2013-03-16 01:40:34 +00:00
2015-07-14 19:04:16 +00:00
return new Tuple<QueryResult<BaseItem>, bool>(result, true);
2013-10-15 22:16:39 +00:00
}
2015-07-14 19:04:16 +00:00
if (request.Recursive)
2013-10-15 22:16:39 +00:00
{
2015-02-07 03:25:23 +00:00
var result = await ((Folder)item).GetItems(GetItemsQuery(request, user)).ConfigureAwait(false);
2014-10-06 23:58:46 +00:00
2015-02-07 03:25:23 +00:00
return new Tuple<QueryResult<BaseItem>, bool>(result, true);
2013-10-15 22:16:39 +00:00
}
2015-07-14 19:04:16 +00:00
if (user == null)
2013-10-15 22:16:39 +00:00
{
2015-07-14 19:04:16 +00:00
var result = await ((Folder)item).GetItems(GetItemsQuery(request, null)).ConfigureAwait(false);
2014-10-06 23:58:46 +00:00
2015-07-14 19:04:16 +00:00
return new Tuple<QueryResult<BaseItem>, bool>(result, true);
}
2014-10-06 23:58:46 +00:00
2015-07-14 19:04:16 +00:00
var userRoot = item as UserRootFolder;
2014-10-06 23:58:46 +00:00
2015-07-14 19:04:16 +00:00
if (userRoot == null)
{
var result = await ((Folder)item).GetItems(GetItemsQuery(request, user)).ConfigureAwait(false);
2013-10-15 22:16:39 +00:00
2015-07-14 19:04:16 +00:00
return new Tuple<QueryResult<BaseItem>, bool>(result, true);
2013-02-21 01:33:05 +00:00
}
2015-07-14 19:04:16 +00:00
IEnumerable<BaseItem> items = ((Folder)item).GetChildren(user, true);
var itemsArray = items.ToArray();
2014-10-06 23:58:46 +00:00
return new Tuple<QueryResult<BaseItem>, bool>(new QueryResult<BaseItem>
{
2015-07-14 19:04:16 +00:00
Items = itemsArray,
TotalRecordCount = itemsArray.Length
2014-10-06 23:58:46 +00:00
}, false);
2013-02-21 01:33:05 +00:00
}
2014-10-06 23:58:46 +00:00
private InternalItemsQuery GetItemsQuery(GetItems request, User user)
2013-02-21 01:33:05 +00:00
{
2014-10-06 23:58:46 +00:00
var query = new InternalItemsQuery
{
User = user,
IsPlayed = request.IsPlayed,
MediaTypes = request.GetMediaTypes(),
IncludeItemTypes = request.GetIncludeItemTypes(),
ExcludeItemTypes = request.GetExcludeItemTypes(),
Recursive = request.Recursive,
SortBy = request.GetOrderBy(),
SortOrder = request.SortOrder ?? SortOrder.Ascending,
2015-07-14 19:04:16 +00:00
Filter = i => ApplyAdditionalFilters(request, i, user, _libraryManager),
2014-10-06 23:58:46 +00:00
Limit = request.Limit,
2014-10-08 01:37:45 +00:00
StartIndex = request.StartIndex,
IsMissing = request.IsMissing,
IsVirtualUnaired = request.IsVirtualUnaired,
IsUnaired = request.IsUnaired,
CollapseBoxSetItems = request.CollapseBoxSetItems,
NameLessThan = request.NameLessThan,
NameStartsWith = request.NameStartsWith,
NameStartsWithOrGreater = request.NameStartsWithOrGreater,
HasImdbId = request.HasImdbId,
IsYearMismatched = request.IsYearMismatched,
IsPlaceHolder = request.IsPlaceHolder,
IsLocked = request.IsLocked,
IsInBoxSet = request.IsInBoxSet,
IsHD = request.IsHD,
Is3D = request.Is3D,
HasTvdbId = request.HasTvdbId,
HasTmdbId = request.HasTmdbId,
HasOverview = request.HasOverview,
HasOfficialRating = request.HasOfficialRating,
HasParentalRating = request.HasParentalRating,
HasSpecialFeature = request.HasSpecialFeature,
HasSubtitles = request.HasSubtitles,
HasThemeSong = request.HasThemeSong,
HasThemeVideo = request.HasThemeVideo,
HasTrailer = request.HasTrailer,
Tags = request.GetTags(),
OfficialRatings = request.GetOfficialRatings(),
2014-10-08 01:37:45 +00:00
Genres = request.GetGenres(),
2015-11-06 15:02:22 +00:00
GenreIds = request.GetGenreIds(),
2014-10-08 01:37:45 +00:00
Studios = request.GetStudios(),
2015-03-13 18:45:03 +00:00
StudioIds = request.GetStudioIds(),
2014-10-08 01:37:45 +00:00
Person = request.Person,
2015-03-18 16:40:16 +00:00
PersonIds = request.GetPersonIds(),
2014-10-08 01:37:45 +00:00
PersonTypes = request.GetPersonTypes(),
Years = request.GetYears(),
ImageTypes = request.GetImageTypes().ToArray(),
VideoTypes = request.GetVideoTypes().ToArray(),
2015-07-14 19:04:16 +00:00
AdjacentTo = request.AdjacentTo,
ItemIds = request.GetItemIds(),
MinPlayers = request.MinPlayers,
MaxPlayers = request.MaxPlayers,
MinCommunityRating = request.MinCommunityRating,
MinCriticRating = request.MinCriticRating
2014-10-06 23:58:46 +00:00
};
2014-10-08 01:37:45 +00:00
if (!string.IsNullOrWhiteSpace(request.Ids))
{
query.CollapseBoxSetItems = false;
}
2014-10-06 23:58:46 +00:00
foreach (var filter in request.GetFilters())
{
switch (filter)
{
case ItemFilter.Dislikes:
query.IsLiked = false;
break;
case ItemFilter.IsFavorite:
query.IsFavorite = true;
break;
case ItemFilter.IsFavoriteOrLikes:
query.IsFavoriteOrLiked = true;
break;
case ItemFilter.IsFolder:
query.IsFolder = true;
break;
case ItemFilter.IsNotFolder:
query.IsFolder = false;
break;
case ItemFilter.IsPlayed:
query.IsPlayed = true;
break;
case ItemFilter.IsRecentlyAdded:
break;
case ItemFilter.IsResumable:
query.IsResumable = true;
break;
case ItemFilter.IsUnplayed:
query.IsPlayed = false;
break;
case ItemFilter.Likes:
query.IsLiked = true;
break;
}
}
2013-02-21 01:33:05 +00:00
2014-10-06 23:58:46 +00:00
return query;
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Applies filtering
/// </summary>
/// <param name="items">The items.</param>
/// <param name="filter">The filter.</param>
/// <param name="user">The user.</param>
/// <param name="repository">The repository.</param>
2013-02-21 01:33:05 +00:00
/// <returns>IEnumerable{BaseItem}.</returns>
2013-10-02 16:08:58 +00:00
internal static IEnumerable<BaseItem> ApplyFilter(IEnumerable<BaseItem> items, ItemFilter filter, User user, IUserDataManager repository)
2013-02-21 01:33:05 +00:00
{
2014-01-15 22:19:37 +00:00
// Avoid implicitly captured closure
var currentUser = user;
2013-02-21 01:33:05 +00:00
switch (filter)
{
2013-07-30 20:31:25 +00:00
case ItemFilter.IsFavoriteOrLikes:
return items.Where(item =>
{
var userdata = repository.GetUserData(user.Id, item.GetUserDataKey());
if (userdata == null)
{
return false;
}
var likes = userdata.Likes ?? false;
var favorite = userdata.IsFavorite;
return likes || favorite;
});
2013-03-30 14:57:30 +00:00
case ItemFilter.Likes:
return items.Where(item =>
{
2013-06-17 20:35:43 +00:00
var userdata = repository.GetUserData(user.Id, item.GetUserDataKey());
2013-03-30 14:57:30 +00:00
return userdata != null && userdata.Likes.HasValue && userdata.Likes.Value;
});
case ItemFilter.Dislikes:
return items.Where(item =>
{
2013-06-17 20:35:43 +00:00
var userdata = repository.GetUserData(user.Id, item.GetUserDataKey());
2013-03-30 14:57:30 +00:00
return userdata != null && userdata.Likes.HasValue && !userdata.Likes.Value;
});
2013-02-21 01:33:05 +00:00
case ItemFilter.IsFavorite:
return items.Where(item =>
{
2013-06-17 20:35:43 +00:00
var userdata = repository.GetUserData(user.Id, item.GetUserDataKey());
2013-02-21 01:33:05 +00:00
return userdata != null && userdata.IsFavorite;
});
case ItemFilter.IsResumable:
return items.Where(item =>
{
2013-06-17 20:35:43 +00:00
var userdata = repository.GetUserData(user.Id, item.GetUserDataKey());
2013-02-21 01:33:05 +00:00
return userdata != null && userdata.PlaybackPositionTicks > 0;
});
case ItemFilter.IsPlayed:
2014-01-15 22:19:37 +00:00
return items.Where(item => item.IsPlayed(currentUser));
2013-02-21 01:33:05 +00:00
case ItemFilter.IsUnplayed:
2014-01-18 05:55:21 +00:00
return items.Where(item => item.IsUnplayed(currentUser));
2013-02-21 01:33:05 +00:00
case ItemFilter.IsFolder:
return items.Where(item => item.IsFolder);
case ItemFilter.IsNotFolder:
return items.Where(item => !item.IsFolder);
2014-05-07 02:28:19 +00:00
case ItemFilter.IsRecentlyAdded:
return items.Where(item => (DateTime.UtcNow - item.DateCreated).TotalDays <= 10);
2013-02-21 01:33:05 +00:00
}
return items;
}
2015-07-14 19:04:16 +00:00
private bool ApplyAdditionalFilters(GetItems request, BaseItem i, User user, ILibraryManager libraryManager)
2013-02-21 01:33:05 +00:00
{
2015-03-13 17:25:28 +00:00
// Artists
if (!string.IsNullOrEmpty(request.ArtistIds))
{
2015-04-26 03:25:07 +00:00
var artistIds = request.ArtistIds.Split(new[] { '|', ',' });
2015-03-13 17:25:28 +00:00
var audio = i as IHasArtist;
if (!(audio != null && artistIds.Any(id =>
{
2015-03-13 18:45:03 +00:00
var artistItem = libraryManager.GetItemById(id);
return artistItem != null && audio.HasAnyArtist(artistItem.Name);
2015-03-13 17:25:28 +00:00
})))
{
return false;
}
}
2014-10-08 01:37:45 +00:00
// Artists
if (!string.IsNullOrEmpty(request.Artists))
2013-10-06 03:03:08 +00:00
{
2014-10-08 01:37:45 +00:00
var artists = request.Artists.Split('|');
2013-10-06 03:03:08 +00:00
2014-10-08 01:37:45 +00:00
var audio = i as IHasArtist;
2013-10-06 03:03:08 +00:00
2015-03-13 17:25:28 +00:00
if (!(audio != null && artists.Any(audio.HasAnyArtist)))
2014-10-06 23:58:46 +00:00
{
2013-10-06 03:03:08 +00:00
return false;
2014-10-06 23:58:46 +00:00
}
2013-10-06 03:03:08 +00:00
}
2014-10-08 01:37:45 +00:00
// Albums
if (!string.IsNullOrEmpty(request.Albums))
2013-04-30 01:29:04 +00:00
{
2014-10-08 01:37:45 +00:00
var albums = request.Albums.Split('|');
2014-10-08 01:37:45 +00:00
var audio = i as Audio;
2014-10-06 23:58:46 +00:00
2014-10-08 01:37:45 +00:00
if (audio != null)
{
2014-10-08 01:37:45 +00:00
if (!albums.Any(a => string.Equals(a, audio.Album, StringComparison.OrdinalIgnoreCase)))
{
2014-10-06 23:58:46 +00:00
return false;
}
2014-10-06 23:58:46 +00:00
}
2013-04-30 01:29:04 +00:00
2014-10-08 01:37:45 +00:00
var album = i as MusicAlbum;
2013-09-11 17:54:59 +00:00
2014-10-08 01:37:45 +00:00
if (album != null)
2014-10-06 23:58:46 +00:00
{
2014-10-08 01:37:45 +00:00
if (!albums.Any(a => string.Equals(a, album.Name, StringComparison.OrdinalIgnoreCase)))
2013-09-11 17:54:59 +00:00
{
2014-10-06 23:58:46 +00:00
return false;
2013-09-11 17:54:59 +00:00
}
2014-10-06 23:58:46 +00:00
}
2014-10-08 01:37:45 +00:00
var musicVideo = i as MusicVideo;
if (musicVideo != null)
2014-10-06 23:58:46 +00:00
{
2014-10-08 01:37:45 +00:00
if (!albums.Any(a => string.Equals(a, musicVideo.Album, StringComparison.OrdinalIgnoreCase)))
2013-09-11 17:54:59 +00:00
{
2014-10-06 23:58:46 +00:00
return false;
2013-09-11 17:54:59 +00:00
}
2014-10-06 23:58:46 +00:00
}
2014-10-08 01:37:45 +00:00
return false;
2013-09-11 17:54:59 +00:00
}
2014-10-08 01:37:45 +00:00
// Min official rating
if (!string.IsNullOrEmpty(request.MinOfficialRating))
{
2014-10-08 01:37:45 +00:00
var level = _localization.GetRatingLevel(request.MinOfficialRating);
2014-10-08 01:37:45 +00:00
if (level.HasValue)
2014-10-06 23:58:46 +00:00
{
2014-10-08 01:37:45 +00:00
var rating = i.CustomRating;
2013-09-13 20:45:27 +00:00
2014-10-08 01:37:45 +00:00
if (string.IsNullOrEmpty(rating))
2013-09-13 20:45:27 +00:00
{
2014-10-08 01:37:45 +00:00
rating = i.OfficialRating;
2013-09-13 20:45:27 +00:00
}
2014-10-08 01:37:45 +00:00
if (!string.IsNullOrEmpty(rating))
2013-09-13 20:45:27 +00:00
{
2014-10-08 01:37:45 +00:00
var itemLevel = _localization.GetRatingLevel(rating);
if (!(!itemLevel.HasValue || itemLevel.Value >= level.Value))
{
return false;
}
2013-09-13 20:45:27 +00:00
}
2014-10-06 23:58:46 +00:00
}
2013-09-13 20:45:27 +00:00
}
2014-10-08 01:37:45 +00:00
// Max official rating
if (!string.IsNullOrEmpty(request.MaxOfficialRating))
2013-11-15 21:31:33 +00:00
{
2014-10-08 01:37:45 +00:00
var level = _localization.GetRatingLevel(request.MaxOfficialRating);
2014-10-06 23:58:46 +00:00
2014-10-08 01:37:45 +00:00
if (level.HasValue)
2014-10-06 23:58:46 +00:00
{
2014-10-08 01:37:45 +00:00
var rating = i.CustomRating;
2014-10-06 23:58:46 +00:00
2014-10-08 01:37:45 +00:00
if (string.IsNullOrEmpty(rating))
{
rating = i.OfficialRating;
}
if (!string.IsNullOrEmpty(rating))
{
var itemLevel = _localization.GetRatingLevel(rating);
if (!(!itemLevel.HasValue || itemLevel.Value <= level.Value))
{
return false;
}
}
2014-10-06 23:58:46 +00:00
}
2013-11-15 21:31:33 +00:00
}
2014-10-08 01:37:45 +00:00
// LocationTypes
if (!string.IsNullOrEmpty(request.LocationTypes))
{
2014-10-08 01:37:45 +00:00
var vals = request.LocationTypes.Split(',');
if (!vals.Contains(i.LocationType.ToString(), StringComparer.OrdinalIgnoreCase))
2014-10-06 23:58:46 +00:00
{
return false;
}
}
2014-10-08 01:37:45 +00:00
// ExcludeLocationTypes
if (!string.IsNullOrEmpty(request.ExcludeLocationTypes))
{
2014-10-08 01:37:45 +00:00
var vals = request.ExcludeLocationTypes.Split(',');
if (vals.Contains(i.LocationType.ToString(), StringComparer.OrdinalIgnoreCase))
2014-10-06 23:58:46 +00:00
{
return false;
}
}
2014-10-08 01:37:45 +00:00
if (!string.IsNullOrEmpty(request.AlbumArtistStartsWithOrGreater))
2013-12-02 02:24:14 +00:00
{
2014-10-08 01:37:45 +00:00
var ok = new[] { i }.OfType<IHasAlbumArtist>()
.Any(p => string.Compare(request.AlbumArtistStartsWithOrGreater, p.AlbumArtists.FirstOrDefault(), StringComparison.CurrentCultureIgnoreCase) < 1);
2013-12-02 02:24:14 +00:00
2014-10-08 01:37:45 +00:00
if (!ok)
2014-10-06 23:58:46 +00:00
{
return false;
}
2013-12-02 02:24:14 +00:00
}
2014-10-08 01:37:45 +00:00
// Filter by Series Status
if (!string.IsNullOrEmpty(request.SeriesStatus))
2013-12-02 02:24:14 +00:00
{
2014-10-08 01:37:45 +00:00
var vals = request.SeriesStatus.Split(',');
2013-12-02 02:24:14 +00:00
2014-10-08 01:37:45 +00:00
var ok = new[] { i }.OfType<Series>().Any(p => p.Status.HasValue && vals.Contains(p.Status.Value.ToString(), StringComparer.OrdinalIgnoreCase));
2013-12-02 02:24:14 +00:00
2014-10-08 01:37:45 +00:00
if (!ok)
2014-10-06 23:58:46 +00:00
{
return false;
}
2013-12-02 02:24:14 +00:00
}
2014-10-08 01:37:45 +00:00
// Filter by Series AirDays
if (!string.IsNullOrEmpty(request.AirDays))
2013-12-02 02:24:14 +00:00
{
2014-10-08 01:37:45 +00:00
var days = request.AirDays.Split(',').Select(d => (DayOfWeek)Enum.Parse(typeof(DayOfWeek), d, true));
2013-12-02 02:24:14 +00:00
2014-10-08 01:37:45 +00:00
var ok = new[] { i }.OfType<Series>().Any(p => p.AirDays != null && days.Any(d => p.AirDays.Contains(d)));
2013-12-02 02:24:14 +00:00
2014-10-08 01:37:45 +00:00
if (!ok)
2014-10-06 23:58:46 +00:00
{
return false;
}
2013-12-02 02:24:14 +00:00
}
2014-10-08 01:37:45 +00:00
if (request.ParentIndexNumber.HasValue)
{
2014-10-08 01:37:45 +00:00
var filterValue = request.ParentIndexNumber.Value;
2014-10-06 23:58:46 +00:00
2014-10-08 01:37:45 +00:00
var episode = i as Episode;
2013-12-02 02:24:14 +00:00
2014-10-08 01:37:45 +00:00
if (episode != null)
2013-12-02 02:24:14 +00:00
{
2014-10-08 01:37:45 +00:00
if (episode.ParentIndexNumber.HasValue && episode.ParentIndexNumber.Value != filterValue)
2013-12-02 02:24:14 +00:00
{
2014-10-08 01:37:45 +00:00
return false;
2013-12-02 02:24:14 +00:00
}
2014-10-08 01:37:45 +00:00
}
2013-12-02 02:24:14 +00:00
2014-10-08 01:37:45 +00:00
var song = i as Audio;
if (song != null)
{
if (song.ParentIndexNumber.HasValue && song.ParentIndexNumber.Value != filterValue)
2013-12-02 02:24:14 +00:00
{
2014-10-08 01:37:45 +00:00
return false;
2013-12-02 02:24:14 +00:00
}
}
}
2014-10-08 01:37:45 +00:00
if (request.AiredDuringSeason.HasValue)
2014-04-22 17:25:54 +00:00
{
2014-10-08 01:37:45 +00:00
var episode = i as Episode;
2014-04-22 17:25:54 +00:00
2014-10-08 01:37:45 +00:00
if (episode == null)
{
return false;
}
2014-04-22 17:25:54 +00:00
2014-10-08 01:37:45 +00:00
if (!Series.FilterEpisodesBySeason(new[] { episode }, request.AiredDuringSeason.Value, true).Any())
{
return false;
}
2014-04-22 17:25:54 +00:00
}
2014-10-08 01:37:45 +00:00
if (!string.IsNullOrEmpty(request.MinPremiereDate))
2014-04-22 17:25:54 +00:00
{
2014-10-08 01:37:45 +00:00
var date = DateTime.Parse(request.MinPremiereDate, null, DateTimeStyles.RoundtripKind).ToUniversalTime();
2014-04-22 17:25:54 +00:00
2014-10-08 01:37:45 +00:00
if (!(i.PremiereDate.HasValue && i.PremiereDate.Value >= date))
{
return false;
}
2014-04-22 17:25:54 +00:00
}
2014-10-08 01:37:45 +00:00
if (!string.IsNullOrEmpty(request.MaxPremiereDate))
2014-04-22 17:25:54 +00:00
{
2014-10-08 01:37:45 +00:00
var date = DateTime.Parse(request.MaxPremiereDate, null, DateTimeStyles.RoundtripKind).ToUniversalTime();
2014-04-22 17:25:54 +00:00
2014-10-08 01:37:45 +00:00
if (!(i.PremiereDate.HasValue && i.PremiereDate.Value <= date))
{
return false;
}
2014-04-22 17:25:54 +00:00
}
2014-04-27 03:42:05 +00:00
2014-03-15 04:14:07 +00:00
return true;
}
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Class DateCreatedComparer
/// </summary>
public class DateCreatedComparer : IComparer<BaseItem>
{
/// <summary>
/// Compares the specified x.
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <returns>System.Int32.</returns>
public int Compare(BaseItem x, BaseItem y)
{
return x.DateCreated.CompareTo(y.DateCreated);
}
}
}