public static bool TryGetNowNext(string radioTimesId, out NowNextDetails nowNext) { DateTime guideTime = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.Local, GMT); string requestTime = new DateTime(guideTime.Year, guideTime.Month, guideTime.Day, guideTime.Hour, 0, 0).ToString("dd-MM-yyyy HH:mm:ss"); string url = string.Format(RADIOTIMES_JSON_URL, requestTime, radioTimesId); JObject guideData = WebCache.Instance.GetWebData <JObject>(url); nowNext = null; if (guideData == null) { return(false); } JArray channels = guideData["Channels"] as JArray; if (channels == null || channels.Count == 0) { return(false); } JArray listings = channels[0]["TvListings"] as JArray; if (listings == null || listings.Count == 0) { return(false); } foreach (JToken listing in listings) { //remove UTC indicator, the times don't appear to be UTC but GMT DateTime startTime = DateTime.Parse(listing.Value <string>("StartTimeMF").Replace("Z", ""), CultureInfo.InvariantCulture); DateTime endTime = DateTime.Parse(listing.Value <string>("EndTimeMF").Replace("Z", ""), CultureInfo.InvariantCulture); if (nowNext != null) { nowNext.NextTitle = listing.Value <string>("Title"); nowNext.NextDescription = listing.Value <string>("Description"); nowNext.NextStart = convertToLocalTime(startTime, GMT); nowNext.NextEnd = convertToLocalTime(endTime, GMT); break; } if (startTime <= guideTime && endTime > guideTime) { nowNext = new NowNextDetails { NowTitle = listing.Value <string>("Title"), NowDescription = listing.Value <string>("Description"), NowStart = convertToLocalTime(startTime, GMT), NowEnd = convertToLocalTime(endTime, GMT) }; } } return(nowNext != null); }
public static bool TryGetNowNextForChannel(string radioTimesId, out NowNextDetails nowNext) { nowNext = null; //Retrieve .dat page for channel string guide = WebCache.Instance.GetWebData(string.Format(RADIO_TIMES_URL, radioTimesId)); if (string.IsNullOrEmpty(guide)) { return(false); } TimeZoneInfo gmt = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time"); DateTime guideTime = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.Local, gmt); //split into individual programmes string[] programmes = guide.Split("\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); foreach (string programme in programmes) { string[] programmeInfo = programme.Split('~'); if (programmeInfo.Length != 23) //not a valid line { continue; } DateTime progDate = DateTime.Parse(programmeInfo[19], new CultureInfo("en-GB")); //uk date format DateTime progStartTime = progDate.Add(TimeSpan.Parse(programmeInfo[20])); DateTime progEndTime = progDate.Add(TimeSpan.Parse(programmeInfo[21])); //Programme might span 2 days, e.g starts at 23.50 and ends at 01.00 if (progEndTime < progStartTime) { progEndTime = progEndTime.AddDays(1); } if (nowNext != null) //if previous was current programme, we must be on next programme { nowNext.NextTitle = programmeInfo[0]; nowNext.NextStart = convertToLocalTime(progStartTime, gmt); nowNext.NextEnd = convertToLocalTime(progEndTime, gmt); break; //only get Now/Next } //if programme starts before current time and ends after current time it is currently playing if (progEndTime > guideTime && progStartTime <= guideTime) { nowNext = new NowNextDetails(); nowNext.NowTitle = programmeInfo[0]; nowNext.NowDescription = programmeInfo[17]; nowNext.NowStart = convertToLocalTime(progStartTime, gmt); nowNext.NowEnd = convertToLocalTime(progEndTime, gmt); } } return(nowNext != null); }
public static bool TryGetNowNext(string radioTimesId, out NowNextDetails nowNext) { DateTime guideTime = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.Local, GMT); string requestTime = new DateTime(guideTime.Year, guideTime.Month, guideTime.Day, guideTime.Hour, 0, 0).ToString("dd-MM-yyyy HH:mm:ss"); string url = string.Format(RADIOTIMES_JSON_URL, requestTime, radioTimesId); JObject guideData = WebCache.Instance.GetWebData<JObject>(url); nowNext = null; if (guideData == null) return false; JArray channels = guideData["Channels"] as JArray; if (channels == null || channels.Count == 0) return false; JArray listings = channels[0]["TvListings"] as JArray; if (listings == null || listings.Count == 0) return false; foreach (JToken listing in listings) { //remove UTC indicator, the times don't appear to be UTC but GMT DateTime startTime = DateTime.Parse(listing.Value<string>("StartTimeMF").Replace("Z", ""), CultureInfo.InvariantCulture); DateTime endTime = DateTime.Parse(listing.Value<string>("EndTimeMF").Replace("Z", ""), CultureInfo.InvariantCulture); if (nowNext != null) { nowNext.NextTitle = listing.Value<string>("Title"); nowNext.NextDescription = listing.Value<string>("Description"); nowNext.NextStart = convertToLocalTime(startTime, GMT); nowNext.NextEnd = convertToLocalTime(endTime, GMT); break; } if (startTime <= guideTime && endTime > guideTime) { nowNext = new NowNextDetails { NowTitle = listing.Value<string>("Title"), NowDescription = listing.Value<string>("Description"), NowStart = convertToLocalTime(startTime, GMT), NowEnd = convertToLocalTime(endTime, GMT) }; } } return nowNext != null; }
public static bool TryGetNowNextForChannel(string radioTimesId, out NowNextDetails nowNext) { nowNext = null; //Retrieve .dat page for channel string guide = WebCache.Instance.GetWebData(string.Format(RADIO_TIMES_URL, radioTimesId)); if (string.IsNullOrEmpty(guide)) return false; TimeZoneInfo gmt = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time"); DateTime guideTime = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.Local, gmt); //split into individual programmes string[] programmes = guide.Split("\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); foreach (string programme in programmes) { string[] programmeInfo = programme.Split('~'); if (programmeInfo.Length != 23) //not a valid line continue; DateTime progDate = DateTime.Parse(programmeInfo[19], new CultureInfo("en-GB")); //uk date format DateTime progStartTime = progDate.Add(TimeSpan.Parse(programmeInfo[20])); DateTime progEndTime = progDate.Add(TimeSpan.Parse(programmeInfo[21])); //Programme might span 2 days, e.g starts at 23.50 and ends at 01.00 if (progEndTime < progStartTime) progEndTime = progEndTime.AddDays(1); if (nowNext != null) //if previous was current programme, we must be on next programme { nowNext.NextTitle = programmeInfo[0]; nowNext.NextStart = convertToLocalTime(progStartTime, gmt); nowNext.NextEnd = convertToLocalTime(progEndTime, gmt); break; //only get Now/Next } //if programme starts before current time and ends after current time it is currently playing if (progEndTime > guideTime && progStartTime <= guideTime) { nowNext = new NowNextDetails(); nowNext.NowTitle = programmeInfo[0]; nowNext.NowDescription = programmeInfo[17]; nowNext.NowStart = convertToLocalTime(progStartTime, gmt); nowNext.NowEnd = convertToLocalTime(progEndTime, gmt); } } return nowNext != null; }
public static bool TryGetNowNext(string radioTimesId, out NowNextDetails nowNext) { DateTime guideTime = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.Local, GMT); string requestTime = new DateTime(guideTime.Year, guideTime.Month, guideTime.Day, guideTime.Hour, 0, 0).ToString("dd-MM-yyyy HH:mm:ss"); string url = string.Format(RADIOTIMES_JSON_URL, requestTime, radioTimesId); nowNext = null; JObject guideData = null; SecurityProtocolType oldSecurityProtocolType = ServicePointManager.SecurityProtocol; try { //EPG API requires SecurityProtocolType TLS 1.2 ServicePointManager.SecurityProtocol = oldSecurityProtocolType | (SecurityProtocolType)3072; guideData = WebCache.Instance.GetWebData <JObject>(url); } catch { Log.Warn("TVGuideGrabber: Error retrieving tv guide from {0}", url); return(false); } finally { ServicePointManager.SecurityProtocol = oldSecurityProtocolType; } if (guideData == null) { return(false); } JArray channels = guideData["Channels"] as JArray; if (channels == null || channels.Count == 0) { return(false); } JArray listings = channels[0]["TvListings"] as JArray; if (listings == null || listings.Count == 0) { return(false); } foreach (JToken listing in listings) { //remove UTC indicator, the times don't appear to be UTC but GMT DateTime startTime = DateTime.Parse(listing.Value <string>("StartTimeMF").Replace("Z", ""), CultureInfo.InvariantCulture); DateTime endTime = DateTime.Parse(listing.Value <string>("EndTimeMF").Replace("Z", ""), CultureInfo.InvariantCulture); if (nowNext != null) { nowNext.NextTitle = listing.Value <string>("Title"); nowNext.NextDescription = listing.Value <string>("Description"); nowNext.NextStart = convertToLocalTime(startTime, GMT); nowNext.NextEnd = convertToLocalTime(endTime, GMT); break; } if (startTime <= guideTime && endTime > guideTime) { nowNext = new NowNextDetails { NowTitle = listing.Value <string>("Title"), NowDescription = listing.Value <string>("Description"), NowStart = convertToLocalTime(startTime, GMT), NowEnd = convertToLocalTime(endTime, GMT) }; } } return(nowNext != null); }