/[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 05:35:32	232
@@ -2,24 +2,35 @@
 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;
 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 +89,109 @@
             }
 
             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);
+
+        }
+
+        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> gbpvr)
+        {
+        }
+
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22