/[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/14 16:56:05	154
+++ trunk/GBPVRProgramDatabaseFixer/Program.cs	2013/03/17 06:53:55	233
@@ -2,24 +2,37 @@
 using System;
 using System.Collections.Generic;
 using System.Text;
+using System.Linq;
 using Enterprise.Logging;
 using System.Windows.Forms;
 using System.Diagnostics;
 using System.IO;
 using libxmltv.Core;
-
+using libxmltv.Interfaces;
+using libxmltv;
+using GBPVR.Public;
+using GBPVR.Backend.Common;
 namespace GBPVRProgramDatabaseFixer
 {
     class Program
     {
+        static ISQLLITE sqlite;
         static string XMLTV_FILE = "";
         static string GBPVR_DATABASE = "";
+
+        //static bool LOADING_XMLTV_FILE = false;
         static bool XMLTV_INSTANCE_CREATED = false;
+        //static int LAST_REPORTED_XMLTV_PROGRESS = 0;
 
-        static int LAST_REPORTED_XMLTV_PROGRESS =0;
 
-        static FileInfo fi_XMLTVFILE;
+        //static bool LOADING_GBPVR_DATABASE = false;
+        static bool SQLLIST_INSTANCE_CREATED = false;
+        //static int LAST_REPORTED_SQLLITE_PROGRESS = 0;
+
+        //static int LAST_REPORTED_PROGRESS = 0;
 
+        static FileInfo fi_XMLTVFILE;
+        static FileInfo fi_GBPVRDATABASE;
         static void CreateLog()
         {
             string log_path = Application.StartupPath;
@@ -78,34 +91,172 @@
             }
 
             LoadXMLTVFile();
+            LoadGBPVRDatabase();
+            PerformDatabaseProcessing();
+        }
+
+        static void ConsoleOverwritePreviosLine(string format, params object[] args)
+        {
+            Console.WriteLine(format,args);
+            Console.SetCursorPosition(0, Console.CursorTop - 1);
         }
 
-        static void gLog_ReportProgress(object sender, ReportProgressEventArgs e) 
+        static void gLog_ReportProgress(object sender, ReportProgressEventArgs e)
         {
-            if (LAST_REPORTED_XMLTV_PROGRESS != e.Progress)
+            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) { ConsoleOverwritePreviosLine("Progress: {0} ", e.Progress); }
+                else { ConsoleOverwritePreviosLine("{0}: Progress: {1:00}%", sender.GetType().Name, e.Progress); }
+            }
+            else // assume we have a user status message
             {
-                Console.WriteLine("XMLTV: {0} Loading: {1:00}%", fi_XMLTVFILE.Name, e.Progress);
-                LAST_REPORTED_XMLTV_PROGRESS = e.Progress;
+                if (sender == null) { ConsoleOverwritePreviosLine("{0} ", k.ToString()); }
+                else { ConsoleOverwritePreviosLine("{0}: {1}", sender.GetType().Name, k.ToString()); }
             }
         }
         static void LoadXMLTVFile()
         {
+            //LOADING_XMLTV_FILE = true;
             if (string.IsNullOrEmpty(XMLTV_FILE))
             {
                 gLog.Error.WriteLine("XMLTV File is either a null or empty string.");
+                //LOADING_XMLTV_FILE = false;
                 return;
             }
             fi_XMLTVFILE = new FileInfo(XMLTV_FILE);
             if (!fi_XMLTVFILE.Exists)
             {
                 gLog.Error.WriteLine("Could not find XMLTV File: {0}", fi_XMLTVFILE.FullName);
+                //LOADING_XMLTV_FILE = false;
                 return;
             }
 
             XMLTV.Create(fi_XMLTVFILE.FullName, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
             while (!XMLTV_INSTANCE_CREATED) { Application.DoEvents(); }
+            //LOADING_XMLTV_FILE = false;
         }
-
         static void XMLTV_OnInstanceCreated(object sender, EventArgs e) { XMLTV_INSTANCE_CREATED = true; }
+
+        static void LoadGBPVRDatabase()
+        {
+            //LOADING_GBPVR_DATABASE = true;
+            if (string.IsNullOrEmpty(GBPVR_DATABASE))
+            {
+                gLog.Error.WriteLine("GBPVR Datbase is either a null or empty string.");
+                //LOADING_GBPVR_DATABASE = false;
+                return;
+            }
+            fi_GBPVRDATABASE = new FileInfo(GBPVR_DATABASE);
+            if (!fi_GBPVRDATABASE.Exists)
+            {
+                gLog.Error.WriteLine("Could not find GBPVR Database: {0}", fi_GBPVRDATABASE.FullName);
+                //LOADING_GBPVR_DATABASE = false;
+                return;
+            }
+            sqlite = SQLLITE.Create(fi_GBPVRDATABASE.FullName, new EventHandler<EventArgs>(SQLLITE_OnInstanceCreated));
+            while (!SQLLIST_INSTANCE_CREATED) { Application.DoEvents(); }
+            //LOADING_GBPVR_DATABASE = false;
+        }
+        static void SQLLITE_OnInstanceCreated(object sender, EventArgs e) { SQLLIST_INSTANCE_CREATED = true; }
+
+        static void PerformDatabaseProcessing()
+        {
+            var sqllite_channels = sqlite.Channels;
+            var sqllite_recordings = sqlite.Recordings;
+            var sqllite_programs = sqlite.Programs;
+            Type datatype;
+            var xmlt_raw_program_data = (IProgramDefinitionList)XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out datatype);
+            var xmltv_programs = xmlt_raw_program_data.ToList().OrderBy(p => DateTime.Parse(p.Start)).ToList();
+            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);     
+            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)
+        {
+            IDateTimeRange range = DateTimeRange.Create();
+            var list = new List<libxmltv.Interfaces.IProgramDefinition>(programs.ToArray());
+            DateTime first = new DateTime();
+            DateTime last = new DateTime();
+            first = DateTime.Parse(list.OrderBy(s => DateTime.Parse(s.Start)).ToList().First().Start);
+            last = DateTime.Parse(list.OrderBy(s => DateTime.Parse(s.Start)).ToList().Last().Start);
+            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());
+            range = DateTimeRange.Create(first, last);
+            return range;
+        }
+
+        static void CheckScheduledRecordingsForDiscrepancies(List<SQLLITE.IRECORDING_SCHEDULE> recordings, List<SQLLITE.IPROGRAMME> programs)
+        {
+            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
+            {
+                IScheduleHelper helper = ScheduleHelper.getInstance();
+                helper.ForceRecordingScheduleReload();
+            }
+            catch (Exception ex)
+            {
+                gLog.Error.WriteLine(ex.ToString());
+            }
+        }
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22