using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Channels;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MediaBrowser.Api.Reports
{
/// A report builder base.
public class ReportBuilderBase
{
///
/// Initializes a new instance of the MediaBrowser.Api.Reports.ReportBuilderBase class.
/// Manager for library.
public ReportBuilderBase(ILibraryManager libraryManager)
{
_libraryManager = libraryManager;
}
/// Manager for library.
protected readonly ILibraryManager _libraryManager;
/// Gets audio stream.
/// The item.
/// The audio stream.
protected string GetAudioStream(BaseItem item)
{
var stream = GetStream(item, MediaStreamType.Audio);
if (stream != null)
return stream.Codec.ToUpper() == "DCA" ? stream.Profile : stream.Codec.
ToUpper();
return string.Empty;
}
/// Gets an episode.
/// The item.
/// The episode.
protected string GetEpisode(BaseItem item)
{
if (item.GetClientTypeName() == ChannelMediaContentType.Episode.ToString() && item.ParentIndexNumber != null)
return "Season " + item.ParentIndexNumber;
else
return item.Name;
}
/// Gets a genre.
/// The name.
/// The genre.
protected Genre GetGenre(string name)
{
if (string.IsNullOrEmpty(name))
return null;
return _libraryManager.GetGenre(name);
}
/// Gets genre identifier.
/// The name.
/// The genre identifier.
protected string GetGenreID(string name)
{
if (string.IsNullOrEmpty(name))
return string.Empty;
return string.Format("{0:N}",
GetGenre(name).Id);
}
/// Gets list as string.
/// The items.
/// The list as string.
protected string GetListAsString(List items)
{
return String.Join("; ", items);
}
/// Gets media source information.
/// The item.
/// The media source information.
protected MediaSourceInfo GetMediaSourceInfo(BaseItem item)
{
var mediaSource = item as IHasMediaSources;
if (mediaSource != null)
return mediaSource.GetMediaSources(false).FirstOrDefault(n => n.Type == MediaSourceType.Default);
return null;
}
/// Gets an object.
/// Generic type parameter.
/// Type of the r.
/// The item.
/// The function.
/// The default value.
/// The object.
protected R GetObject(BaseItem item, Func function, R defaultValue = default(R)) where T : class
{
var value = item as T;
if (value != null && function != null)
return function(value);
else
return defaultValue;
}
/// Gets a person.
/// The name.
/// The person.
protected Person GetPerson(string name)
{
if (string.IsNullOrEmpty(name))
return null;
return _libraryManager.GetPerson(name);
}
/// Gets person identifier.
/// The name.
/// The person identifier.
protected string GetPersonID(string name)
{
if (string.IsNullOrEmpty(name))
return string.Empty;
return string.Format("{0:N}",
GetPerson(name).Id);
}
/// Gets runtime date time.
/// The runtime.
/// The runtime date time.
protected double? GetRuntimeDateTime(long? runtime)
{
if (runtime.HasValue)
return Math.Ceiling(new TimeSpan(runtime.Value).TotalMinutes);
return null;
}
/// Gets series production year.
/// The item.
/// The series production year.
protected string GetSeriesProductionYear(BaseItem item)
{
string productionYear = item.ProductionYear.ToString();
var series = item as Series;
if (series == null)
{
if (item.ProductionYear == null || item.ProductionYear == 0)
return string.Empty;
return productionYear;
}
if (series.Status == SeriesStatus.Continuing)
return productionYear += "-Present";
if (series.EndDate != null && series.EndDate.Value.Year != series.ProductionYear)
return productionYear += "-" + series.EndDate.Value.Year;
return productionYear;
}
/// Gets a stream.
/// The item.
/// Type of the stream.
/// The stream.
protected MediaStream GetStream(BaseItem item, MediaStreamType streamType)
{
var itemInfo = GetMediaSourceInfo(item);
if (itemInfo != null)
return itemInfo.MediaStreams.FirstOrDefault(n => n.Type == streamType);
return null;
}
/// Gets a studio.
/// The name.
/// The studio.
protected Studio GetStudio(string name)
{
if (string.IsNullOrEmpty(name))
return null;
return _libraryManager.GetStudio(name);
}
/// Gets studio identifier.
/// The name.
/// The studio identifier.
protected string GetStudioID(string name)
{
if (string.IsNullOrEmpty(name))
return string.Empty;
return string.Format("{0:N}",
GetStudio(name).Id);
}
/// Gets video resolution.
/// The item.
/// The video resolution.
protected string GetVideoResolution(BaseItem item)
{
var stream = GetStream(item,
MediaStreamType.Video);
if (stream != null && stream.Width != null)
return string.Format("{0} * {1}",
stream.Width,
(stream.Height != null ? stream.Height.ToString() : "-"));
return string.Empty;
}
/// Gets video stream.
/// The item.
/// The video stream.
protected string GetVideoStream(BaseItem item)
{
var stream = GetStream(item, MediaStreamType.Video);
if (stream != null)
return stream.Codec.ToUpper();
return string.Empty;
}
}
}