/[xmltv_parser]/trunk/GBPVRProgramDatabaseFixer/SQLLITE.cs
ViewVC logotype

Diff of /trunk/GBPVRProgramDatabaseFixer/SQLLITE.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/GBPVRProgramDatabaseFixer/SQLLITE.cs	2013/03/14 19:36:01	157
+++ trunk/GBPVRProgramDatabaseFixer/SQLLITE.cs	2013/03/16 14:28:56	170
@@ -6,12 +6,59 @@
 using System.Data.SQLite;
 using System.Diagnostics;
 using Enterprise.Logging;
-
+using libxmltv;
+using libxmltv.Interfaces;
+using libxmltv.Core;
 namespace GBPVRProgramDatabaseFixer
 {
-    internal class SQLLITE
+    public interface ISQLLITE
+    {
+        List<SQLLITE.IPROGRAMME> Programs { get; }
+        List<SQLLITE.IRECORDING_SCHEDULE> Recordings { get; }
+        List<SQLLITE.ICHANNEL> Channels { get; }
+        IDateTimeRange GetProgramsDateRange(List<SQLLITE.IPROGRAMME> programs);
+        List<SQLLITE.IPROGRAMME> CreateUpdatedProgramsList(List<SQLLITE.IPROGRAMME> gbpvr_programs, List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs, out List<SQLLITE.IPROGRAMME> removed_programs);
+
+    }
+    public class SQLLITE : ISQLLITE
     {
+        public static ISQLLITE Create(string database, EventHandler<EventArgs> OnInstanceCreated)
+        {
+            return new SQLLITE(database, OnInstanceCreated);
+        }
+
         #region DATABASE DEFINITIONS
+
+        public interface ICHANNEL
+        {
+            Int64 oid { get; }
+            String name { get; }
+            String channelID { get; }
+            Int64 channel_number { get; }
+            String favourite_channel { get; }
+            String display_name { get; }
+        }
+        private class CHANNEL : ICHANNEL
+        {
+            public CHANNEL() 
+            {
+                BaseDatabaseDefinition<CHANNEL>.CreateDefault(this); 
+            }
+            //public RECORDING_SCHEDULE(SQLiteDataReader r, int index) { BaseDatabaseDefinition<RECORDING_SCHEDULE>.Create(this, r, index); }
+
+            public static void Create(ref CHANNEL instance, SQLiteDataReader r, int index)
+            {
+                BaseDatabaseDefinition<CHANNEL>.Create(ref instance, r, index);
+            }
+            #region ICHANNEL members
+            public Int64 oid { get; set; }
+            public String name { get; set; }
+            public String channelID { get; set; }
+            public Int64 channel_number { get; set; }
+            public String favourite_channel { get; set; }
+            public String display_name { get; set; }
+            #endregion
+        }
         public interface IRECORDING_SCHEDULE
         {
             Int64 oid { get; }
@@ -144,15 +191,17 @@
 
         public interface IPROGRAMME
         {
-            Int64 oid { get; }
-            String name { get; }
-            String sub_title { get; }
-            String description { get; }
-            DateTime start_time { get; }
-            DateTime end_time { get; }
-            Int64 channel_oid { get; }
-            String unique_identifier { get; }
-            String rating { get; }
+            Int64 oid { get; set; }
+            String name { get; set; }
+            String sub_title { get; set; }
+            String description { get; set; }
+            DateTime start_time { get; set; }
+            DateTime end_time { get; set; }
+            Int64 channel_oid { get; set; }
+            String unique_identifier { get; set; }
+            String rating { get; set; }
+
+            IProgramDefinition AsXMLTVProgramDefinition(ISQLLITE sqllite);
         }
         private class PROGRAMME : IPROGRAMME
         {
@@ -175,6 +224,19 @@
             public Int64 channel_oid { get; set; }
             public String unique_identifier { get; set; }
             public String rating { get; set; }
+            public IProgramDefinition AsXMLTVProgramDefinition(ISQLLITE sqllite)
+            {
+                ProgramList.ProgramDefintion definition = new ProgramList.ProgramDefintion();
+                var channel = sqllite.Channels.Find(s => s.oid == this.channel_oid);
+                definition.ChannelName = channel.display_name;
+                definition.ChannelNumber = (int)channel.channel_number;
+                definition.Description = this.description;
+                definition.Start = this.start_time.ToDateTimeString();
+                definition.Stop = this.end_time.ToDateTimeString();
+                definition.SubTitle = this.sub_title;
+                definition.Title = this.name;
+                return definition;
+            }
             #endregion
         }
         #endregion
@@ -182,10 +244,11 @@
         {
             public const string RECORDING_SCHEDULE = "RECORDING_SCHEDULE";
             public const string PROGRAMME = "PROGRAMME";
+            public const string CHANNEL = "CHANNEL";
         }
 
         //public SQLLite() { }
-        public SQLLITE(string database, EventHandler<EventArgs> OnInstanceCreated) 
+        protected SQLLITE(string database, EventHandler<EventArgs> OnInstanceCreated) 
         {
             this.OnInstanceCreated = OnInstanceCreated;
             //CreateConnection(database);
@@ -196,6 +259,7 @@
                 return;
             }
             ConnectionTest();
+            ReadChannelData();
             ReadRecodringScheduleData();
             ReadProgrammeData();
             OnCreatedInstance(this, new EventArgs());
@@ -210,10 +274,102 @@
             if (OnInstanceCreated != null) { OnInstanceCreated.Invoke(sender, e); }
         }
 
-        #region SQLLite 
         private string Database;
+        #region ISQLLITE members        
         public List<IPROGRAMME> Programs { get; private set; }
         public List<IRECORDING_SCHEDULE> Recordings { get; private set; }
+        public List<ICHANNEL> Channels { get; private set; }
+
+        public IDateTimeRange GetProgramsDateRange(List<SQLLITE.IPROGRAMME> programs)
+        {
+            var list = new List<IPROGRAMME>(programs.ToArray());
+            DateTime first = new DateTime();
+            DateTime last = new DateTime();
+            first = list.OrderBy(s => s.start_time).ToList().First().start_time;            
+            last = list.OrderBy(s => s.start_time).ToList().Last().start_time;
+            gLog.Verbose.Debug.WriteLine("\tFirst: {0} = ({1})", first.ToString("yyyy/MM/dd HH:mm:ss.fffffff"), first.ToDateTimeString());
+            gLog.Verbose.Debug.WriteLine("\tLast: {0} = ({1})", last.ToString("yyyy/MM/dd HH:mm:ss.fffffff"), last.ToDateTimeString());
+            var range = DateTimeRange.Create(first, last);
+            return range;
+        }
+
+        public List<SQLLITE.IPROGRAMME> CreateUpdatedProgramsList(List<SQLLITE.IPROGRAMME> gbpvr_programs, List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs, out List<SQLLITE.IPROGRAMME> source_invalid)
+        {
+            source_invalid = new List<IPROGRAMME>();
+            gbpvr_programs = gbpvr_programs.OrderBy(s => s.start_time).ToList();            
+            List<SQLLITE.IPROGRAMME> source_valid = new List<IPROGRAMME>();
+            //if (range == null)
+            //{
+            //    gLog.Warn.WriteLine("The DateTimeRange passed in is null...returning the original program list");
+            //    return list;
+            //}
+            //gLog.Warn.WriteLine("FilterProgramsByDateRange has not been implemented");
+            
+            double total = gbpvr_programs.Count;
+            double index = 0;
+            double progress = 0;
+            foreach (var program in gbpvr_programs)
+            {  
+                progress = 100.0 * (index / total);
+                gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Filtering GBPVR Programs: {0:00}%", (int)progress)));                
+                var channel_oid = program.channel_oid;
+                var channel = this.Channels.Find(s => s.oid == channel_oid);
+                var start_date = program.start_time;
+                var xmltv_entry_list = xmltv_programs.FindAll(s => s.ChannelNumber == channel.channel_number && s.Start == start_date.ToDateTimeString());
+                IProgramDefinition xmltv_entry = null;
+                if (xmltv_entry_list.Count > 0)
+                {
+                    gLog.Error.WriteLine("Found more than one entry: Matching channel='{0}' and start='{1}'", channel.channel_number, start_date.ToDateTimeString());
+                    if (Debugger.IsAttached)
+                    {
+                        Debugger.Break();
+                    }
+                }
+                else
+                {
+                    xmltv_entry = xmltv_entry_list.FirstOrDefault();
+                }
+
+                if (xmltv_entry == null)
+                {
+                    gLog.Warn.WriteLine("Invalidating GB-PVR Program: oid='{0}' channel_oid='{1}' title='{2}' start='{3}'", program.oid, program.channel_oid, program.name, program.start_time.ToDateTimeString());
+                    source_invalid.Add(program);
+                }
+                else
+                {
+                    var updated_program = program;
+                    updated_program.description = xmltv_entry.Description;
+                    updated_program.end_time = DateTime.Parse(xmltv_entry.Stop);
+                    updated_program.name = xmltv_entry.Title;
+                    updated_program.start_time = DateTime.Parse(xmltv_entry.Start);                    
+                    updated_program.sub_title = xmltv_entry.SubTitle;
+                    gLog.Warn.WriteLine("Updating GB-PVR Program: oid='{0}' channel_oid='{1}' title='{2}' subtitle='{3}' start='{4}'", program.oid, program.channel_oid, program.name,program.sub_title, program.start_time.ToDateTimeString());
+                    gLog.Warn.WriteLine("    Updated with Program: title='{0}' subtitle='{1}' start='{2}'", updated_program.name,updated_program.sub_title, program.start_time.ToDateTimeString());
+                    source_valid.Add(updated_program);
+                }
+                index++;
+            }
+
+            //for (int i = 0; i < programs.Count(); i++)
+            //{
+            //    var gbpvr_entry = gbpvr[i];
+            //    var xmltv_entry = gbpvr_entry.AsXMLTVProgramDefinition(sqlite);
+            //    if (!xmltv[i].Equals(xmltv_entry))
+            //    {
+            //        gLog.Warn.WriteLine("Warning GBPVR Program oid: {0} might be invalid", gbpvr_entry.oid);
+            //    }
+            //}
+
+            source_valid = source_valid.OrderBy(s => s.channel_oid).ThenBy(s => s.start_time).ToList();
+            source_invalid = source_invalid.OrderBy(s => s.channel_oid).ThenBy(s => s.start_time).ToList();
+
+            gLog.Info.WriteLine("Total XMLTV Programs: 0x{0:x8}", xmltv_programs.Count);
+            gLog.Info.WriteLine("Updated: 0x{0:x8} GB-PVR Programs", source_valid.Count);
+            gLog.Info.WriteLine("Removed: 0x{0:x8} GB-PVR Programs", source_invalid.Count);
+            gLog.Info.WriteLine("Total GB-PVR Programs (Updated & Removed): 0x{0:x8}", source_valid.Count + source_invalid.Count);
+
+            return source_valid;
+        }
         #endregion
 
 
@@ -233,7 +389,7 @@
                 using (SQLiteConnection con = CreateConnection())
                 {
                     con.Open();
-                    string command_text = string.Format("select * from {0};", TABLES.PROGRAMME);
+                    string command_text = string.Format("select * from {0};", TABLES.CHANNEL);
                     gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
                     using (SQLiteCommand cmd = new SQLiteCommand(command_text, con))
                     {
@@ -288,7 +444,47 @@
                 return false;
             }
         }
-
+        private void ReadChannelData()
+        {
+            try
+            {
+                List<ICHANNEL> channels = new List<ICHANNEL>();
+                using (SQLiteConnection con = CreateConnection())
+                {
+                    con.Open();
+                    string command_text = string.Format("select * from {0};", TABLES.CHANNEL);
+                    gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
+                    using (SQLiteCommand cmd = new SQLiteCommand(command_text, con))
+                    {
+                        using (SQLiteDataReader r = cmd.ExecuteReader())
+                        {
+                            if (!r.HasRows)
+                            {
+                                gLog.Warn.WriteLine("Query: '{0}' returned no rows.", cmd.CommandText);
+                            }
+                            else
+                            {
+                                while (r.Read())
+                                {
+                                    CHANNEL channel = new CHANNEL();
+                                    for (int i = 0; i < r.FieldCount; i++)
+                                    {
+                                        CHANNEL.Create(ref channel, r, i);
+                                    }
+                                    channels.Add(channel);
+                                }
+                            }
+                        }
+                    }
+                    con.Clone();
+                }
+                this.Channels = channels;
+            }
+            catch (Exception ex)
+            {
+                gLog.Error.WriteLine(ex.ToString());
+            }
+        }
         private void ReadProgrammeData()
         {
             try
@@ -357,7 +553,6 @@
                                     {
                                         RECORDING_SCHEDULE.Create(ref recording, r, i);
                                     }
-
                                     recordings.Add(recording);
                                 }
                             }
@@ -372,5 +567,6 @@
                 gLog.Error.WriteLine(ex.ToString());
             }
         }
+
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22