/[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/15 16:53:40	163
@@ -2,24 +2,33 @@
 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 FileInfo fi_XMLTVFILE;
+        static FileInfo fi_GBPVRDATABASE;
         static void CreateLog()
         {
             string log_path = Application.StartupPath;
@@ -78,34 +87,174 @@
             }
 
             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 (LAST_REPORTED_XMLTV_PROGRESS != e.Progress)
+            if (!XMLTV_INSTANCE_CREATED && LOADING_XMLTV_FILE)
             {
-                Console.WriteLine("XMLTV: {0} Loading: {1:00}%", fi_XMLTVFILE.Name, e.Progress);
-                LAST_REPORTED_XMLTV_PROGRESS = e.Progress;
+                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);
+                    }
+                }
             }
         }
         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_programs;
+        static List<SQLLITE.ICHANNEL> gbpvr_channels;
+        static List<SQLLITE.IRECORDING_SCHEDULE> gbpvr_recordings;
+        static List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs;
+        static void PerformDatabaseProcessing()
+        {
+            gbpvr_channels = sqlite.Channels;
+            gbpvr_programs = sqlite.Programs;
+            gbpvr_recordings = sqlite.Recordings;            
+            Type datatype;
+            var xmlt_raw_program_data = (IProgramDefinitionList)XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out datatype);
+            //xmltv_programs = xmlt_raw_program_data.ToList().OrderBy(p => p.ChannelNumber).ThenBy(p => DateTime.Parse(p.Start)).ToList();
+            //xmltv_programs = xmltv_programs.FindAll(p => p.Title == "Law & Order: Criminal Intent").ToList();
+            xmltv_programs = xmlt_raw_program_data.ToList().OrderBy(p => DateTime.Parse(p.Start)).ToList();
+
+
+            var gbpvr_programs_startdate_range = sqlite.GetProgramsDateRange(gbpvr_programs);
+            var xmltv_programs_startdate_range = GetXMLTVProgramStartDateRange(xmltv_programs);
+            gbpvr_programs = sqlite.FilterProgramsByDateRange(gbpvr_programs, xmltv_programs_startdate_range);
+            xmltv_programs = xmltv_programs.OrderBy(s => s.ChannelNumber).ThenBy(s => DateTime.Parse(s.Start)).ToList();
+            CheckScheduledRecordingsForDiscrepancies();
+            CheckProgramsForDiscrepancies();
+        }
+
+        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.Info.WriteLine("\tFirst: {0} = ({1})", first.ToString("yyyy/MM/dd HH:mm:ss.fffffff"), first.ToDateTimeString());
+            gLog.Info.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()
+        {
+            //gLog.Warn.WriteLine("CheckScheduledRecordingsForDiscrepancies() has not been implemented");
+            var manual_gbpvr_recordings = gbpvr_recordings.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()
+        {
+            gLog.Warn.WriteLine("CheckProgramsForDiscrepancies() has not been implemented");
+        }
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22