LiveTvProgram.cs 8.3 KB

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