LiveTvProgram.cs 8.4 KB

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