EmbyListingsNorthAmerica.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.LiveTv;
  3. using MediaBrowser.Model.Dto;
  4. using MediaBrowser.Model.LiveTv;
  5. using MediaBrowser.Model.Serialization;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Globalization;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace MediaBrowser.Server.Implementations.LiveTv.Listings.Emby
  14. {
  15. public class EmbyListingsNorthAmerica : IEmbyListingProvider
  16. {
  17. private readonly IHttpClient _httpClient;
  18. private readonly IJsonSerializer _jsonSerializer;
  19. public EmbyListingsNorthAmerica(IHttpClient httpClient, IJsonSerializer jsonSerializer)
  20. {
  21. _httpClient = httpClient;
  22. _jsonSerializer = jsonSerializer;
  23. }
  24. public async Task<IEnumerable<ProgramInfo>> GetProgramsAsync(ListingsProviderInfo info, string channelNumber, DateTime startDateUtc, DateTime endDateUtc, CancellationToken cancellationToken)
  25. {
  26. channelNumber = NormalizeNumber(channelNumber);
  27. var url = "https://data.emby.media/service/listings?id=" + info.ListingsId;
  28. // Normalize
  29. startDateUtc = startDateUtc.Date;
  30. endDateUtc = startDateUtc.AddDays(7);
  31. url += "&start=" + startDateUtc.ToString("s", CultureInfo.InvariantCulture) + "Z";
  32. url += "&end=" + endDateUtc.ToString("s", CultureInfo.InvariantCulture) + "Z";
  33. var response = await GetResponse<ListingInfo[]>(url).ConfigureAwait(false);
  34. return response.Where(i => IncludeInResults(i, channelNumber)).Select(GetProgramInfo).OrderBy(i => i.StartDate);
  35. }
  36. private ProgramInfo GetProgramInfo(ListingInfo info)
  37. {
  38. var showType = info.showType ?? string.Empty;
  39. var program = new ProgramInfo
  40. {
  41. Id = info.listingID.ToString(CultureInfo.InvariantCulture),
  42. Name = GetStringValue(info.showName),
  43. HomePageUrl = GetStringValue(info.webLink),
  44. Overview = info.description,
  45. IsHD = info.hd,
  46. IsLive = info.live,
  47. IsPremiere = info.seasonPremiere || info.seriesPremiere,
  48. IsMovie = showType.IndexOf("Movie", StringComparison.OrdinalIgnoreCase) != -1,
  49. IsKids = showType.IndexOf("Children", StringComparison.OrdinalIgnoreCase) != -1,
  50. IsNews = showType.IndexOf("News", StringComparison.OrdinalIgnoreCase) != -1,
  51. IsSports = showType.IndexOf("Sports", StringComparison.OrdinalIgnoreCase) != -1
  52. };
  53. if (!string.IsNullOrWhiteSpace(info.listDateTime))
  54. {
  55. program.StartDate = DateTime.ParseExact(info.listDateTime, "yyyy'-'MM'-'dd' 'HH':'mm':'ss", CultureInfo.InvariantCulture);
  56. program.StartDate = DateTime.SpecifyKind(program.StartDate, DateTimeKind.Utc);
  57. program.EndDate = program.StartDate.AddMinutes(info.duration);
  58. }
  59. if (info.starRating > 0)
  60. {
  61. program.CommunityRating = info.starRating*2;
  62. }
  63. if (!string.IsNullOrWhiteSpace(info.rating))
  64. {
  65. // They don't have dashes so try to normalize
  66. program.OfficialRating = info.rating.Replace("TV", "TV-").Replace("--", "-");
  67. var invalid = new[] { "N/A", "Approved", "Not Rated" };
  68. if (invalid.Contains(program.OfficialRating, StringComparer.OrdinalIgnoreCase))
  69. {
  70. program.OfficialRating = null;
  71. }
  72. }
  73. if (!string.IsNullOrWhiteSpace(info.year))
  74. {
  75. program.ProductionYear = int.Parse(info.year, CultureInfo.InvariantCulture);
  76. }
  77. if (info.showID > 0)
  78. {
  79. program.ShowId = info.showID.ToString(CultureInfo.InvariantCulture);
  80. }
  81. if (info.seriesID > 0)
  82. {
  83. program.SeriesId = info.seriesID.ToString(CultureInfo.InvariantCulture);
  84. program.IsSeries = true;
  85. program.IsRepeat = info.repeat;
  86. program.EpisodeTitle = GetStringValue(info.episodeTitle);
  87. if (string.Equals(program.Name, program.EpisodeTitle, StringComparison.OrdinalIgnoreCase))
  88. {
  89. program.EpisodeTitle = null;
  90. }
  91. }
  92. if (info.starRating > 0)
  93. {
  94. program.CommunityRating = info.starRating * 2;
  95. }
  96. if (string.Equals(info.showName, "Movie", StringComparison.OrdinalIgnoreCase))
  97. {
  98. // Sometimes the movie title will be in here
  99. if (!string.IsNullOrWhiteSpace(info.episodeTitle))
  100. {
  101. program.Name = info.episodeTitle;
  102. }
  103. }
  104. return program;
  105. }
  106. private string GetStringValue(string s)
  107. {
  108. return string.IsNullOrWhiteSpace(s) ? null : s;
  109. }
  110. private bool IncludeInResults(ListingInfo info, string itemNumber)
  111. {
  112. if (string.Equals(itemNumber, NormalizeNumber(info.number), StringComparison.OrdinalIgnoreCase))
  113. {
  114. return true;
  115. }
  116. var channelNumber = info.channelNumber.ToString(CultureInfo.InvariantCulture);
  117. if (info.subChannelNumber > 0)
  118. {
  119. channelNumber += "." + info.subChannelNumber.ToString(CultureInfo.InvariantCulture);
  120. }
  121. return string.Equals(channelNumber, itemNumber, StringComparison.OrdinalIgnoreCase);
  122. }
  123. public async Task AddMetadata(ListingsProviderInfo info, List<ChannelInfo> channels, CancellationToken cancellationToken)
  124. {
  125. var response = await GetResponse<LineupDetailResponse>("https://data.emby.media/service/lineups?id=" + info.ListingsId).ConfigureAwait(false);
  126. foreach (var channel in channels)
  127. {
  128. var station = response.stations.FirstOrDefault(i =>
  129. {
  130. var itemNumber = NormalizeNumber(channel.Number);
  131. if (string.Equals(itemNumber, NormalizeNumber(i.number), StringComparison.OrdinalIgnoreCase))
  132. {
  133. return true;
  134. }
  135. var channelNumber = i.channelNumber.ToString(CultureInfo.InvariantCulture);
  136. if (i.subChannelNumber > 0)
  137. {
  138. channelNumber += "." + i.subChannelNumber.ToString(CultureInfo.InvariantCulture);
  139. }
  140. return string.Equals(channelNumber, itemNumber, StringComparison.OrdinalIgnoreCase);
  141. });
  142. if (station != null)
  143. {
  144. //channel.Name = station.name;
  145. if (!string.IsNullOrWhiteSpace(station.logoFilename))
  146. {
  147. channel.HasImage = true;
  148. channel.ImageUrl = "http://cdn.tvpassport.com/image/station/100x100/" + station.logoFilename;
  149. }
  150. }
  151. }
  152. }
  153. private string NormalizeNumber(string number)
  154. {
  155. return number.Replace('-', '.');
  156. }
  157. public Task Validate(ListingsProviderInfo info, bool validateLogin, bool validateListings)
  158. {
  159. return Task.FromResult(true);
  160. }
  161. public async Task<List<NameIdPair>> GetLineups(string country, string location)
  162. {
  163. // location = postal code
  164. var response = await GetResponse<LineupInfo[]>("https://data.emby.media/service/lineups?postalCode=" + location).ConfigureAwait(false);
  165. return response.Select(i => new NameIdPair
  166. {
  167. Name = GetName(i),
  168. Id = i.lineupID
  169. }).OrderBy(i => i.Name).ToList();
  170. }
  171. private string GetName(LineupInfo info)
  172. {
  173. var name = info.lineupName;
  174. if (string.Equals(info.lineupType, "cab", StringComparison.OrdinalIgnoreCase))
  175. {
  176. name += " - Cable";
  177. }
  178. else if (string.Equals(info.lineupType, "sat", StringComparison.OrdinalIgnoreCase))
  179. {
  180. name += " - SAT";
  181. }
  182. else if (string.Equals(info.lineupType, "ota", StringComparison.OrdinalIgnoreCase))
  183. {
  184. name += " - OTA";
  185. }
  186. return name;
  187. }
  188. private async Task<T> GetResponse<T>(string url, Func<string, string> filter = null)
  189. where T : class
  190. {
  191. using (var stream = await _httpClient.Get(new HttpRequestOptions
  192. {
  193. Url = url,
  194. CacheLength = TimeSpan.FromDays(1),
  195. CacheMode = CacheMode.Unconditional
  196. }).ConfigureAwait(false))
  197. {
  198. using (var reader = new StreamReader(stream))
  199. {
  200. var path = await reader.ReadToEndAsync().ConfigureAwait(false);
  201. using (var secondStream = await _httpClient.Get(new HttpRequestOptions
  202. {
  203. Url = "https://www.mb3admin.com" + path,
  204. CacheLength = TimeSpan.FromDays(1),
  205. CacheMode = CacheMode.Unconditional
  206. }).ConfigureAwait(false))
  207. {
  208. return ParseResponse<T>(secondStream, filter);
  209. }
  210. }
  211. }
  212. }
  213. private T ParseResponse<T>(Stream response, Func<string,string> filter)
  214. {
  215. using (var reader = new StreamReader(response))
  216. {
  217. var json = reader.ReadToEnd();
  218. if (filter != null)
  219. {
  220. json = filter(json);
  221. }
  222. return _jsonSerializer.DeserializeFromString<T>(json);
  223. }
  224. }
  225. private class LineupInfo
  226. {
  227. public string lineupID { get; set; }
  228. public string lineupName { get; set; }
  229. public string lineupType { get; set; }
  230. public string providerID { get; set; }
  231. public string providerName { get; set; }
  232. public string serviceArea { get; set; }
  233. public string country { get; set; }
  234. }
  235. private class Station
  236. {
  237. public string number { get; set; }
  238. public int channelNumber { get; set; }
  239. public int subChannelNumber { get; set; }
  240. public int stationID { get; set; }
  241. public string name { get; set; }
  242. public string callsign { get; set; }
  243. public string network { get; set; }
  244. public string stationType { get; set; }
  245. public int NTSC_TSID { get; set; }
  246. public int DTV_TSID { get; set; }
  247. public string webLink { get; set; }
  248. public string logoFilename { get; set; }
  249. }
  250. private class LineupDetailResponse
  251. {
  252. public string lineupID { get; set; }
  253. public string lineupName { get; set; }
  254. public string lineupType { get; set; }
  255. public string providerID { get; set; }
  256. public string providerName { get; set; }
  257. public string serviceArea { get; set; }
  258. public string country { get; set; }
  259. public List<Station> stations { get; set; }
  260. }
  261. private class ListingInfo
  262. {
  263. public string number { get; set; }
  264. public int channelNumber { get; set; }
  265. public int subChannelNumber { get; set; }
  266. public int stationID { get; set; }
  267. public string name { get; set; }
  268. public string callsign { get; set; }
  269. public string network { get; set; }
  270. public string stationType { get; set; }
  271. public string webLink { get; set; }
  272. public string logoFilename { get; set; }
  273. public int listingID { get; set; }
  274. public string listDateTime { get; set; }
  275. public int duration { get; set; }
  276. public int showID { get; set; }
  277. public int seriesID { get; set; }
  278. public string showName { get; set; }
  279. public string episodeTitle { get; set; }
  280. public string episodeNumber { get; set; }
  281. public int parts { get; set; }
  282. public int partNum { get; set; }
  283. public bool seriesPremiere { get; set; }
  284. public bool seasonPremiere { get; set; }
  285. public bool seriesFinale { get; set; }
  286. public bool seasonFinale { get; set; }
  287. public bool repeat { get; set; }
  288. public bool @new { get; set; }
  289. public string rating { get; set; }
  290. public bool captioned { get; set; }
  291. public bool educational { get; set; }
  292. public bool blackWhite { get; set; }
  293. public bool subtitled { get; set; }
  294. public bool live { get; set; }
  295. public bool hd { get; set; }
  296. public bool descriptiveVideo { get; set; }
  297. public bool inProgress { get; set; }
  298. public string showTypeID { get; set; }
  299. public int breakoutLevel { get; set; }
  300. public string showType { get; set; }
  301. public string year { get; set; }
  302. public string guest { get; set; }
  303. public string cast { get; set; }
  304. public string director { get; set; }
  305. public int starRating { get; set; }
  306. public string description { get; set; }
  307. public string league { get; set; }
  308. public string team1 { get; set; }
  309. public string team2 { get; set; }
  310. public string @event { get; set; }
  311. public string location { get; set; }
  312. }
  313. }
  314. }