LiveTvProgram.cs 8.4 KB

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