LiveTvProgram.cs 8.4 KB

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