/// <summary> /// Filters the specified list of programmes and orders them by airdate. /// </summary> /// <param name="programmes">The full extracted list of programmes.</param> /// <param name="config">The configuration.</param> /// <returns> /// List of filtered and ordered programmes. /// </returns> public IEnumerable<XMLTVProgramme> Filter(IEnumerable<XMLTVProgramme> programmes, XMLTVConfiguration config) { var regexes = new Dictionary<Regex, TVShow>(); foreach (var show in Database.TVShows) { regexes.Add(new Regex(@"(^|:\s+)" + Regex.Escape(show.Value.Name) + @"(?!(?:[:,0-9]| \- |\s*[a-z&]))", RegexOptions.IgnoreCase), show.Value); if (!string.IsNullOrWhiteSpace(config.Language) && config.Language.Length == 2) { var foreign = show.Value.GetForeignTitle(config.Language); if (!string.IsNullOrWhiteSpace(foreign)) { regexes.Add(new Regex(@"(^|:\s+)" + Regex.Escape(foreign.RemoveDiacritics()) + @"(?!(?:[:,0-9]| \- |\s*[a-z&]))", RegexOptions.IgnoreCase), show.Value); } } } foreach (var prog in programmes.OrderBy(p => p.Airdate)) { foreach (var regex in regexes) { if (regex.Key.IsMatch(prog.Name.RemoveDiacritics())) { prog.ShowID = regex.Value.ID; yield return prog; break; } } } }
/// <summary> /// Parses the specified XMLTV file. /// </summary> /// <param name="config">The configuration.</param> /// <returns> /// List of extracted programmes. /// </returns> public IEnumerable <XMLTVProgramme> ParseFile(XMLTVConfiguration config) { var channels = new Dictionary <string, string[]>(); var document = XDocument.Load(config.File); foreach (var channel in document.Descendants("channel")) { var id = channel.Attribute("id"); var name = channel.Descendants("display-name").ToList(); var url = channel.Descendants("url").ToList(); if (id == null || !name.Any()) { continue; } channels.Add(id.Value, new[] { name[0].Value, url.Any() ? url.Last().Value : null }); } foreach (var programme in document.Descendants("programme")) { var channel = programme.Attribute("channel"); var start = programme.Attribute("start"); var title = programme.Descendants("title").ToList(); var descr = programme.Descendants("desc").ToList(); if (channel == null || start == null || !title.Any()) { continue; } DateTime airdate; if (!DateTime.TryParseExact(start.Value, "yyyyMMddHHmmss zzz", CultureInfo.InvariantCulture, DateTimeStyles.None, out airdate)) { continue; } var prog = new XMLTVProgramme { Channel = channels[channel.Value][0], Name = title[0].Value.Trim(), Airdate = airdate, URL = channels[channel.Value][1] }; if (descr.Any()) { prog.Description = descr[0].Value.Trim(); } yield return(prog); } }
/// <summary> /// Parses the specified XMLTV file. /// </summary> /// <param name="config">The configuration.</param> /// <returns> /// List of extracted programmes. /// </returns> public IEnumerable<XMLTVProgramme> ParseFile(XMLTVConfiguration config) { var channels = new Dictionary<string, string[]>(); var document = XDocument.Load(config.File); foreach (var channel in document.Descendants("channel")) { var id = channel.Attribute("id"); var name = channel.Descendants("display-name").ToList(); var url = channel.Descendants("url").ToList(); if (id == null || !name.Any()) { continue; } channels.Add(id.Value, new[] { name[0].Value, url.Any() ? url.Last().Value : null }); } foreach (var programme in document.Descendants("programme")) { var channel = programme.Attribute("channel"); var start = programme.Attribute("start"); var title = programme.Descendants("title").ToList(); var descr = programme.Descendants("desc").ToList(); if (channel == null || start == null || !title.Any()) { continue; } DateTime airdate; if (!DateTime.TryParseExact(start.Value, "yyyyMMddHHmmss zzz", CultureInfo.InvariantCulture, DateTimeStyles.None, out airdate)) { continue; } var prog = new XMLTVProgramme { Channel = channels[channel.Value][0], Name = title[0].Value.Trim(), Airdate = airdate, URL = channels[channel.Value][1] }; if (descr.Any()) { prog.Description = descr[0].Value.Trim(); } yield return prog; } }
/// <summary> /// Filters the specified list of programmes and orders them by airdate. /// </summary> /// <param name="programmes">The full extracted list of programmes.</param> /// <param name="config">The configuration.</param> /// <returns> /// List of filtered and ordered programmes. /// </returns> public IEnumerable <XMLTVProgramme> Filter(IEnumerable <XMLTVProgramme> programmes, XMLTVConfiguration config) { var regexes = new Dictionary <Regex, TVShow>(); foreach (var show in Database.TVShows) { regexes.Add(new Regex(@"(^|:\s+)" + Regex.Escape(show.Value.Title) + @"(?!(?:[:,0-9]| \- |\s*[a-z&]))", RegexOptions.IgnoreCase), show.Value); if (!string.IsNullOrWhiteSpace(config.Language) && config.Language.Length == 2) { var foreign = show.Value.GetForeignTitle(config.Language); if (!string.IsNullOrWhiteSpace(foreign)) { regexes.Add(new Regex(@"(^|:\s+)" + Regex.Escape(foreign.RemoveDiacritics()) + @"(?!(?:[:,0-9]| \- |\s*[a-z&]))", RegexOptions.IgnoreCase), show.Value); } } } foreach (var prog in programmes.OrderBy(p => p.Airdate)) { foreach (var regex in regexes) { if (regex.Key.IsMatch(prog.Name.RemoveDiacritics())) { prog.ShowID = regex.Value.ID; yield return(prog); break; } } } }