SchedulesDirect.cs 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  1. using System.Net;
  2. using MediaBrowser.Common;
  3. using MediaBrowser.Common.Net;
  4. using MediaBrowser.Controller.LiveTv;
  5. using MediaBrowser.Model.Dto;
  6. using MediaBrowser.Model.LiveTv;
  7. using MediaBrowser.Model.Logging;
  8. using MediaBrowser.Model.Net;
  9. using MediaBrowser.Model.Serialization;
  10. using System;
  11. using System.Collections.Concurrent;
  12. using System.Collections.Generic;
  13. using System.Globalization;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. namespace MediaBrowser.Server.Implementations.LiveTv.Listings
  19. {
  20. public class SchedulesDirect : IListingsProvider
  21. {
  22. private readonly ILogger _logger;
  23. private readonly IJsonSerializer _jsonSerializer;
  24. private readonly IHttpClient _httpClient;
  25. private readonly SemaphoreSlim _tokenSemaphore = new SemaphoreSlim(1, 1);
  26. private readonly IApplicationHost _appHost;
  27. private const string ApiUrl = "https://json.schedulesdirect.org/20141201";
  28. private readonly Dictionary<string, Dictionary<string, ScheduleDirect.Station>> _channelPairingCache =
  29. new Dictionary<string, Dictionary<string, ScheduleDirect.Station>>(StringComparer.OrdinalIgnoreCase);
  30. public SchedulesDirect(ILogger logger, IJsonSerializer jsonSerializer, IHttpClient httpClient, IApplicationHost appHost)
  31. {
  32. _logger = logger;
  33. _jsonSerializer = jsonSerializer;
  34. _httpClient = httpClient;
  35. _appHost = appHost;
  36. }
  37. private string UserAgent
  38. {
  39. get { return "Emby/" + _appHost.ApplicationVersion; }
  40. }
  41. private List<string> GetScheduleRequestDates(DateTime startDateUtc, DateTime endDateUtc)
  42. {
  43. List<string> dates = new List<string>();
  44. var start = new List<DateTime> { startDateUtc, startDateUtc.ToLocalTime() }.Min().Date;
  45. var end = new List<DateTime> { endDateUtc, endDateUtc.ToLocalTime() }.Max().Date;
  46. while (start <= end)
  47. {
  48. dates.Add(start.ToString("yyyy-MM-dd"));
  49. start = start.AddDays(1);
  50. }
  51. return dates;
  52. }
  53. public async Task<IEnumerable<ProgramInfo>> GetProgramsAsync(ListingsProviderInfo info, string channelNumber, string channelName, DateTime startDateUtc, DateTime endDateUtc, CancellationToken cancellationToken)
  54. {
  55. List<ProgramInfo> programsInfo = new List<ProgramInfo>();
  56. var token = await GetToken(info, cancellationToken).ConfigureAwait(false);
  57. if (string.IsNullOrWhiteSpace(token))
  58. {
  59. _logger.Warn("SchedulesDirect token is empty, returning empty program list");
  60. return programsInfo;
  61. }
  62. if (string.IsNullOrWhiteSpace(info.ListingsId))
  63. {
  64. _logger.Warn("ListingsId is null, returning empty program list");
  65. return programsInfo;
  66. }
  67. var dates = GetScheduleRequestDates(startDateUtc, endDateUtc);
  68. ScheduleDirect.Station station = GetStation(info.ListingsId, channelNumber, channelName);
  69. if (station == null)
  70. {
  71. _logger.Info("No Schedules Direct Station found for channel {0} with name {1}", channelNumber, channelName);
  72. return programsInfo;
  73. }
  74. string stationID = station.stationID;
  75. _logger.Info("Channel Station ID is: " + stationID);
  76. List<ScheduleDirect.RequestScheduleForChannel> requestList =
  77. new List<ScheduleDirect.RequestScheduleForChannel>()
  78. {
  79. new ScheduleDirect.RequestScheduleForChannel()
  80. {
  81. stationID = stationID,
  82. date = dates
  83. }
  84. };
  85. var requestString = _jsonSerializer.SerializeToString(requestList);
  86. _logger.Debug("Request string for schedules is: " + requestString);
  87. var httpOptions = new HttpRequestOptions()
  88. {
  89. Url = ApiUrl + "/schedules",
  90. UserAgent = UserAgent,
  91. CancellationToken = cancellationToken,
  92. // The data can be large so give it some extra time
  93. TimeoutMs = 60000,
  94. LogErrorResponseBody = true
  95. };
  96. httpOptions.RequestHeaders["token"] = token;
  97. httpOptions.RequestContent = requestString;
  98. using (var response = await Post(httpOptions, true, info).ConfigureAwait(false))
  99. {
  100. StreamReader reader = new StreamReader(response.Content);
  101. string responseString = reader.ReadToEnd();
  102. var dailySchedules = _jsonSerializer.DeserializeFromString<List<ScheduleDirect.Day>>(responseString);
  103. _logger.Debug("Found " + dailySchedules.Count + " programs on " + channelNumber + " ScheduleDirect");
  104. httpOptions = new HttpRequestOptions()
  105. {
  106. Url = ApiUrl + "/programs",
  107. UserAgent = UserAgent,
  108. CancellationToken = cancellationToken,
  109. LogErrorResponseBody = true,
  110. // The data can be large so give it some extra time
  111. TimeoutMs = 60000
  112. };
  113. httpOptions.RequestHeaders["token"] = token;
  114. List<string> programsID = new List<string>();
  115. programsID = dailySchedules.SelectMany(d => d.programs.Select(s => s.programID)).Distinct().ToList();
  116. var requestBody = "[\"" + string.Join("\", \"", programsID) + "\"]";
  117. httpOptions.RequestContent = requestBody;
  118. using (var innerResponse = await Post(httpOptions, true, info).ConfigureAwait(false))
  119. {
  120. StreamReader innerReader = new StreamReader(innerResponse.Content);
  121. responseString = innerReader.ReadToEnd();
  122. var programDetails =
  123. _jsonSerializer.DeserializeFromString<List<ScheduleDirect.ProgramDetails>>(
  124. responseString);
  125. var programDict = programDetails.ToDictionary(p => p.programID, y => y);
  126. var images = await GetImageForPrograms(info, programDetails.Where(p => p.hasImageArtwork).Select(p => p.programID).ToList(), cancellationToken);
  127. var schedules = dailySchedules.SelectMany(d => d.programs);
  128. foreach (ScheduleDirect.Program schedule in schedules)
  129. {
  130. //_logger.Debug("Proccesing Schedule for statio ID " + stationID +
  131. // " which corresponds to channel " + channelNumber + " and program id " +
  132. // schedule.programID + " which says it has images? " +
  133. // programDict[schedule.programID].hasImageArtwork);
  134. if (images != null)
  135. {
  136. var imageIndex = images.FindIndex(i => i.programID == schedule.programID.Substring(0, 10));
  137. if (imageIndex > -1)
  138. {
  139. programDict[schedule.programID].images = GetProgramLogo(ApiUrl, images[imageIndex]);
  140. }
  141. }
  142. programsInfo.Add(GetProgram(channelNumber, schedule, programDict[schedule.programID]));
  143. }
  144. _logger.Info("Finished with EPGData");
  145. }
  146. }
  147. return programsInfo;
  148. }
  149. private readonly object _channelCacheLock = new object();
  150. private ScheduleDirect.Station GetStation(string listingsId, string channelNumber, string channelName)
  151. {
  152. lock (_channelCacheLock)
  153. {
  154. Dictionary<string, ScheduleDirect.Station> channelPair;
  155. if (_channelPairingCache.TryGetValue(listingsId, out channelPair))
  156. {
  157. ScheduleDirect.Station station;
  158. if (channelPair.TryGetValue(channelNumber, out station))
  159. {
  160. return station;
  161. }
  162. if (string.IsNullOrWhiteSpace(channelName))
  163. {
  164. return null;
  165. }
  166. channelName = NormalizeName(channelName);
  167. return channelPair.Values.FirstOrDefault(i => string.Equals(NormalizeName(i.callsign ?? string.Empty), channelName, StringComparison.OrdinalIgnoreCase));
  168. }
  169. return null;
  170. }
  171. }
  172. private void AddToChannelPairCache(string listingsId, string channelNumber, ScheduleDirect.Station schChannel)
  173. {
  174. lock (_channelCacheLock)
  175. {
  176. Dictionary<string, ScheduleDirect.Station> cache;
  177. if (_channelPairingCache.TryGetValue(listingsId, out cache))
  178. {
  179. cache[channelNumber] = schChannel;
  180. }
  181. else
  182. {
  183. cache = new Dictionary<string, ScheduleDirect.Station>();
  184. cache[channelNumber] = schChannel;
  185. _channelPairingCache[listingsId] = cache;
  186. }
  187. }
  188. }
  189. private void ClearPairCache(string listingsId)
  190. {
  191. lock (_channelCacheLock)
  192. {
  193. Dictionary<string, ScheduleDirect.Station> cache;
  194. if (_channelPairingCache.TryGetValue(listingsId, out cache))
  195. {
  196. cache.Clear();
  197. }
  198. }
  199. }
  200. private int GetChannelPairCacheCount(string listingsId)
  201. {
  202. lock (_channelCacheLock)
  203. {
  204. Dictionary<string, ScheduleDirect.Station> cache;
  205. if (_channelPairingCache.TryGetValue(listingsId, out cache))
  206. {
  207. return cache.Count;
  208. }
  209. return 0;
  210. }
  211. }
  212. private string NormalizeName(string value)
  213. {
  214. return value.Replace(" ", string.Empty).Replace("-", string.Empty);
  215. }
  216. public async Task AddMetadata(ListingsProviderInfo info, List<ChannelInfo> channels,
  217. CancellationToken cancellationToken)
  218. {
  219. var listingsId = info.ListingsId;
  220. if (string.IsNullOrWhiteSpace(listingsId))
  221. {
  222. throw new Exception("ListingsId required");
  223. }
  224. var token = await GetToken(info, cancellationToken);
  225. if (string.IsNullOrWhiteSpace(token))
  226. {
  227. throw new Exception("token required");
  228. }
  229. ClearPairCache(listingsId);
  230. var httpOptions = new HttpRequestOptions()
  231. {
  232. Url = ApiUrl + "/lineups/" + listingsId,
  233. UserAgent = UserAgent,
  234. CancellationToken = cancellationToken,
  235. LogErrorResponseBody = true,
  236. // The data can be large so give it some extra time
  237. TimeoutMs = 60000
  238. };
  239. httpOptions.RequestHeaders["token"] = token;
  240. using (var response = await Get(httpOptions, true, info).ConfigureAwait(false))
  241. {
  242. var root = _jsonSerializer.DeserializeFromStream<ScheduleDirect.Channel>(response);
  243. _logger.Info("Found " + root.map.Count + " channels on the lineup on ScheduleDirect");
  244. _logger.Info("Mapping Stations to Channel");
  245. foreach (ScheduleDirect.Map map in root.map)
  246. {
  247. var channelNumber = map.logicalChannelNumber;
  248. if (string.IsNullOrWhiteSpace(channelNumber))
  249. {
  250. channelNumber = map.channel;
  251. }
  252. if (string.IsNullOrWhiteSpace(channelNumber))
  253. {
  254. channelNumber = map.atscMajor + "." + map.atscMinor;
  255. }
  256. channelNumber = channelNumber.TrimStart('0');
  257. _logger.Debug("Found channel: " + channelNumber + " in Schedules Direct");
  258. var schChannel = root.stations.FirstOrDefault(item => item.stationID == map.stationID);
  259. AddToChannelPairCache(listingsId, channelNumber, schChannel);
  260. }
  261. _logger.Info("Added " + GetChannelPairCacheCount(listingsId) + " channels to the dictionary");
  262. foreach (ChannelInfo channel in channels)
  263. {
  264. var station = GetStation(listingsId, channel.Number, channel.Name);
  265. if (station != null)
  266. {
  267. if (station.logo != null)
  268. {
  269. channel.ImageUrl = station.logo.URL;
  270. channel.HasImage = true;
  271. }
  272. string channelName = station.name;
  273. channel.Name = channelName;
  274. }
  275. else
  276. {
  277. _logger.Info("Schedules Direct doesnt have data for channel: " + channel.Number + " " + channel.Name);
  278. }
  279. }
  280. }
  281. }
  282. private ProgramInfo GetProgram(string channel, ScheduleDirect.Program programInfo,
  283. ScheduleDirect.ProgramDetails details)
  284. {
  285. //_logger.Debug("Show type is: " + (details.showType ?? "No ShowType"));
  286. DateTime startAt = GetDate(programInfo.airDateTime);
  287. DateTime endAt = startAt.AddSeconds(programInfo.duration);
  288. ProgramAudio audioType = ProgramAudio.Stereo;
  289. bool repeat = programInfo.@new == null;
  290. string newID = programInfo.programID + "T" + startAt.Ticks + "C" + channel;
  291. if (programInfo.audioProperties != null)
  292. {
  293. if (programInfo.audioProperties.Exists(item => string.Equals(item, "dd 5.1", StringComparison.OrdinalIgnoreCase)))
  294. {
  295. audioType = ProgramAudio.DolbyDigital;
  296. }
  297. else if (programInfo.audioProperties.Exists(item => string.Equals(item, "dd", StringComparison.OrdinalIgnoreCase)))
  298. {
  299. audioType = ProgramAudio.DolbyDigital;
  300. }
  301. else if (programInfo.audioProperties.Exists(item => string.Equals(item, "stereo", StringComparison.OrdinalIgnoreCase)))
  302. {
  303. audioType = ProgramAudio.Stereo;
  304. }
  305. else
  306. {
  307. audioType = ProgramAudio.Mono;
  308. }
  309. }
  310. string episodeTitle = null;
  311. if (details.episodeTitle150 != null)
  312. {
  313. episodeTitle = details.episodeTitle150;
  314. }
  315. string imageUrl = null;
  316. if (details.hasImageArtwork)
  317. {
  318. imageUrl = details.images;
  319. }
  320. var showType = details.showType ?? string.Empty;
  321. var info = new ProgramInfo
  322. {
  323. ChannelId = channel,
  324. Id = newID,
  325. StartDate = startAt,
  326. EndDate = endAt,
  327. Name = details.titles[0].title120 ?? "Unkown",
  328. OfficialRating = null,
  329. CommunityRating = null,
  330. EpisodeTitle = episodeTitle,
  331. Audio = audioType,
  332. IsRepeat = repeat,
  333. IsSeries = showType.IndexOf("series", StringComparison.OrdinalIgnoreCase) != -1,
  334. ImageUrl = imageUrl,
  335. IsKids = string.Equals(details.audience, "children", StringComparison.OrdinalIgnoreCase),
  336. IsSports = showType.IndexOf("sports", StringComparison.OrdinalIgnoreCase) != -1,
  337. IsMovie = showType.IndexOf("movie", StringComparison.OrdinalIgnoreCase) != -1 || showType.IndexOf("film", StringComparison.OrdinalIgnoreCase) != -1,
  338. ShowId = programInfo.programID,
  339. Etag = programInfo.md5
  340. };
  341. if (programInfo.videoProperties != null)
  342. {
  343. info.IsHD = programInfo.videoProperties.Contains("hdtv", StringComparer.OrdinalIgnoreCase);
  344. }
  345. if (details.contentRating != null && details.contentRating.Count > 0)
  346. {
  347. info.OfficialRating = details.contentRating[0].code.Replace("TV", "TV-").Replace("--", "-");
  348. var invalid = new[] { "N/A", "Approved", "Not Rated", "Passed" };
  349. if (invalid.Contains(info.OfficialRating, StringComparer.OrdinalIgnoreCase))
  350. {
  351. info.OfficialRating = null;
  352. }
  353. }
  354. if (details.descriptions != null)
  355. {
  356. if (details.descriptions.description1000 != null)
  357. {
  358. info.Overview = details.descriptions.description1000[0].description;
  359. }
  360. else if (details.descriptions.description100 != null)
  361. {
  362. info.ShortOverview = details.descriptions.description100[0].description;
  363. }
  364. }
  365. if (info.IsSeries)
  366. {
  367. info.SeriesId = programInfo.programID.Substring(0, 10);
  368. if (details.metadata != null)
  369. {
  370. var gracenote = details.metadata.Find(x => x.Gracenote != null).Gracenote;
  371. info.SeasonNumber = gracenote.season;
  372. info.EpisodeNumber = gracenote.episode;
  373. }
  374. }
  375. if (!string.IsNullOrWhiteSpace(details.originalAirDate))
  376. {
  377. info.OriginalAirDate = DateTime.Parse(details.originalAirDate);
  378. }
  379. if (details.genres != null)
  380. {
  381. info.Genres = details.genres.Where(g => !string.IsNullOrWhiteSpace(g)).ToList();
  382. info.IsNews = details.genres.Contains("news", StringComparer.OrdinalIgnoreCase);
  383. if (info.Genres.Contains("children", StringComparer.OrdinalIgnoreCase))
  384. {
  385. info.IsKids = true;
  386. }
  387. }
  388. return info;
  389. }
  390. private DateTime GetDate(string value)
  391. {
  392. var date = DateTime.ParseExact(value, "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'", CultureInfo.InvariantCulture);
  393. if (date.Kind != DateTimeKind.Utc)
  394. {
  395. date = DateTime.SpecifyKind(date, DateTimeKind.Utc);
  396. }
  397. return date;
  398. }
  399. private string GetProgramLogo(string apiUrl, ScheduleDirect.ShowImages images)
  400. {
  401. string url = null;
  402. if (images.data != null)
  403. {
  404. var smallImages = images.data.Where(i => i.size == "Sm").ToList();
  405. if (smallImages.Any())
  406. {
  407. images.data = smallImages;
  408. }
  409. var logoIndex = images.data.FindIndex(i => i.category == "Logo");
  410. if (logoIndex == -1)
  411. {
  412. logoIndex = 0;
  413. }
  414. var uri = images.data[logoIndex].uri;
  415. if (!string.IsNullOrWhiteSpace(uri))
  416. {
  417. if (uri.IndexOf("http", StringComparison.OrdinalIgnoreCase) != -1)
  418. {
  419. url = uri;
  420. }
  421. else
  422. {
  423. url = apiUrl + "/image/" + uri;
  424. }
  425. }
  426. //_logger.Debug("URL for image is : " + url);
  427. }
  428. return url;
  429. }
  430. private async Task<List<ScheduleDirect.ShowImages>> GetImageForPrograms(
  431. ListingsProviderInfo info,
  432. List<string> programIds,
  433. CancellationToken cancellationToken)
  434. {
  435. var imageIdString = "[";
  436. programIds.ForEach(i =>
  437. {
  438. if (!imageIdString.Contains(i.Substring(0, 10)))
  439. {
  440. imageIdString += "\"" + i.Substring(0, 10) + "\",";
  441. }
  442. });
  443. imageIdString = imageIdString.TrimEnd(',') + "]";
  444. var httpOptions = new HttpRequestOptions()
  445. {
  446. Url = ApiUrl + "/metadata/programs",
  447. UserAgent = UserAgent,
  448. CancellationToken = cancellationToken,
  449. RequestContent = imageIdString,
  450. LogErrorResponseBody = true,
  451. // The data can be large so give it some extra time
  452. TimeoutMs = 60000
  453. };
  454. List<ScheduleDirect.ShowImages> images;
  455. using (var innerResponse2 = await Post(httpOptions, true, info).ConfigureAwait(false))
  456. {
  457. images = _jsonSerializer.DeserializeFromStream<List<ScheduleDirect.ShowImages>>(
  458. innerResponse2.Content);
  459. }
  460. return images;
  461. }
  462. public async Task<List<NameIdPair>> GetHeadends(ListingsProviderInfo info, string country, string location, CancellationToken cancellationToken)
  463. {
  464. var token = await GetToken(info, cancellationToken);
  465. var lineups = new List<NameIdPair>();
  466. if (string.IsNullOrWhiteSpace(token))
  467. {
  468. return lineups;
  469. }
  470. var options = new HttpRequestOptions()
  471. {
  472. Url = ApiUrl + "/headends?country=" + country + "&postalcode=" + location,
  473. UserAgent = UserAgent,
  474. CancellationToken = cancellationToken,
  475. LogErrorResponseBody = true
  476. };
  477. options.RequestHeaders["token"] = token;
  478. try
  479. {
  480. using (Stream responce = await Get(options, false, info).ConfigureAwait(false))
  481. {
  482. var root = _jsonSerializer.DeserializeFromStream<List<ScheduleDirect.Headends>>(responce);
  483. if (root != null)
  484. {
  485. foreach (ScheduleDirect.Headends headend in root)
  486. {
  487. foreach (ScheduleDirect.Lineup lineup in headend.lineups)
  488. {
  489. lineups.Add(new NameIdPair
  490. {
  491. Name = string.IsNullOrWhiteSpace(lineup.name) ? lineup.lineup : lineup.name,
  492. Id = lineup.uri.Substring(18)
  493. });
  494. }
  495. }
  496. }
  497. else
  498. {
  499. _logger.Info("No lineups available");
  500. }
  501. }
  502. }
  503. catch (Exception ex)
  504. {
  505. _logger.Error("Error getting headends", ex);
  506. }
  507. return lineups;
  508. }
  509. private readonly ConcurrentDictionary<string, NameValuePair> _tokens = new ConcurrentDictionary<string, NameValuePair>();
  510. private DateTime _lastErrorResponse;
  511. private async Task<string> GetToken(ListingsProviderInfo info, CancellationToken cancellationToken)
  512. {
  513. var username = info.Username;
  514. // Reset the token if there's no username
  515. if (string.IsNullOrWhiteSpace(username))
  516. {
  517. return null;
  518. }
  519. var password = info.Password;
  520. if (string.IsNullOrWhiteSpace(password))
  521. {
  522. return null;
  523. }
  524. // Avoid hammering SD
  525. if ((DateTime.UtcNow - _lastErrorResponse).TotalMinutes < 1)
  526. {
  527. return null;
  528. }
  529. NameValuePair savedToken = null;
  530. if (!_tokens.TryGetValue(username, out savedToken))
  531. {
  532. savedToken = new NameValuePair();
  533. _tokens.TryAdd(username, savedToken);
  534. }
  535. if (!string.IsNullOrWhiteSpace(savedToken.Name) && !string.IsNullOrWhiteSpace(savedToken.Value))
  536. {
  537. long ticks;
  538. if (long.TryParse(savedToken.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out ticks))
  539. {
  540. // If it's under 24 hours old we can still use it
  541. if (DateTime.UtcNow.Ticks - ticks < TimeSpan.FromHours(20).Ticks)
  542. {
  543. return savedToken.Name;
  544. }
  545. }
  546. }
  547. await _tokenSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
  548. try
  549. {
  550. var result = await GetTokenInternal(username, password, cancellationToken).ConfigureAwait(false);
  551. savedToken.Name = result;
  552. savedToken.Value = DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture);
  553. return result;
  554. }
  555. catch (HttpException ex)
  556. {
  557. if (ex.StatusCode.HasValue)
  558. {
  559. if ((int)ex.StatusCode.Value == 400)
  560. {
  561. _tokens.Clear();
  562. _lastErrorResponse = DateTime.UtcNow;
  563. }
  564. }
  565. throw;
  566. }
  567. finally
  568. {
  569. _tokenSemaphore.Release();
  570. }
  571. }
  572. private async Task<HttpResponseInfo> Post(HttpRequestOptions options,
  573. bool enableRetry,
  574. ListingsProviderInfo providerInfo)
  575. {
  576. try
  577. {
  578. return await _httpClient.Post(options).ConfigureAwait(false);
  579. }
  580. catch (HttpException ex)
  581. {
  582. _tokens.Clear();
  583. if (!ex.StatusCode.HasValue || (int)ex.StatusCode.Value >= 500)
  584. {
  585. enableRetry = false;
  586. }
  587. if (!enableRetry)
  588. {
  589. throw;
  590. }
  591. }
  592. var newToken = await GetToken(providerInfo, options.CancellationToken).ConfigureAwait(false);
  593. options.RequestHeaders["token"] = newToken;
  594. return await Post(options, false, providerInfo).ConfigureAwait(false);
  595. }
  596. private async Task<Stream> Get(HttpRequestOptions options,
  597. bool enableRetry,
  598. ListingsProviderInfo providerInfo)
  599. {
  600. try
  601. {
  602. return await _httpClient.Get(options).ConfigureAwait(false);
  603. }
  604. catch (HttpException ex)
  605. {
  606. _tokens.Clear();
  607. if (!ex.StatusCode.HasValue || (int)ex.StatusCode.Value >= 500)
  608. {
  609. enableRetry = false;
  610. }
  611. if (!enableRetry)
  612. {
  613. throw;
  614. }
  615. }
  616. var newToken = await GetToken(providerInfo, options.CancellationToken).ConfigureAwait(false);
  617. options.RequestHeaders["token"] = newToken;
  618. return await Get(options, false, providerInfo).ConfigureAwait(false);
  619. }
  620. private async Task<string> GetTokenInternal(string username, string password,
  621. CancellationToken cancellationToken)
  622. {
  623. var httpOptions = new HttpRequestOptions()
  624. {
  625. Url = ApiUrl + "/token",
  626. UserAgent = UserAgent,
  627. RequestContent = "{\"username\":\"" + username + "\",\"password\":\"" + password + "\"}",
  628. CancellationToken = cancellationToken,
  629. LogErrorResponseBody = true
  630. };
  631. //_logger.Info("Obtaining token from Schedules Direct from addres: " + httpOptions.Url + " with body " +
  632. // httpOptions.RequestContent);
  633. using (var responce = await Post(httpOptions, false, null).ConfigureAwait(false))
  634. {
  635. var root = _jsonSerializer.DeserializeFromStream<ScheduleDirect.Token>(responce.Content);
  636. if (root.message == "OK")
  637. {
  638. _logger.Info("Authenticated with Schedules Direct token: " + root.token);
  639. return root.token;
  640. }
  641. throw new ApplicationException("Could not authenticate with Schedules Direct Error: " + root.message);
  642. }
  643. }
  644. private async Task AddLineupToAccount(ListingsProviderInfo info, CancellationToken cancellationToken)
  645. {
  646. var token = await GetToken(info, cancellationToken);
  647. if (string.IsNullOrWhiteSpace(token))
  648. {
  649. throw new ArgumentException("Authentication required.");
  650. }
  651. if (string.IsNullOrWhiteSpace(info.ListingsId))
  652. {
  653. throw new ArgumentException("Listings Id required");
  654. }
  655. _logger.Info("Adding new LineUp ");
  656. var httpOptions = new HttpRequestOptions()
  657. {
  658. Url = ApiUrl + "/lineups/" + info.ListingsId,
  659. UserAgent = UserAgent,
  660. CancellationToken = cancellationToken,
  661. LogErrorResponseBody = true
  662. };
  663. httpOptions.RequestHeaders["token"] = token;
  664. using (var response = await _httpClient.SendAsync(httpOptions, "PUT"))
  665. {
  666. }
  667. }
  668. public string Name
  669. {
  670. get { return "Schedules Direct"; }
  671. }
  672. public string Type
  673. {
  674. get { return "SchedulesDirect"; }
  675. }
  676. private async Task<bool> HasLineup(ListingsProviderInfo info, CancellationToken cancellationToken)
  677. {
  678. if (string.IsNullOrWhiteSpace(info.ListingsId))
  679. {
  680. throw new ArgumentException("Listings Id required");
  681. }
  682. var token = await GetToken(info, cancellationToken);
  683. if (string.IsNullOrWhiteSpace(token))
  684. {
  685. throw new Exception("token required");
  686. }
  687. _logger.Info("Headends on account ");
  688. var options = new HttpRequestOptions()
  689. {
  690. Url = ApiUrl + "/lineups",
  691. UserAgent = UserAgent,
  692. CancellationToken = cancellationToken,
  693. LogErrorResponseBody = true
  694. };
  695. options.RequestHeaders["token"] = token;
  696. try
  697. {
  698. using (var response = await Get(options, false, null).ConfigureAwait(false))
  699. {
  700. var root = _jsonSerializer.DeserializeFromStream<ScheduleDirect.Lineups>(response);
  701. return root.lineups.Any(i => string.Equals(info.ListingsId, i.lineup, StringComparison.OrdinalIgnoreCase));
  702. }
  703. }
  704. catch (HttpException ex)
  705. {
  706. // Apparently we're supposed to swallow this
  707. if (ex.StatusCode.HasValue && ex.StatusCode.Value == HttpStatusCode.BadRequest)
  708. {
  709. return false;
  710. }
  711. throw;
  712. }
  713. }
  714. public async Task Validate(ListingsProviderInfo info, bool validateLogin, bool validateListings)
  715. {
  716. if (validateLogin)
  717. {
  718. if (string.IsNullOrWhiteSpace(info.Username))
  719. {
  720. throw new ArgumentException("Username is required");
  721. }
  722. if (string.IsNullOrWhiteSpace(info.Password))
  723. {
  724. throw new ArgumentException("Password is required");
  725. }
  726. }
  727. if (validateListings)
  728. {
  729. if (string.IsNullOrWhiteSpace(info.ListingsId))
  730. {
  731. throw new ArgumentException("Listings Id required");
  732. }
  733. var hasLineup = await HasLineup(info, CancellationToken.None).ConfigureAwait(false);
  734. if (!hasLineup)
  735. {
  736. await AddLineupToAccount(info, CancellationToken.None).ConfigureAwait(false);
  737. }
  738. }
  739. }
  740. public Task<List<NameIdPair>> GetLineups(ListingsProviderInfo info, string country, string location)
  741. {
  742. return GetHeadends(info, country, location, CancellationToken.None);
  743. }
  744. public async Task<List<ChannelInfo>> GetChannels(ListingsProviderInfo info, CancellationToken cancellationToken)
  745. {
  746. var listingsId = info.ListingsId;
  747. if (string.IsNullOrWhiteSpace(listingsId))
  748. {
  749. throw new Exception("ListingsId required");
  750. }
  751. await AddMetadata(info, new List<ChannelInfo>(), cancellationToken).ConfigureAwait(false);
  752. var token = await GetToken(info, cancellationToken);
  753. if (string.IsNullOrWhiteSpace(token))
  754. {
  755. throw new Exception("token required");
  756. }
  757. var httpOptions = new HttpRequestOptions()
  758. {
  759. Url = ApiUrl + "/lineups/" + listingsId,
  760. UserAgent = UserAgent,
  761. CancellationToken = cancellationToken,
  762. LogErrorResponseBody = true,
  763. // The data can be large so give it some extra time
  764. TimeoutMs = 60000
  765. };
  766. httpOptions.RequestHeaders["token"] = token;
  767. var list = new List<ChannelInfo>();
  768. using (var response = await Get(httpOptions, true, info).ConfigureAwait(false))
  769. {
  770. var root = _jsonSerializer.DeserializeFromStream<ScheduleDirect.Channel>(response);
  771. _logger.Info("Found " + root.map.Count + " channels on the lineup on ScheduleDirect");
  772. _logger.Info("Mapping Stations to Channel");
  773. foreach (ScheduleDirect.Map map in root.map)
  774. {
  775. var channelNumber = map.logicalChannelNumber;
  776. if (string.IsNullOrWhiteSpace(channelNumber))
  777. {
  778. channelNumber = map.channel;
  779. }
  780. if (string.IsNullOrWhiteSpace(channelNumber))
  781. {
  782. channelNumber = map.atscMajor + "." + map.atscMinor;
  783. }
  784. channelNumber = channelNumber.TrimStart('0');
  785. var name = channelNumber;
  786. var station = GetStation(listingsId, channelNumber, null);
  787. if (station != null)
  788. {
  789. name = station.name;
  790. }
  791. list.Add(new ChannelInfo
  792. {
  793. Number = channelNumber,
  794. Name = name
  795. });
  796. }
  797. }
  798. return list;
  799. }
  800. public class ScheduleDirect
  801. {
  802. public class Token
  803. {
  804. public int code { get; set; }
  805. public string message { get; set; }
  806. public string serverID { get; set; }
  807. public string token { get; set; }
  808. }
  809. public class Lineup
  810. {
  811. public string lineup { get; set; }
  812. public string name { get; set; }
  813. public string transport { get; set; }
  814. public string location { get; set; }
  815. public string uri { get; set; }
  816. }
  817. public class Lineups
  818. {
  819. public int code { get; set; }
  820. public string serverID { get; set; }
  821. public string datetime { get; set; }
  822. public List<Lineup> lineups { get; set; }
  823. }
  824. public class Headends
  825. {
  826. public string headend { get; set; }
  827. public string transport { get; set; }
  828. public string location { get; set; }
  829. public List<Lineup> lineups { get; set; }
  830. }
  831. public class Map
  832. {
  833. public string stationID { get; set; }
  834. public string channel { get; set; }
  835. public string logicalChannelNumber { get; set; }
  836. public int uhfVhf { get; set; }
  837. public int atscMajor { get; set; }
  838. public int atscMinor { get; set; }
  839. }
  840. public class Broadcaster
  841. {
  842. public string city { get; set; }
  843. public string state { get; set; }
  844. public string postalcode { get; set; }
  845. public string country { get; set; }
  846. }
  847. public class Logo
  848. {
  849. public string URL { get; set; }
  850. public int height { get; set; }
  851. public int width { get; set; }
  852. public string md5 { get; set; }
  853. }
  854. public class Station
  855. {
  856. public string stationID { get; set; }
  857. public string name { get; set; }
  858. public string callsign { get; set; }
  859. public List<string> broadcastLanguage { get; set; }
  860. public List<string> descriptionLanguage { get; set; }
  861. public Broadcaster broadcaster { get; set; }
  862. public string affiliate { get; set; }
  863. public Logo logo { get; set; }
  864. public bool? isCommercialFree { get; set; }
  865. }
  866. public class Metadata
  867. {
  868. public string lineup { get; set; }
  869. public string modified { get; set; }
  870. public string transport { get; set; }
  871. }
  872. public class Channel
  873. {
  874. public List<Map> map { get; set; }
  875. public List<Station> stations { get; set; }
  876. public Metadata metadata { get; set; }
  877. }
  878. public class RequestScheduleForChannel
  879. {
  880. public string stationID { get; set; }
  881. public List<string> date { get; set; }
  882. }
  883. public class Rating
  884. {
  885. public string body { get; set; }
  886. public string code { get; set; }
  887. }
  888. public class Multipart
  889. {
  890. public int partNumber { get; set; }
  891. public int totalParts { get; set; }
  892. }
  893. public class Program
  894. {
  895. public string programID { get; set; }
  896. public string airDateTime { get; set; }
  897. public int duration { get; set; }
  898. public string md5 { get; set; }
  899. public List<string> audioProperties { get; set; }
  900. public List<string> videoProperties { get; set; }
  901. public List<Rating> ratings { get; set; }
  902. public bool? @new { get; set; }
  903. public Multipart multipart { get; set; }
  904. }
  905. public class MetadataSchedule
  906. {
  907. public string modified { get; set; }
  908. public string md5 { get; set; }
  909. public string startDate { get; set; }
  910. public string endDate { get; set; }
  911. public int days { get; set; }
  912. }
  913. public class Day
  914. {
  915. public string stationID { get; set; }
  916. public List<Program> programs { get; set; }
  917. public MetadataSchedule metadata { get; set; }
  918. public Day()
  919. {
  920. programs = new List<Program>();
  921. }
  922. }
  923. //
  924. public class Title
  925. {
  926. public string title120 { get; set; }
  927. }
  928. public class EventDetails
  929. {
  930. public string subType { get; set; }
  931. }
  932. public class Description100
  933. {
  934. public string descriptionLanguage { get; set; }
  935. public string description { get; set; }
  936. }
  937. public class Description1000
  938. {
  939. public string descriptionLanguage { get; set; }
  940. public string description { get; set; }
  941. }
  942. public class DescriptionsProgram
  943. {
  944. public List<Description100> description100 { get; set; }
  945. public List<Description1000> description1000 { get; set; }
  946. }
  947. public class Gracenote
  948. {
  949. public int season { get; set; }
  950. public int episode { get; set; }
  951. }
  952. public class MetadataPrograms
  953. {
  954. public Gracenote Gracenote { get; set; }
  955. }
  956. public class ContentRating
  957. {
  958. public string body { get; set; }
  959. public string code { get; set; }
  960. }
  961. public class Cast
  962. {
  963. public string billingOrder { get; set; }
  964. public string role { get; set; }
  965. public string nameId { get; set; }
  966. public string personId { get; set; }
  967. public string name { get; set; }
  968. public string characterName { get; set; }
  969. }
  970. public class Crew
  971. {
  972. public string billingOrder { get; set; }
  973. public string role { get; set; }
  974. public string nameId { get; set; }
  975. public string personId { get; set; }
  976. public string name { get; set; }
  977. }
  978. public class QualityRating
  979. {
  980. public string ratingsBody { get; set; }
  981. public string rating { get; set; }
  982. public string minRating { get; set; }
  983. public string maxRating { get; set; }
  984. public string increment { get; set; }
  985. }
  986. public class Movie
  987. {
  988. public string year { get; set; }
  989. public int duration { get; set; }
  990. public List<QualityRating> qualityRating { get; set; }
  991. }
  992. public class Recommendation
  993. {
  994. public string programID { get; set; }
  995. public string title120 { get; set; }
  996. }
  997. public class ProgramDetails
  998. {
  999. public string audience { get; set; }
  1000. public string programID { get; set; }
  1001. public List<Title> titles { get; set; }
  1002. public EventDetails eventDetails { get; set; }
  1003. public DescriptionsProgram descriptions { get; set; }
  1004. public string originalAirDate { get; set; }
  1005. public List<string> genres { get; set; }
  1006. public string episodeTitle150 { get; set; }
  1007. public List<MetadataPrograms> metadata { get; set; }
  1008. public List<ContentRating> contentRating { get; set; }
  1009. public List<Cast> cast { get; set; }
  1010. public List<Crew> crew { get; set; }
  1011. public string showType { get; set; }
  1012. public bool hasImageArtwork { get; set; }
  1013. public string images { get; set; }
  1014. public string imageID { get; set; }
  1015. public string md5 { get; set; }
  1016. public List<string> contentAdvisory { get; set; }
  1017. public Movie movie { get; set; }
  1018. public List<Recommendation> recommendations { get; set; }
  1019. }
  1020. public class Caption
  1021. {
  1022. public string content { get; set; }
  1023. public string lang { get; set; }
  1024. }
  1025. public class ImageData
  1026. {
  1027. public string width { get; set; }
  1028. public string height { get; set; }
  1029. public string uri { get; set; }
  1030. public string size { get; set; }
  1031. public string aspect { get; set; }
  1032. public string category { get; set; }
  1033. public string text { get; set; }
  1034. public string primary { get; set; }
  1035. public string tier { get; set; }
  1036. public Caption caption { get; set; }
  1037. }
  1038. public class ShowImages
  1039. {
  1040. public string programID { get; set; }
  1041. public List<ImageData> data { get; set; }
  1042. }
  1043. }
  1044. }
  1045. }