using System; using System.Collections.Generic; using System.Linq; using System.Text; using libxmltv.Interfaces; using System.Xml.Linq; using System.Globalization; namespace libxmltv.Core { internal class XMLTVProgramCollection { private Dictionary entries = new Dictionary(); public XMLTVProgramCollection(object xmltv) { XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("Creating Instance of XMLTVProgramCollection"); IXMLTV_PARSER _xmltv; if (!Internals.VerifyInstance(xmltv, out _xmltv)) { return; } XMLTV_PARSER = _xmltv; Create(); } #region IXMLTVSource private IXMLTV_PARSER XMLTV_PARSER { get; set; } public Dictionary Collection { get { return entries; } } #endregion // private DateTime ParseDate(string timeStamp) { DateTime dt = new DateTime(); try { dt = DateTime.ParseExact(timeStamp, "yyyyMMddHHmmss zzzz", System.Globalization.CultureInfo.CurrentCulture); } catch (Exception ex) { throw ex; } return dt; } private void Create() { var doc = XMLTV_PARSER.XMLTV_LOADER.XmlDoc; int index = 0; foreach (var c in doc.Descendants(XMLTV_CONSTANTS.PROGRAM_ELEMENT)) { Program program = new Program(); program.Id = index; //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("program_Id: {0}", program.Id); if (c.HasAttributes) { var start = c.Attribute(XMLTV_CONSTANTS.Programs.ProgramStart).Value; program.Start = ParseDate(start); //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_start: {0}", start); var stop = c.Attribute(XMLTV_CONSTANTS.Programs.ProgramStop).Value; program.Stop = ParseDate(stop); //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_stop: {0}", stop); var channelid = c.Attribute(XMLTV_CONSTANTS.Programs.ProgramChannelId).Value; //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_channelid: {0}", channelid); IXMLTVChannel channel = new Channel(); try { channel = XMLTV_PARSER.Channels[channelid]; } catch (KeyNotFoundException) { XMLTV_LOGGER.Log.Verbose.Error.WriteLine(string.Format("Unable to find Channel by id: '{0}' for this program.", channelid)); } program.Channel = channel; //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_channel: {0}", program.Channel.ToString()); } try { var title = c.Descendants(XMLTV_CONSTANTS.Programs.ProgramTitle).FirstOrDefault().Value; program.Title = title; } catch (Exception) { program.Title = string.Empty; } //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_title: {0}", program.Title == string.Empty ? "empty" : program.Title); try { var subtitle = c.Descendants(XMLTV_CONSTANTS.Programs.ProgramSubTitle).FirstOrDefault().Value; program.SubTitle = subtitle; } catch (Exception) { program.SubTitle = string.Empty; } //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_subtitle: {0}", program.SubTitle == string.Empty ? "empty" : program.SubTitle); try { var description = c.Descendants(XMLTV_CONSTANTS.Programs.ProgramDescription).FirstOrDefault().Value; program.Description = description; } catch (Exception) { program.Description = string.Empty; } //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_description: {0}", program.Description == string.Empty ? "empty" : program.Description); entries.Add(program.Id, program); index++; } } public override string ToString() { //return string.Format("XmlTv Source: '{0}' (Generated by: '{1}') (support: '{2}')", SourceName, GeneratorName, GeneratorUrl); return string.Empty; } } internal class Program : IXMLTVProgram { public Program() { Id = 0; Start = new DateTime(); Stop = new DateTime(); Channel = new Channel(); Title = string.Empty; SubTitle = string.Empty; Description = string.Empty; } #region IXMLTVProgram members public int Id { get; set; } public DateTime Start { get; set; } public DateTime Stop { get; set; } public IXMLTVChannel Channel { get; set; } public string Title { get; set; } public string SubTitle { get; set; } public string Description { get; set; } #endregion public override string ToString() { return string.Format("{0}: {1} - {2} ({3}) ['{4}' <==> '{5}']", Id, Title, SubTitle, Channel.ToString(), Start.ToString("yyyy/MM/dd hh:mm tt"), Stop.ToString("yyyy/MM/dd hh:mm tt")); } } }