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

Diff of /trunk/GBPVRProgramDatabaseFixer/Program.cs

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

--- trunk/GBPVRProgramDatabaseFixer/Program.cs	2013/03/16 16:03:19	174
+++ trunk/GBPVRProgramDatabaseFixer/Program.cs	2013/03/17 10:12:28	239
@@ -10,6 +10,8 @@
 using libxmltv.Core;
 using libxmltv.Interfaces;
 using libxmltv;
+using GBPVR.Public;
+using GBPVR.Backend.Common;
 namespace GBPVRProgramDatabaseFixer
 {
     class Program
@@ -27,7 +29,7 @@
         static bool SQLLIST_INSTANCE_CREATED = false;
         //static int LAST_REPORTED_SQLLITE_PROGRESS = 0;
 
-        static int LAST_REPORTED_PROGRESS = 0;
+        //static int LAST_REPORTED_PROGRESS = 0;
 
         static FileInfo fi_XMLTVFILE;
         static FileInfo fi_GBPVRDATABASE;
@@ -99,87 +101,18 @@
             Console.SetCursorPosition(0, Console.CursorTop - 1);
         }
 
-        static void gLog_ReportProgress(object sender, ReportProgressEventArgs e) 
+        static void gLog_ReportProgress(object sender, ReportProgressEventArgs e)
         {
-            //if (!XMLTV_INSTANCE_CREATED && LOADING_XMLTV_FILE)
-            //{
-            //    if (LAST_REPORTED_XMLTV_PROGRESS != e.Progress)
-            //    {
-            //        ConsoleOverwritePreviosLine("XMLTV: {0} Loading: {1:00}%", fi_XMLTVFILE.Name, e.Progress);
-            //        LAST_REPORTED_XMLTV_PROGRESS = e.Progress;
-            //        if (e.Progress == 100)
-            //        {
-            //            Console.WriteLine("XMLTV: {0} Loading: {1:00}%", fi_XMLTVFILE.Name, e.Progress);
-            //        }
-            //    }
-            //}
-            //if (!SQLLIST_INSTANCE_CREATED && LOADING_GBPVR_DATABASE)
-            //{
-            //    if (LAST_REPORTED_SQLLITE_PROGRESS != e.Progress)
-            //    {
-            //        ConsoleOverwritePreviosLine("SQLLITE: {0} Loading: {1:00}%", fi_XMLTVFILE.Name, e.Progress);
-            //        LAST_REPORTED_SQLLITE_PROGRESS = e.Progress;
-            //        if (e.Progress == 100)
-            //        {
-            //            Console.WriteLine("SQLLITE: {0} Loading: {1:00}%", fi_XMLTVFILE.Name, e.Progress);
-            //        }
-            //    }
-            //}
-
-            object k = e.UserState;
-            if (k == null)
+            object k = e.UserState; // we will assum that is a status message
+            if (k == null) // assume we do not have a user status message (ie: is only a progress message)
             {
-                if (sender == null)
-                {
-                    if (LAST_REPORTED_PROGRESS != e.Progress)
-                    {
-                        ConsoleOverwritePreviosLine("Progress: {0} ", e.Progress);
-                        LAST_REPORTED_PROGRESS = e.Progress;
-                        if (e.Progress == 100)
-                        {
-                            Console.WriteLine("Progress: {0} ", e.Progress);
-                        }
-                    }
-                }
-                else
-                {
-                    if (LAST_REPORTED_PROGRESS != e.Progress)
-                    {
-                        ConsoleOverwritePreviosLine("{0}: Progress: {1:00}%", sender.GetType().Name, e.Progress);
-                        LAST_REPORTED_PROGRESS = e.Progress;
-                        if (e.Progress == 100)
-                        {
-                            Console.WriteLine("{0}: Progress: {1:00}%", sender.GetType().Name, e.Progress);
-                        }
-                    }
-                }
+                if (sender == null) { ConsoleOverwritePreviosLine("Progress: {0} ", e.Progress); }
+                else { ConsoleOverwritePreviosLine("{0}: Progress: {1:00}%", sender.GetType().Name, e.Progress); }
             }
-            else
+            else // assume we have a user status message
             {
-                if (sender == null)
-                {
-                    if (LAST_REPORTED_PROGRESS != e.Progress)
-                    {
-                        ConsoleOverwritePreviosLine("Progress: {0} ", k.ToString());
-                        LAST_REPORTED_PROGRESS = e.Progress;
-                        if (e.Progress == 100)
-                        {
-                            Console.WriteLine("Progress: {0} ", k.ToString());
-                        }
-                    }
-                }
-                else
-                {
-                    if (LAST_REPORTED_PROGRESS != e.Progress)
-                    {
-                        ConsoleOverwritePreviosLine("{0}: {1}", sender.GetType().Name, k.ToString());
-                        LAST_REPORTED_PROGRESS = e.Progress;
-                        if (e.Progress == 100)
-                        {
-                            Console.WriteLine("{0}: {1}", sender.GetType().Name, k.ToString());
-                        }
-                    }
-                }
+                if (sender == null) { ConsoleOverwritePreviosLine("{0} ", k.ToString()); }
+                else { ConsoleOverwritePreviosLine("{0}: {1}", sender.GetType().Name, k.ToString()); }
             }
         }
         static void LoadXMLTVFile()
@@ -227,12 +160,6 @@
         }
         static void SQLLITE_OnInstanceCreated(object sender, EventArgs e) { SQLLIST_INSTANCE_CREATED = true; }
 
-
-        //static List<SQLLITE.IPROGRAMME> gbpvr_invalid_programs;
-        //static List<SQLLITE.IPROGRAMME> gbpvr_valid_programs;
-        //static List<SQLLITE.ICHANNEL> gbpvr_channels;
-        //static List<SQLLITE.IRECORDING_SCHEDULE> gbpvr_recordings;
-        //static List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs;
         static void PerformDatabaseProcessing()
         {
             var sqllite_channels = sqlite.Channels;
@@ -244,15 +171,14 @@
             var sqllite_programs_startdate_range = sqlite.GetProgramsDateRange(sqllite_programs);
             var xmltv_programs_startdate_range = GetXMLTVProgramStartDateRange(xmltv_programs);
             var gbpvr_invalid_programs = new List<SQLLITE.IPROGRAMME>();
-            var gbpvr_valid_programs = sqlite.FixGBPVRProgramsDatabase(sqllite_programs, xmltv_programs, out gbpvr_invalid_programs);
-            //xmltv_programs = xmltv_programs.OrderBy(s => s.ChannelNumber).ThenBy(s => DateTime.Parse(s.Start)).ToList();
-            //sqllite_channels.TrimExcess();
-            //gbpvr_invalid_programs.TrimExcess();
-            //gbpvr_valid_programs.TrimExcess();
-            //sqllite_programs.TrimExcess();
-            //xmltv_programs.TrimExcess();
-            //CheckScheduledRecordingsForDiscrepancies(sqllite_recordings);
-            //CheckProgramsForDiscrepancies(gbpvr_valid_programs, xmltv_programs);
+            var gbpvr_valid_programs = sqlite.FixGBPVRProgramsDatabase(sqllite_programs, xmltv_programs, out gbpvr_invalid_programs);     
+            sqlite.RemoveOldGBPVRPrograms(gbpvr_invalid_programs);
+            sqlite.UpdateGBPVRPrograms(gbpvr_valid_programs);
+
+            sqllite_programs.Clear();
+            gbpvr_valid_programs.ForEach(s=>sqllite_programs.Add(s.NewProgram));
+
+            CheckScheduledRecordingsForDiscrepancies(sqllite_recordings,sqllite_programs);
         }
 
         static IDateTimeRange GetXMLTVProgramStartDateRange(List<libxmltv.Interfaces.IProgramDefinition> programs)
@@ -269,63 +195,71 @@
             return range;
         }
 
-        static void CheckScheduledRecordingsForDiscrepancies(List<SQLLITE.IRECORDING_SCHEDULE> gbpvr)
+        static void CheckScheduledRecordingsForDiscrepancies(List<SQLLITE.IRECORDING_SCHEDULE> recordings, List<SQLLITE.IPROGRAMME> programs)
         {
-            ////gLog.Warn.WriteLine("CheckScheduledRecordingsForDiscrepancies() has not been implemented");
-            //var manual_gbpvr_recordings = gbpvr.FindAll(p => p.recording_type == 0).ToList();
+            List<SQLLITE.IRECORDING_SCHEDULE> recordings_to_delete = new List<SQLLITE.IRECORDING_SCHEDULE>();
+            //List<SQLLITE.IRECORDING_SCHEDULE> deleted_recordings = new List<SQLLITE.IRECORDING_SCHEDULE>();
+            //List<SQLLITE.IRECORDING_SCHEDULE> reocurring_recordings = new List<SQLLITE.IRECORDING_SCHEDULE>();
+
+
+            //reocurring_recordings = recordings.FindAll(s => s.recording_type != (long)RecordingType.TYPE_RECORD_ONCE).ToList();
+
+            foreach (var recording in recordings)
+            {
+                RecordingStatus status = (RecordingStatus)recording.status;
+                RecordingType type = (RecordingType)recording.recording_type;
+                if (type == RecordingType.TYPE_RECORD_SEASON)
+                {
+                    continue; // ignore TYPE_RECORD_SEASON
+                }
+                if (status != RecordingStatus.STATUS_PENDING)
+                {
+                    continue; // ignore any status except STATUS_PENDING
+                }
+                if (recording.recording_group == 0 && recording.programme_oid == 0)
+                {
+                    continue; // ignore manual recordings
+                }
+                // verify data for this recording
+                var recording_oid = recording.oid;
+                var programme_oid = recording.programme_oid;
+                var filename = recording.filename;
+                var manual_start_time = recording.manual_start_time;
+                var manual_end_time = recording.manual_end_time;
+                var manual_channel_oid = recording.manual_channel_oid;
+
+                // lookup program by oid
+                var entry = programs.Find(s => s.oid == programme_oid);
+                if (entry == null) 
+                {
+                    gLog.Warn.WriteLine("Unable to find program with oid: '{0}' for recording with oid: '{1}' -- assuming it should be deleted", programme_oid, recording_oid);
+                    //throw new NullReferenceException(string.Format("Unable to find program with oid: '{0}' for recording with oid: '{1}'", programme_oid, recording_oid)); 
+                    //deleted_recordings.Add(recording);
+                }
+                else
+                {
+                    recordings_to_delete.Add(recording);        
+                }
+            }
+            recordings_to_delete.TrimExcess();
+            if (recordings_to_delete.Count > 0)
+            {
+                gLog.Info.WriteLine("Found {0} recordings to delete.", recordings_to_delete.Count);
+                sqlite.DeleteGBPVRScheduledRecordings(recordings_to_delete);
+            }
+
+            try
+            {
 
-            //foreach (var manual_recording in manual_gbpvr_recordings)
-            //{
-            //    string title = manual_recording.filename;
-            //    DateTime start = manual_recording.manual_start_time;
-            //    DateTime end = manual_recording.manual_end_time;
-            //    long channel_oid = manual_recording.manual_channel_oid;
-            //    var found_channel = gbpvr_channels.Find(p => p.oid == channel_oid);
-            //    var channel_number = found_channel.channel_number;
-            //    var found_program = xmltv_programs.ToList().Find(p => 
-            //        p.Title.ToLower() == title.ToLower() && 
-            //        p.Start == start.ToDateTimeString() &&
-            //        p.Stop == end.ToDateTimeString() &&
-            //        p.ChannelNumber == channel_number
-            //        );
-            //    if (found_program != null)
-            //    {
-            //        gLog.Info.WriteLine("Recording might be valid: oid='{0}' programme_oid='{1}' title='{2}' start='{3}', end='{4}'", manual_recording.oid, manual_recording.programme_oid, title, start.ToDateTimeString(), end.ToDateTimeString());
-            //    }
-            //    else
-            //    {
-            //        gLog.Warn.WriteLine("Recording may not be valid: oid='{0}' programme_oid='{1}' title='{2}' start='{3}', end='{4}'", manual_recording.oid, manual_recording.programme_oid, title, start.ToDateTimeString(), end.ToDateTimeString());
-            //        // find the program on the channel at the start time
-            //        var correct_program = xmltv_programs.ToList().Find(p => 
-            //            p.ChannelNumber == channel_number &&
-            //            p.Start == start.ToDateTimeString()
-            //            );
-            //        if (correct_program != null)
-            //        {
-            //            gLog.Warn.WriteLine("\tThis will be recorded: {0}", correct_program.ToString());
-            //        }
-            //    }
-            //}
-        }
-
-        static void CheckProgramsForDiscrepancies(List<SQLLITE.IPROGRAMME> list_gbpvr, List<libxmltv.Interfaces.IProgramDefinition> list_xmltv)
-        {
-            ////gLog.Warn.WriteLine("CheckProgramsForDiscrepancies() has not been implemented");
-            //var gbpvr = list_gbpvr.ToArray();
-            //var xmltv = list_xmltv.ToArray();
-            ////if (gbpvr.Count() != xmltv.Count())
-            ////{
-            ////    throw new ArgumentOutOfRangeException("gbpvr.Count/xmltv.Count", string.Format("gbpvr.Count: '0x{0:x8}' != xmltv.Count: '0x{1:x8}'", gbpvr.Count(),xmltv.Count()));
-            ////}
-            ////for (int i = 0; i < gbpvr.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);
-            ////    }
-            ////}
+                // taken from: Config.EpgUpdateProgress.method_1()
+                Zap2itEpgReader.smethod_0();
+                GClass4.smethod_0().method_14();
+
+            }
+            catch (Exception ex)
+            {
+                gLog.Error.WriteLine(ex.ToString());
+            }
         }
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22