ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/xmltv_parser/trunk/libxmltv/Core/XMLTVProgramCollection.cs
(Generate patch)

Comparing trunk/libxmltv/Core/XMLTVProgramCollection.cs (file contents):
Revision 27 by william, Thu Mar 7 12:11:41 2013 UTC vs.
Revision 30 by william, Thu Mar 7 13:36:42 2013 UTC

--- trunk/libxmltv/Core/XMLTVProgramCollection.cs	2013/03/07 12:11:41	27
+++ trunk/libxmltv/Core/XMLTVProgramCollection.cs	2013/03/07 13:36:42	30
@@ -4,6 +4,7 @@ using System.Linq;
 using System.Text;
 using libxmltv.Interfaces;
 using System.Xml.Linq;
+using System.Globalization;
 
 namespace libxmltv.Core
 {
@@ -12,7 +13,7 @@ namespace libxmltv.Core
         private Dictionary<int, IXMLTVProgram> entries = new Dictionary<int, IXMLTVProgram>();
         public XMLTVProgramCollection(object xmltv)
         {
-            XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("Creating Instance of XMLTVChannelCollection");
+            XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("Creating Instance of XMLTVProgramCollection");
             IXMLTV_PARSER _xmltv;
             if (!Internals.VerifyInstance<IXMLTV_PARSER>(xmltv, out _xmltv)) { return; }
             XMLTV_PARSER = _xmltv;
@@ -27,14 +28,73 @@ namespace libxmltv.Core
         }
         #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(c, index);
-                entries.Add(program.ProgramId, program);
+                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++;
             }
         }
@@ -49,16 +109,26 @@ namespace libxmltv.Core
     {
         public Program()
         {
-            ProgramId = 0;
-        }
-        public Program(XElement e, int index)
-            : this()
-        {
-            //ProgramId = e.Attribute(XMLTV_CONSTANTS.Programs.ChannelStart).Value;
-            ProgramId = index;
+            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 ProgramId { get; private set; }
+        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"));
+        }
     }
 }