ReportBuilderBase.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Entities.Audio;
  3. using MediaBrowser.Controller.Entities.TV;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Model.Channels;
  6. using MediaBrowser.Model.Dto;
  7. using MediaBrowser.Model.Entities;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace MediaBrowser.Api.Reports
  14. {
  15. /// <summary> A report builder base. </summary>
  16. public class ReportBuilderBase
  17. {
  18. /// <summary>
  19. /// Initializes a new instance of the MediaBrowser.Api.Reports.ReportBuilderBase class. </summary>
  20. /// <param name="libraryManager"> Manager for library. </param>
  21. public ReportBuilderBase(ILibraryManager libraryManager)
  22. {
  23. _libraryManager = libraryManager;
  24. }
  25. /// <summary> Manager for library. </summary>
  26. protected readonly ILibraryManager _libraryManager;
  27. /// <summary> Gets audio stream. </summary>
  28. /// <param name="item"> The item. </param>
  29. /// <returns> The audio stream. </returns>
  30. protected string GetAudioStream(BaseItem item)
  31. {
  32. var stream = GetStream(item, MediaStreamType.Audio);
  33. if (stream != null)
  34. return stream.Codec.ToUpper() == "DCA" ? stream.Profile : stream.Codec.
  35. ToUpper();
  36. return string.Empty;
  37. }
  38. /// <summary> Gets an episode. </summary>
  39. /// <param name="item"> The item. </param>
  40. /// <returns> The episode. </returns>
  41. protected string GetEpisode(BaseItem item)
  42. {
  43. if (item.GetClientTypeName() == ChannelMediaContentType.Episode.ToString() && item.ParentIndexNumber != null)
  44. return "Season " + item.ParentIndexNumber;
  45. else
  46. return item.Name;
  47. }
  48. /// <summary> Gets a genre. </summary>
  49. /// <param name="name"> The name. </param>
  50. /// <returns> The genre. </returns>
  51. protected Genre GetGenre(string name)
  52. {
  53. if (string.IsNullOrEmpty(name))
  54. return null;
  55. return _libraryManager.GetGenre(name);
  56. }
  57. /// <summary> Gets genre identifier. </summary>
  58. /// <param name="name"> The name. </param>
  59. /// <returns> The genre identifier. </returns>
  60. protected string GetGenreID(string name)
  61. {
  62. if (string.IsNullOrEmpty(name))
  63. return string.Empty;
  64. return string.Format("{0:N}",
  65. GetGenre(name).Id);
  66. }
  67. /// <summary> Gets list as string. </summary>
  68. /// <param name="items"> The items. </param>
  69. /// <returns> The list as string. </returns>
  70. protected string GetListAsString(List<string> items)
  71. {
  72. return String.Join("; ", items);
  73. }
  74. /// <summary> Gets media source information. </summary>
  75. /// <param name="item"> The item. </param>
  76. /// <returns> The media source information. </returns>
  77. protected MediaSourceInfo GetMediaSourceInfo(BaseItem item)
  78. {
  79. var mediaSource = item as IHasMediaSources;
  80. if (mediaSource != null)
  81. return mediaSource.GetMediaSources(false).FirstOrDefault(n => n.Type == MediaSourceType.Default);
  82. return null;
  83. }
  84. /// <summary> Gets an object. </summary>
  85. /// <typeparam name="T"> Generic type parameter. </typeparam>
  86. /// <typeparam name="R"> Type of the r. </typeparam>
  87. /// <param name="item"> The item. </param>
  88. /// <param name="function"> The function. </param>
  89. /// <param name="defaultValue"> The default value. </param>
  90. /// <returns> The object. </returns>
  91. protected R GetObject<T, R>(BaseItem item, Func<T, R> function, R defaultValue = default(R)) where T : class
  92. {
  93. var value = item as T;
  94. if (value != null && function != null)
  95. return function(value);
  96. else
  97. return defaultValue;
  98. }
  99. /// <summary> Gets a person. </summary>
  100. /// <param name="name"> The name. </param>
  101. /// <returns> The person. </returns>
  102. protected Person GetPerson(string name)
  103. {
  104. if (string.IsNullOrEmpty(name))
  105. return null;
  106. return _libraryManager.GetPerson(name);
  107. }
  108. /// <summary> Gets person identifier. </summary>
  109. /// <param name="name"> The name. </param>
  110. /// <returns> The person identifier. </returns>
  111. protected string GetPersonID(string name)
  112. {
  113. if (string.IsNullOrEmpty(name))
  114. return string.Empty;
  115. return string.Format("{0:N}",
  116. GetPerson(name).Id);
  117. }
  118. /// <summary> Gets runtime date time. </summary>
  119. /// <param name="runtime"> The runtime. </param>
  120. /// <returns> The runtime date time. </returns>
  121. protected double? GetRuntimeDateTime(long? runtime)
  122. {
  123. if (runtime.HasValue)
  124. return Math.Ceiling(new TimeSpan(runtime.Value).TotalMinutes);
  125. return null;
  126. }
  127. /// <summary> Gets series production year. </summary>
  128. /// <param name="item"> The item. </param>
  129. /// <returns> The series production year. </returns>
  130. protected string GetSeriesProductionYear(BaseItem item)
  131. {
  132. string productionYear = item.ProductionYear.ToString();
  133. var series = item as Series;
  134. if (series == null)
  135. {
  136. if (item.ProductionYear == null || item.ProductionYear == 0)
  137. return string.Empty;
  138. return productionYear;
  139. }
  140. if (series.Status == SeriesStatus.Continuing)
  141. return productionYear += "-Present";
  142. if (series.EndDate != null && series.EndDate.Value.Year != series.ProductionYear)
  143. return productionYear += "-" + series.EndDate.Value.Year;
  144. return productionYear;
  145. }
  146. /// <summary> Gets a stream. </summary>
  147. /// <param name="item"> The item. </param>
  148. /// <param name="streamType"> Type of the stream. </param>
  149. /// <returns> The stream. </returns>
  150. protected MediaStream GetStream(BaseItem item, MediaStreamType streamType)
  151. {
  152. var itemInfo = GetMediaSourceInfo(item);
  153. if (itemInfo != null)
  154. return itemInfo.MediaStreams.FirstOrDefault(n => n.Type == streamType);
  155. return null;
  156. }
  157. /// <summary> Gets a studio. </summary>
  158. /// <param name="name"> The name. </param>
  159. /// <returns> The studio. </returns>
  160. protected Studio GetStudio(string name)
  161. {
  162. if (string.IsNullOrEmpty(name))
  163. return null;
  164. return _libraryManager.GetStudio(name);
  165. }
  166. /// <summary> Gets studio identifier. </summary>
  167. /// <param name="name"> The name. </param>
  168. /// <returns> The studio identifier. </returns>
  169. protected string GetStudioID(string name)
  170. {
  171. if (string.IsNullOrEmpty(name))
  172. return string.Empty;
  173. return string.Format("{0:N}",
  174. GetStudio(name).Id);
  175. }
  176. /// <summary> Gets video resolution. </summary>
  177. /// <param name="item"> The item. </param>
  178. /// <returns> The video resolution. </returns>
  179. protected string GetVideoResolution(BaseItem item)
  180. {
  181. var stream = GetStream(item,
  182. MediaStreamType.Video);
  183. if (stream != null && stream.Width != null)
  184. return string.Format("{0} * {1}",
  185. stream.Width,
  186. (stream.Height != null ? stream.Height.ToString() : "-"));
  187. return string.Empty;
  188. }
  189. /// <summary> Gets video stream. </summary>
  190. /// <param name="item"> The item. </param>
  191. /// <returns> The video stream. </returns>
  192. protected string GetVideoStream(BaseItem item)
  193. {
  194. var stream = GetStream(item, MediaStreamType.Video);
  195. if (stream != null)
  196. return stream.Codec.ToUpper();
  197. return string.Empty;
  198. }
  199. }
  200. }