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

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

  ViewVC Help
Powered by ViewVC 1.1.22