/[xmltv_parser]/trunk/GBPVRProgramDatabaseFixer/Program.cs
ViewVC logotype

Annotation of /trunk/GBPVRProgramDatabaseFixer/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 272 - (hide annotations) (download)
Sat Apr 4 00:27:55 2015 UTC (4 years, 7 months ago) by william
File size: 14002 byte(s)

1 william 154 #define OVERRIDE_COMMANDLINE_ARGUMENTS // when defined will override supplied commandline args
2 william 251 //#define ENABLE_SCHEDULDED_RECORDINGS_FIXUP // when defined will enable calls to CheckScheduledRecordingsForDiscrepancies
3    
4 william 272 #if DEBUG
5     //#define ENABLE_VERBOSE_DEBUG_WHEN_DEBUGGING // when defined and debugging will enable verbose debug log messages
6     #endif
7 william 153 using System;
8 william 150 using System.Collections.Generic;
9     using System.Text;
10 william 159 using System.Linq;
11 william 153 using Enterprise.Logging;
12     using System.Windows.Forms;
13     using System.Diagnostics;
14     using System.IO;
15     using libxmltv.Core;
16 william 159 using libxmltv.Interfaces;
17     using libxmltv;
18 william 233 using GBPVR.Backend.Common;
19 william 150 namespace GBPVRProgramDatabaseFixer
20     {
21     class Program
22     {
23 william 241 const long MAX_LOG_FILESIZE_IN_BYTES = 250000; //250kb max
24     static void TruncateLogFile(FileInfo log_file)
25     {
26     if (!log_file.Exists)
27     {
28 william 272 gLog.CreateLog(log_file.FullName, false, LogLevel.kLogLevel_All, new LogWriter.LogStream());
29 william 241 }
30     else
31     {
32     if (log_file.Length > MAX_LOG_FILESIZE_IN_BYTES)
33     {
34     string match = string.Format("{0}.*", log_file.Name);
35     var files = Directory.GetFiles(log_file.Directory.FullName, match).ToList();
36     files.RemoveAt(0);
37     files.TrimExcess();
38     int count = files.Count;
39     if (log_file.Exists)
40     {
41     File.Copy(log_file.FullName, string.Format("{0}.{1}", log_file.FullName, count));
42     }
43 william 272 gLog.CreateLog(log_file.FullName, true, LogLevel.kLogLevel_All, new LogWriter.LogStream());
44 william 241 }
45     else
46     {
47 william 272 gLog.CreateLog(log_file.FullName, false, LogLevel.kLogLevel_All, new LogWriter.LogStream());
48 william 241 }
49     }
50     }
51    
52 william 163 static ISQLLITE sqlite;
53 william 153 static string XMLTV_FILE = "";
54     static string GBPVR_DATABASE = "";
55 william 155
56 william 166 //static bool LOADING_XMLTV_FILE = false;
57 william 153 static bool XMLTV_INSTANCE_CREATED = false;
58 william 166 //static int LAST_REPORTED_XMLTV_PROGRESS = 0;
59 william 153
60    
61 william 166 //static bool LOADING_GBPVR_DATABASE = false;
62 william 155 static bool SQLLIST_INSTANCE_CREATED = false;
63 william 166 //static int LAST_REPORTED_SQLLITE_PROGRESS = 0;
64 william 155
65 william 212 //static int LAST_REPORTED_PROGRESS = 0;
66 william 166
67 william 153 static FileInfo fi_XMLTVFILE;
68 william 155 static FileInfo fi_GBPVRDATABASE;
69 william 153 static void CreateLog()
70     {
71     string log_path = Application.StartupPath;
72     string log_filename = string.Format("{0}.log", typeof(Program).Assembly.GetName().Name);
73 william 241 FileInfo log_file = new FileInfo(string.Format(@"{0}\{1}", log_path, log_filename));
74     TruncateLogFile(log_file);
75 william 153 #if DEBUG
76     LogLevel gLevel = gLog.LogLevel;
77 william 272 #if ENABLE_VERBOSE_DEBUG_WHEN_DEBUGGING
78 william 153 gLevel |= LogLevel.kLogLevel_VerboseDebug;
79 william 272 #else
80     gLevel = gLevel & ~LogLevel.kLogLevel_VerboseDebug;
81     #endif
82 william 153 gLevel |= LogLevel.kLogLevel_Debug;
83     gLog.SetLogLevel(gLevel);
84     #else
85     LogLevel gLevel = LogLevel.kLogLevel_Default; // set the default log level: Info, Warn, Error, Debug
86     // it is OK for kLogLevel_Debug to be set in Release mode ... must of the chatty messages are from kLogLevel_VerboseDebug
87     gLevel &= ~LogLevel.kLogLevel_Debug; // ensure this is not set, ever in release mode
88     gLevel &= ~LogLevel.kLogLevel_VerboseDebug; // ensure this is not set, ever in release mode
89     gLog.SetLogLevel(gLevel);
90     #endif
91     gLog.ReportProgressEvent += new EventHandler<ReportProgressEventArgs>(gLog_ReportProgress);
92     }
93    
94 william 154 [Conditional("OVERRIDE_COMMANDLINE_ARGUMENTS")]
95 william 153 static void CreateDebugCommandlineArgs(ref List<string> cargs)
96     {
97     cargs = new List<string>();
98     cargs.Add(string.Format("--xmltv_file=\"{0}\"", @"c:\EPG Guide\Guide\tvguide.xml"));
99     cargs.Add(string.Format("--gbprv_db=\"{0}\"", @"c:\Program Files (x86)\Devnz\GBPVR\gbpvr.db3"));
100     }
101    
102     static List<string> CreateCommandlineArgs(string[] args)
103     {
104     List<string> cargs = new List<string>(args);
105     CreateDebugCommandlineArgs(ref cargs);
106     return cargs;
107     }
108    
109 william 150 static void Main(string[] args)
110     {
111 william 153 CreateLog();
112     var cargs = CreateCommandlineArgs(args);
113     foreach (var carg in cargs)
114     {
115     if (carg.ToLower().Contains("--xmltv_file="))
116     {
117     XMLTV_FILE = carg.Replace("--xmltv_file=", "").Replace("\"","");
118     gLog.Info.WriteLine("XMLTV FILE: '{0}'", XMLTV_FILE);
119     }
120     else if (carg.ToLower().Contains("--gbprv_db="))
121     {
122     GBPVR_DATABASE = carg.Replace("--gbprv_db=", "").Replace("\"", "");
123     gLog.Info.WriteLine("GBPVR DATABASE: '{0}'", GBPVR_DATABASE);
124     }
125     else
126     {
127     gLog.Warn.WriteLine("Unknown commandline option: {0}", carg);
128     }
129     }
130    
131 william 157 LoadXMLTVFile();
132 william 155 LoadGBPVRDatabase();
133 william 163 PerformDatabaseProcessing();
134 william 150 }
135 william 153
136 william 155 static void ConsoleOverwritePreviosLine(string format, params object[] args)
137     {
138     Console.WriteLine(format,args);
139     Console.SetCursorPosition(0, Console.CursorTop - 1);
140     }
141    
142 william 232 static void gLog_ReportProgress(object sender, ReportProgressEventArgs e)
143 william 153 {
144 william 272 //object k = e.UserState; // we will assum that is a status message
145     //if (k == null) // assume we do not have a user status message (ie: is only a progress message)
146     //{
147     // if (sender == null) { ConsoleOverwritePreviosLine("Progress: {0} ", e.Progress); }
148     // else { ConsoleOverwritePreviosLine("{0}: Progress: {1:00}%", sender.GetType().Name, e.Progress); }
149     //}
150     //else // assume we have a user status message
151     //{
152     // if (sender == null) { ConsoleOverwritePreviosLine("{0} ", k.ToString()); }
153     // else { ConsoleOverwritePreviosLine("{0}: {1}", sender.GetType().Name, k.ToString()); }
154     //}
155 william 153 }
156     static void LoadXMLTVFile()
157     {
158 william 166 //LOADING_XMLTV_FILE = true;
159 william 153 if (string.IsNullOrEmpty(XMLTV_FILE))
160     {
161     gLog.Error.WriteLine("XMLTV File is either a null or empty string.");
162 william 166 //LOADING_XMLTV_FILE = false;
163 william 153 return;
164     }
165     fi_XMLTVFILE = new FileInfo(XMLTV_FILE);
166     if (!fi_XMLTVFILE.Exists)
167     {
168     gLog.Error.WriteLine("Could not find XMLTV File: {0}", fi_XMLTVFILE.FullName);
169 william 166 //LOADING_XMLTV_FILE = false;
170 william 153 return;
171     }
172    
173     XMLTV.Create(fi_XMLTVFILE.FullName, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
174     while (!XMLTV_INSTANCE_CREATED) { Application.DoEvents(); }
175 william 166 //LOADING_XMLTV_FILE = false;
176 william 153 }
177 william 155 static void XMLTV_OnInstanceCreated(object sender, EventArgs e) { XMLTV_INSTANCE_CREATED = true; }
178 william 153
179 william 155 static void LoadGBPVRDatabase()
180     {
181 william 166 //LOADING_GBPVR_DATABASE = true;
182 william 155 if (string.IsNullOrEmpty(GBPVR_DATABASE))
183     {
184     gLog.Error.WriteLine("GBPVR Datbase is either a null or empty string.");
185 william 166 //LOADING_GBPVR_DATABASE = false;
186 william 155 return;
187     }
188     fi_GBPVRDATABASE = new FileInfo(GBPVR_DATABASE);
189     if (!fi_GBPVRDATABASE.Exists)
190     {
191     gLog.Error.WriteLine("Could not find GBPVR Database: {0}", fi_GBPVRDATABASE.FullName);
192 william 166 //LOADING_GBPVR_DATABASE = false;
193 william 155 return;
194     }
195 william 163 sqlite = SQLLITE.Create(fi_GBPVRDATABASE.FullName, new EventHandler<EventArgs>(SQLLITE_OnInstanceCreated));
196 william 156 while (!SQLLIST_INSTANCE_CREATED) { Application.DoEvents(); }
197 william 166 //LOADING_GBPVR_DATABASE = false;
198 william 155 }
199 william 156 static void SQLLITE_OnInstanceCreated(object sender, EventArgs e) { SQLLIST_INSTANCE_CREATED = true; }
200 william 157
201     static void PerformDatabaseProcessing()
202     {
203 william 166 var sqllite_channels = sqlite.Channels;
204     var sqllite_recordings = sqlite.Recordings;
205     var sqllite_programs = sqlite.Programs;
206 william 159 Type datatype;
207 william 162 var xmlt_raw_program_data = (IProgramDefinitionList)XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out datatype);
208 william 166 var xmltv_programs = xmlt_raw_program_data.ToList().OrderBy(p => DateTime.Parse(p.Start)).ToList();
209     var sqllite_programs_startdate_range = sqlite.GetProgramsDateRange(sqllite_programs);
210 william 165 var xmltv_programs_startdate_range = GetXMLTVProgramStartDateRange(xmltv_programs);
211 william 166 var gbpvr_invalid_programs = new List<SQLLITE.IPROGRAMME>();
212 william 232 var gbpvr_valid_programs = sqlite.FixGBPVRProgramsDatabase(sqllite_programs, xmltv_programs, out gbpvr_invalid_programs);
213 william 183 sqlite.RemoveOldGBPVRPrograms(gbpvr_invalid_programs);
214     sqlite.UpdateGBPVRPrograms(gbpvr_valid_programs);
215    
216 william 233 sqllite_programs.Clear();
217     gbpvr_valid_programs.ForEach(s=>sqllite_programs.Add(s.NewProgram));
218    
219     CheckScheduledRecordingsForDiscrepancies(sqllite_recordings,sqllite_programs);
220 william 157 }
221 william 158
222 william 163 static IDateTimeRange GetXMLTVProgramStartDateRange(List<libxmltv.Interfaces.IProgramDefinition> programs)
223     {
224     IDateTimeRange range = DateTimeRange.Create();
225     var list = new List<libxmltv.Interfaces.IProgramDefinition>(programs.ToArray());
226     DateTime first = new DateTime();
227     DateTime last = new DateTime();
228     first = DateTime.Parse(list.OrderBy(s => DateTime.Parse(s.Start)).ToList().First().Start);
229     last = DateTime.Parse(list.OrderBy(s => DateTime.Parse(s.Start)).ToList().Last().Start);
230 william 167 gLog.Verbose.Debug.WriteLine("\tFirst: {0} = ({1})", first.ToString("yyyy/MM/dd HH:mm:ss.fffffff"), first.ToDateTimeString());
231     gLog.Verbose.Debug.WriteLine("\tLast: {0} = ({1})", last.ToString("yyyy/MM/dd HH:mm:ss.fffffff"), last.ToDateTimeString());
232 william 163 range = DateTimeRange.Create(first, last);
233     return range;
234     }
235    
236 william 233 static void CheckScheduledRecordingsForDiscrepancies(List<SQLLITE.IRECORDING_SCHEDULE> recordings, List<SQLLITE.IPROGRAMME> programs)
237 william 158 {
238 william 251 #if !ENABLE_SCHEDULDED_RECORDINGS_FIXUP
239     gLog.Warn.WriteLine("WANRING: Checking scheduled recordings for errors (and then deleting them) has been disabled.");
240     #else
241     List<SQLLITE.IRECORDING_SCHEDULE> recordings_to_delete = new List<SQLLITE.IRECORDING_SCHEDULE>();
242 william 233 foreach (var recording in recordings)
243     {
244     RecordingStatus status = (RecordingStatus)recording.status;
245     RecordingType type = (RecordingType)recording.recording_type;
246 william 250
247     // ignore any recording whose type is not: RecordingType.TYPE_RECORD_ONCE
248     if (type != RecordingType.TYPE_RECORD_ONCE) { continue; }
249     // ignore any recording whose status is not: RecordingStatus.STATUS_PENDING and not: RecordingStatus.STATUS_IN_PROGRESS
250     if (status != RecordingStatus.STATUS_PENDING && status != RecordingStatus.STATUS_IN_PROGRESS) { continue; }
251     // only fix recordings that have a recording group greater than 0 (zero)
252     if (recording.recording_group == 0) { continue; }
253 william 233 // verify data for this recording
254     var recording_oid = recording.oid;
255     var programme_oid = recording.programme_oid;
256     var filename = recording.filename;
257     var manual_start_time = recording.manual_start_time;
258     var manual_end_time = recording.manual_end_time;
259     var manual_channel_oid = recording.manual_channel_oid;
260    
261     // lookup program by oid
262     var entry = programs.Find(s => s.oid == programme_oid);
263     if (entry == null)
264     {
265     gLog.Warn.WriteLine("Unable to find program with oid: '{0}' for recording with oid: '{1}' -- assuming it should be deleted", programme_oid, recording_oid);
266     //throw new NullReferenceException(string.Format("Unable to find program with oid: '{0}' for recording with oid: '{1}'", programme_oid, recording_oid));
267     //deleted_recordings.Add(recording);
268     }
269     else
270     {
271     recordings_to_delete.Add(recording);
272     }
273     }
274     recordings_to_delete.TrimExcess();
275     if (recordings_to_delete.Count > 0)
276     {
277     gLog.Info.WriteLine("Found {0} recordings to delete.", recordings_to_delete.Count);
278     sqlite.DeleteGBPVRScheduledRecordings(recordings_to_delete);
279     }
280     try
281     {
282 william 239
283     // taken from: Config.EpgUpdateProgress.method_1()
284 william 247 //Zap2itEpgReader.smethod_0();
285     //GClass4.smethod_0().method_14();
286     Zap2itEpgReader.x9a6781c43845a63b();
287     x396614b3dae3b1da.x479a7b07c9d5bd70().xf03e555cf1814572();
288 william 233 }
289     catch (Exception ex)
290     {
291     gLog.Error.WriteLine(ex.ToString());
292     }
293 william 251 #endif
294 william 158 }
295 william 150 }
296     }

  ViewVC Help
Powered by ViewVC 1.1.22