LiveTvProgram.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #nullable disable
  2. #pragma warning disable CS1591, SA1306
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Text.Json.Serialization;
  8. using Jellyfin.Data.Enums;
  9. using Jellyfin.Extensions;
  10. using MediaBrowser.Common.Configuration;
  11. using MediaBrowser.Controller.Entities;
  12. using MediaBrowser.Controller.Providers;
  13. using MediaBrowser.Model.Entities;
  14. using MediaBrowser.Model.LiveTv;
  15. using MediaBrowser.Model.Providers;
  16. namespace MediaBrowser.Controller.LiveTv
  17. {
  18. [Common.RequiresSourceSerialisation]
  19. public class LiveTvProgram : BaseItem, IHasLookupInfo<ItemLookupInfo>, IHasStartDate, IHasProgramAttributes
  20. {
  21. private const string EmbyServiceName = "Emby";
  22. public LiveTvProgram()
  23. {
  24. IsVirtualItem = true;
  25. }
  26. public string SeriesName { get; set; }
  27. [JsonIgnore]
  28. public override SourceType SourceType => SourceType.LiveTV;
  29. /// <summary>
  30. /// Gets or sets start date of the program, in UTC.
  31. /// </summary>
  32. [JsonIgnore]
  33. public DateTime StartDate { get; set; }
  34. /// <summary>
  35. /// Gets or sets a value indicating whether this instance is repeat.
  36. /// </summary>
  37. /// <value><c>true</c> if this instance is repeat; otherwise, <c>false</c>.</value>
  38. [JsonIgnore]
  39. public bool IsRepeat { get; set; }
  40. /// <summary>
  41. /// Gets or sets the episode title.
  42. /// </summary>
  43. /// <value>The episode title.</value>
  44. [JsonIgnore]
  45. public string EpisodeTitle { get; set; }
  46. [JsonIgnore]
  47. public string ShowId { get; set; }
  48. /// <summary>
  49. /// Gets or sets a value indicating whether this instance is movie.
  50. /// </summary>
  51. /// <value><c>true</c> if this instance is movie; otherwise, <c>false</c>.</value>
  52. [JsonIgnore]
  53. public bool IsMovie { get; set; }
  54. /// <summary>
  55. /// Gets a value indicating whether this instance is sports.
  56. /// </summary>
  57. /// <value><c>true</c> if this instance is sports; otherwise, <c>false</c>.</value>
  58. [JsonIgnore]
  59. public bool IsSports => Tags.Contains("Sports", StringComparison.OrdinalIgnoreCase);
  60. /// <summary>
  61. /// Gets or sets a value indicating whether this instance is series.
  62. /// </summary>
  63. /// <value><c>true</c> if this instance is series; otherwise, <c>false</c>.</value>
  64. [JsonIgnore]
  65. public bool IsSeries { get; set; }
  66. /// <summary>
  67. /// Gets a value indicating whether this instance is live.
  68. /// </summary>
  69. /// <value><c>true</c> if this instance is live; otherwise, <c>false</c>.</value>
  70. [JsonIgnore]
  71. public bool IsLive => Tags.Contains("Live", StringComparison.OrdinalIgnoreCase);
  72. /// <summary>
  73. /// Gets a value indicating whether this instance is news.
  74. /// </summary>
  75. /// <value><c>true</c> if this instance is news; otherwise, <c>false</c>.</value>
  76. [JsonIgnore]
  77. public bool IsNews => Tags.Contains("News", StringComparison.OrdinalIgnoreCase);
  78. /// <summary>
  79. /// Gets a value indicating whether this instance is kids.
  80. /// </summary>
  81. /// <value><c>true</c> if this instance is kids; otherwise, <c>false</c>.</value>
  82. [JsonIgnore]
  83. public bool IsKids => Tags.Contains("Kids", StringComparison.OrdinalIgnoreCase);
  84. /// <summary>
  85. /// Gets a value indicating whether this instance is premiere.
  86. /// </summary>
  87. /// <value><c>true</c> if this instance is premiere; otherwise, <c>false</c>.</value>
  88. [JsonIgnore]
  89. public bool IsPremiere => Tags.Contains("Premiere", StringComparison.OrdinalIgnoreCase);
  90. /// <summary>
  91. /// Gets the folder containing the item.
  92. /// If the item is a folder, it returns the folder itself.
  93. /// </summary>
  94. /// <value>The containing folder path.</value>
  95. [JsonIgnore]
  96. public override string ContainingFolderPath => Path;
  97. // [JsonIgnore]
  98. // public override string MediaType
  99. // {
  100. // get
  101. // {
  102. // return ChannelType == ChannelType.TV ? Model.Entities.MediaType.Video : Model.Entities.MediaType.Audio;
  103. // }
  104. // }
  105. [JsonIgnore]
  106. public bool IsAiring
  107. {
  108. get
  109. {
  110. var now = DateTime.UtcNow;
  111. return now >= StartDate && now < EndDate;
  112. }
  113. }
  114. [JsonIgnore]
  115. public bool HasAired
  116. {
  117. get
  118. {
  119. var now = DateTime.UtcNow;
  120. return now >= EndDate;
  121. }
  122. }
  123. [JsonIgnore]
  124. public override bool SupportsPeople
  125. {
  126. get
  127. {
  128. // Optimization
  129. if (IsNews || IsSports)
  130. {
  131. return false;
  132. }
  133. return base.SupportsPeople;
  134. }
  135. }
  136. [JsonIgnore]
  137. public override bool SupportsAncestors => false;
  138. public override List<string> GetUserDataKeys()
  139. {
  140. var list = base.GetUserDataKeys();
  141. if (!IsSeries)
  142. {
  143. var key = this.GetProviderId(MetadataProvider.Imdb);
  144. if (!string.IsNullOrEmpty(key))
  145. {
  146. list.Insert(0, key);
  147. }
  148. key = this.GetProviderId(MetadataProvider.Tmdb);
  149. if (!string.IsNullOrEmpty(key))
  150. {
  151. list.Insert(0, key);
  152. }
  153. }
  154. else if (!string.IsNullOrEmpty(EpisodeTitle))
  155. {
  156. var name = GetClientTypeName();
  157. list.Insert(0, name + "-" + Name + (EpisodeTitle ?? string.Empty));
  158. }
  159. return list;
  160. }
  161. public override double GetDefaultPrimaryImageAspectRatio()
  162. {
  163. var serviceName = ServiceName;
  164. if (string.Equals(serviceName, EmbyServiceName, StringComparison.OrdinalIgnoreCase) || string.Equals(serviceName, "Next Pvr", StringComparison.OrdinalIgnoreCase))
  165. {
  166. return 2.0 / 3;
  167. }
  168. return 16.0 / 9;
  169. }
  170. public override string GetClientTypeName()
  171. {
  172. return "Program";
  173. }
  174. public override UnratedItem GetBlockUnratedType()
  175. {
  176. return UnratedItem.LiveTvProgram;
  177. }
  178. protected override string GetInternalMetadataPath(string basePath)
  179. {
  180. return System.IO.Path.Combine(basePath, "livetv", Id.ToString("N", CultureInfo.InvariantCulture));
  181. }
  182. public override bool CanDelete()
  183. {
  184. return false;
  185. }
  186. private LiveTvOptions GetConfiguration()
  187. {
  188. return ConfigurationManager.GetConfiguration<LiveTvOptions>("livetv");
  189. }
  190. private ListingsProviderInfo GetListingsProviderInfo()
  191. {
  192. if (string.Equals(ServiceName, "Emby", StringComparison.OrdinalIgnoreCase))
  193. {
  194. var config = GetConfiguration();
  195. return config.ListingProviders.FirstOrDefault(i => !string.IsNullOrEmpty(i.MoviePrefix));
  196. }
  197. return null;
  198. }
  199. protected override string GetNameForMetadataLookup()
  200. {
  201. var name = base.GetNameForMetadataLookup();
  202. var listings = GetListingsProviderInfo();
  203. if (listings is not null)
  204. {
  205. if (!string.IsNullOrEmpty(listings.MoviePrefix) && name.StartsWith(listings.MoviePrefix, StringComparison.OrdinalIgnoreCase))
  206. {
  207. name = name.Substring(listings.MoviePrefix.Length).Trim();
  208. }
  209. }
  210. return name;
  211. }
  212. }
  213. }