/[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:24:37	150
+++ trunk/GBPVRProgramDatabaseFixer/Program.cs	2013/03/16 16:03:19	174
@@ -1,13 +1,331 @@
-´╗┐using System;
+´╗┐#define OVERRIDE_COMMANDLINE_ARGUMENTS // when defined will override supplied commandline args
+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 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;
+            string log_filename = string.Format("{0}.log", typeof(Program).Assembly.GetName().Name);
+            gLog.CreateLog(string.Format(@"{0}\{1}", log_path, log_filename), false, LogLevel.kLogLevel_All);
+#if DEBUG
+            LogLevel gLevel = gLog.LogLevel;
+            gLevel |= LogLevel.kLogLevel_VerboseDebug;
+            gLevel |= LogLevel.kLogLevel_Debug;
+            gLog.SetLogLevel(gLevel);
+#else
+            LogLevel gLevel = LogLevel.kLogLevel_Default; // set the default log level: Info, Warn, Error, Debug
+            // it is OK for kLogLevel_Debug to be set in Release mode ... must of the chatty messages are from kLogLevel_VerboseDebug
+            gLevel &= ~LogLevel.kLogLevel_Debug; // ensure this is not set, ever in release mode         
+            gLevel &= ~LogLevel.kLogLevel_VerboseDebug; // ensure this is not set, ever in release mode         
+            gLog.SetLogLevel(gLevel);
+#endif
+            gLog.ReportProgressEvent += new EventHandler<ReportProgressEventArgs>(gLog_ReportProgress);
+        }
+
+        [Conditional("OVERRIDE_COMMANDLINE_ARGUMENTS")]
+        static void CreateDebugCommandlineArgs(ref List<string> cargs)
+        {
+           cargs = new List<string>();
+            cargs.Add(string.Format("--xmltv_file=\"{0}\"", @"c:\EPG Guide\Guide\tvguide.xml"));
+            cargs.Add(string.Format("--gbprv_db=\"{0}\"", @"c:\Program Files (x86)\Devnz\GBPVR\gbpvr.db3"));
+        }
+
+        static List<string> CreateCommandlineArgs(string[] args)
+        {
+            List<string> cargs = new List<string>(args);
+            CreateDebugCommandlineArgs(ref cargs);
+            return cargs;
+        }
+
         static void Main(string[] args)
         {
+            CreateLog();
+            var cargs = CreateCommandlineArgs(args);
+            foreach (var carg in cargs)
+            {
+                if (carg.ToLower().Contains("--xmltv_file="))
+                {
+                    XMLTV_FILE = carg.Replace("--xmltv_file=", "").Replace("\"","");
+                    gLog.Info.WriteLine("XMLTV FILE: '{0}'", XMLTV_FILE);
+                }
+                else if (carg.ToLower().Contains("--gbprv_db="))
+                {
+                    GBPVR_DATABASE = carg.Replace("--gbprv_db=", "").Replace("\"", "");
+                    gLog.Info.WriteLine("GBPVR DATABASE: '{0}'", GBPVR_DATABASE);
+                }
+                else
+                {
+                    gLog.Warn.WriteLine("Unknown commandline option: {0}", carg);
+                }
+            }
+
+            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) 
+        {
+            //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)
+            {
+                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);
+                        }
+                    }
+                }
+            }
+            else
+            {
+                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());
+                        }
+                    }
+                }
+            }
+        }
+        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 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;
+            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);
+            //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);
+        }
+
+        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)
+        {
+            ////gLog.Warn.WriteLine("CheckScheduledRecordingsForDiscrepancies() has not been implemented");
+            //var manual_gbpvr_recordings = gbpvr.FindAll(p => p.recording_type == 0).ToList();
+
+            //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);
+            ////    }
+            ////}
         }
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22