/[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

revision 174 by william, Sat Mar 16 16:03:19 2013 UTC revision 251 by william, Sat Mar 30 04:46:15 2013 UTC
# Line 1  Line 1 
1  #define OVERRIDE_COMMANDLINE_ARGUMENTS // when defined will override supplied commandline args  #define OVERRIDE_COMMANDLINE_ARGUMENTS // when defined will override supplied commandline args
2    //#define ENABLE_SCHEDULDED_RECORDINGS_FIXUP // when defined will enable calls to CheckScheduledRecordingsForDiscrepancies
3    
4  using System;  using System;
5  using System.Collections.Generic;  using System.Collections.Generic;
6  using System.Text;  using System.Text;
# Line 10  using System.IO; Line 12  using System.IO;
12  using libxmltv.Core;  using libxmltv.Core;
13  using libxmltv.Interfaces;  using libxmltv.Interfaces;
14  using libxmltv;  using libxmltv;
15    using GBPVR.Backend.Common;
16  namespace GBPVRProgramDatabaseFixer  namespace GBPVRProgramDatabaseFixer
17  {  {
18      class Program      class Program
19      {      {
20            const long MAX_LOG_FILESIZE_IN_BYTES = 250000; //250kb max
21            static void TruncateLogFile(FileInfo log_file)
22            {
23                if (!log_file.Exists)
24                {
25                    gLog.CreateLog(log_file.FullName, false, LogLevel.kLogLevel_All);
26                }
27                else
28                {
29                    if (log_file.Length > MAX_LOG_FILESIZE_IN_BYTES)
30                    {
31                        string match = string.Format("{0}.*", log_file.Name);
32                        var files = Directory.GetFiles(log_file.Directory.FullName, match).ToList();
33                        files.RemoveAt(0);
34                        files.TrimExcess();
35                        int count = files.Count;
36                        if (log_file.Exists)
37                        {
38                            File.Copy(log_file.FullName, string.Format("{0}.{1}", log_file.FullName, count));
39                        }
40                        gLog.CreateLog(log_file.FullName, true, LogLevel.kLogLevel_All);
41                    }
42                    else
43                    {
44                        gLog.CreateLog(log_file.FullName, false, LogLevel.kLogLevel_All);
45                    }
46                }
47            }
48    
49          static ISQLLITE sqlite;          static ISQLLITE sqlite;
50          static string XMLTV_FILE = "";          static string XMLTV_FILE = "";
51          static string GBPVR_DATABASE = "";          static string GBPVR_DATABASE = "";
# Line 27  namespace GBPVRProgramDatabaseFixer Line 59  namespace GBPVRProgramDatabaseFixer
59          static bool SQLLIST_INSTANCE_CREATED = false;          static bool SQLLIST_INSTANCE_CREATED = false;
60          //static int LAST_REPORTED_SQLLITE_PROGRESS = 0;          //static int LAST_REPORTED_SQLLITE_PROGRESS = 0;
61    
62          static int LAST_REPORTED_PROGRESS = 0;          //static int LAST_REPORTED_PROGRESS = 0;
63    
64          static FileInfo fi_XMLTVFILE;          static FileInfo fi_XMLTVFILE;
65          static FileInfo fi_GBPVRDATABASE;          static FileInfo fi_GBPVRDATABASE;
# Line 35  namespace GBPVRProgramDatabaseFixer Line 67  namespace GBPVRProgramDatabaseFixer
67          {          {
68              string log_path = Application.StartupPath;              string log_path = Application.StartupPath;
69              string log_filename = string.Format("{0}.log", typeof(Program).Assembly.GetName().Name);              string log_filename = string.Format("{0}.log", typeof(Program).Assembly.GetName().Name);
70              gLog.CreateLog(string.Format(@"{0}\{1}", log_path, log_filename), false, LogLevel.kLogLevel_All);              FileInfo log_file = new FileInfo(string.Format(@"{0}\{1}", log_path, log_filename));
71                TruncateLogFile(log_file);
72  #if DEBUG  #if DEBUG
73              LogLevel gLevel = gLog.LogLevel;              LogLevel gLevel = gLog.LogLevel;
74              gLevel |= LogLevel.kLogLevel_VerboseDebug;              gLevel |= LogLevel.kLogLevel_VerboseDebug;
# Line 99  namespace GBPVRProgramDatabaseFixer Line 132  namespace GBPVRProgramDatabaseFixer
132              Console.SetCursorPosition(0, Console.CursorTop - 1);              Console.SetCursorPosition(0, Console.CursorTop - 1);
133          }          }
134    
135          static void gLog_ReportProgress(object sender, ReportProgressEventArgs e)          static void gLog_ReportProgress(object sender, ReportProgressEventArgs e)
136          {          {
137              //if (!XMLTV_INSTANCE_CREATED && LOADING_XMLTV_FILE)              object k = e.UserState; // we will assum that is a status message
138              //{              if (k == null) // assume we do not have a user status message (ie: is only a progress message)
             //    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)  
139              {              {
140                  if (sender == null)                  if (sender == null) { ConsoleOverwritePreviosLine("Progress: {0} ", e.Progress); }
141                  {                  else { ConsoleOverwritePreviosLine("{0}: Progress: {1:00}%", sender.GetType().Name, e.Progress); }
                     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);  
                         }  
                     }  
                 }  
142              }              }
143              else              else // assume we have a user status message
144              {              {
145                  if (sender == null)                  if (sender == null) { ConsoleOverwritePreviosLine("{0} ", k.ToString()); }
146                  {                  else { ConsoleOverwritePreviosLine("{0}: {1}", sender.GetType().Name, k.ToString()); }
                     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());  
                         }  
                     }  
                 }  
147              }              }
148          }          }
149          static void LoadXMLTVFile()          static void LoadXMLTVFile()
# Line 227  namespace GBPVRProgramDatabaseFixer Line 191  namespace GBPVRProgramDatabaseFixer
191          }          }
192          static void SQLLITE_OnInstanceCreated(object sender, EventArgs e) { SQLLIST_INSTANCE_CREATED = true; }          static void SQLLITE_OnInstanceCreated(object sender, EventArgs e) { SQLLIST_INSTANCE_CREATED = true; }
193    
   
         //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;  
194          static void PerformDatabaseProcessing()          static void PerformDatabaseProcessing()
195          {          {
196              var sqllite_channels = sqlite.Channels;              var sqllite_channels = sqlite.Channels;
# Line 244  namespace GBPVRProgramDatabaseFixer Line 202  namespace GBPVRProgramDatabaseFixer
202              var sqllite_programs_startdate_range = sqlite.GetProgramsDateRange(sqllite_programs);              var sqllite_programs_startdate_range = sqlite.GetProgramsDateRange(sqllite_programs);
203              var xmltv_programs_startdate_range = GetXMLTVProgramStartDateRange(xmltv_programs);              var xmltv_programs_startdate_range = GetXMLTVProgramStartDateRange(xmltv_programs);
204              var gbpvr_invalid_programs = new List<SQLLITE.IPROGRAMME>();              var gbpvr_invalid_programs = new List<SQLLITE.IPROGRAMME>();
205              var gbpvr_valid_programs = sqlite.FixGBPVRProgramsDatabase(sqllite_programs, xmltv_programs, out gbpvr_invalid_programs);              var gbpvr_valid_programs = sqlite.FixGBPVRProgramsDatabase(sqllite_programs, xmltv_programs, out gbpvr_invalid_programs);    
206              //xmltv_programs = xmltv_programs.OrderBy(s => s.ChannelNumber).ThenBy(s => DateTime.Parse(s.Start)).ToList();              sqlite.RemoveOldGBPVRPrograms(gbpvr_invalid_programs);
207              //sqllite_channels.TrimExcess();              sqlite.UpdateGBPVRPrograms(gbpvr_valid_programs);
208              //gbpvr_invalid_programs.TrimExcess();  
209              //gbpvr_valid_programs.TrimExcess();              sqllite_programs.Clear();
210              //sqllite_programs.TrimExcess();              gbpvr_valid_programs.ForEach(s=>sqllite_programs.Add(s.NewProgram));
211              //xmltv_programs.TrimExcess();  
212              //CheckScheduledRecordingsForDiscrepancies(sqllite_recordings);              CheckScheduledRecordingsForDiscrepancies(sqllite_recordings,sqllite_programs);
             //CheckProgramsForDiscrepancies(gbpvr_valid_programs, xmltv_programs);  
213          }          }
214    
215          static IDateTimeRange GetXMLTVProgramStartDateRange(List<libxmltv.Interfaces.IProgramDefinition> programs)          static IDateTimeRange GetXMLTVProgramStartDateRange(List<libxmltv.Interfaces.IProgramDefinition> programs)
# Line 269  namespace GBPVRProgramDatabaseFixer Line 226  namespace GBPVRProgramDatabaseFixer
226              return range;              return range;
227          }          }
228    
229          static void CheckScheduledRecordingsForDiscrepancies(List<SQLLITE.IRECORDING_SCHEDULE> gbpvr)          static void CheckScheduledRecordingsForDiscrepancies(List<SQLLITE.IRECORDING_SCHEDULE> recordings, List<SQLLITE.IPROGRAMME> programs)
230          {          {
231              ////gLog.Warn.WriteLine("CheckScheduledRecordingsForDiscrepancies() has not been implemented");  #if !ENABLE_SCHEDULDED_RECORDINGS_FIXUP
232              //var manual_gbpvr_recordings = gbpvr.FindAll(p => p.recording_type == 0).ToList();               gLog.Warn.WriteLine("WANRING: Checking scheduled recordings for errors (and then deleting them) has been disabled.");
233    #else
234                List<SQLLITE.IRECORDING_SCHEDULE> recordings_to_delete = new List<SQLLITE.IRECORDING_SCHEDULE>();    
235                foreach (var recording in recordings)
236                {
237                    RecordingStatus status = (RecordingStatus)recording.status;
238                    RecordingType type = (RecordingType)recording.recording_type;
239    
240              //foreach (var manual_recording in manual_gbpvr_recordings)                  // ignore any recording whose type is not: RecordingType.TYPE_RECORD_ONCE
241              //{                  if (type != RecordingType.TYPE_RECORD_ONCE) { continue; }
242              //    string title = manual_recording.filename;                  // ignore any recording whose status is not: RecordingStatus.STATUS_PENDING and not: RecordingStatus.STATUS_IN_PROGRESS
243              //    DateTime start = manual_recording.manual_start_time;                  if (status != RecordingStatus.STATUS_PENDING && status != RecordingStatus.STATUS_IN_PROGRESS) { continue; }
244              //    DateTime end = manual_recording.manual_end_time;                  // only fix recordings that have a recording group greater than 0 (zero)
245              //    long channel_oid = manual_recording.manual_channel_oid;                  if (recording.recording_group == 0) { continue; }
246              //    var found_channel = gbpvr_channels.Find(p => p.oid == channel_oid);                  // verify data for this recording
247              //    var channel_number = found_channel.channel_number;                  var recording_oid = recording.oid;
248              //    var found_program = xmltv_programs.ToList().Find(p =>                  var programme_oid = recording.programme_oid;
249              //        p.Title.ToLower() == title.ToLower() &&                  var filename = recording.filename;
250              //        p.Start == start.ToDateTimeString() &&                  var manual_start_time = recording.manual_start_time;
251              //        p.Stop == end.ToDateTimeString() &&                  var manual_end_time = recording.manual_end_time;
252              //        p.ChannelNumber == channel_number                  var manual_channel_oid = recording.manual_channel_oid;
253              //        );  
254              //    if (found_program != null)                  // lookup program by oid
255              //    {                  var entry = programs.Find(s => s.oid == programme_oid);
256              //        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());                  if (entry == null)
257              //    }                  {
258              //    else                      gLog.Warn.WriteLine("Unable to find program with oid: '{0}' for recording with oid: '{1}' -- assuming it should be deleted", programme_oid, recording_oid);
259              //    {                      //throw new NullReferenceException(string.Format("Unable to find program with oid: '{0}' for recording with oid: '{1}'", programme_oid, recording_oid));
260              //        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());                      //deleted_recordings.Add(recording);
261              //        // find the program on the channel at the start time                  }
262              //        var correct_program = xmltv_programs.ToList().Find(p =>                  else
263              //            p.ChannelNumber == channel_number &&                  {
264              //            p.Start == start.ToDateTimeString()                      recordings_to_delete.Add(recording);        
265              //            );                  }
266              //        if (correct_program != null)              }
267              //        {              recordings_to_delete.TrimExcess();
268              //            gLog.Warn.WriteLine("\tThis will be recorded: {0}", correct_program.ToString());              if (recordings_to_delete.Count > 0)
269              //        }              {
270              //    }                  gLog.Info.WriteLine("Found {0} recordings to delete.", recordings_to_delete.Count);
271              //}                  sqlite.DeleteGBPVRScheduledRecordings(recordings_to_delete);
272          }              }
273                try
274          static void CheckProgramsForDiscrepancies(List<SQLLITE.IPROGRAMME> list_gbpvr, List<libxmltv.Interfaces.IProgramDefinition> list_xmltv)              {
275          {  
276              ////gLog.Warn.WriteLine("CheckProgramsForDiscrepancies() has not been implemented");                  // taken from: Config.EpgUpdateProgress.method_1()
277              //var gbpvr = list_gbpvr.ToArray();                  //Zap2itEpgReader.smethod_0();
278              //var xmltv = list_xmltv.ToArray();                  //GClass4.smethod_0().method_14();
279              ////if (gbpvr.Count() != xmltv.Count())                  Zap2itEpgReader.x9a6781c43845a63b();
280              ////{                  x396614b3dae3b1da.x479a7b07c9d5bd70().xf03e555cf1814572();
281              ////    throw new ArgumentOutOfRangeException("gbpvr.Count/xmltv.Count", string.Format("gbpvr.Count: '0x{0:x8}' != xmltv.Count: '0x{1:x8}'", gbpvr.Count(),xmltv.Count()));              }
282              ////}              catch (Exception ex)
283              ////for (int i = 0; i < gbpvr.Count(); i++)              {
284              ////{                  gLog.Error.WriteLine(ex.ToString());
285              ////    var gbpvr_entry = gbpvr[i];              }
286              ////    var xmltv_entry = gbpvr_entry.AsXMLTVProgramDefinition(sqlite);  #endif
             ////    if (!xmltv[i].Equals(xmltv_entry))  
             ////    {  
             ////        gLog.Warn.WriteLine("Warning GBPVR Program oid: {0} might be invalid", gbpvr_entry.oid);  
             ////    }  
             ////}  
287          }          }
288      }      }
289  }  }

Legend:
Removed from v.174  
changed lines
  Added in v.251

  ViewVC Help
Powered by ViewVC 1.1.22