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

Annotation of /trunk/GBPVRProgramDatabaseFixer/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 205 - (hide annotations) (download)
Sat Mar 16 23:00:07 2013 UTC (6 years, 6 months ago) by william
File size: 16529 byte(s)

1 william 154 #define OVERRIDE_COMMANDLINE_ARGUMENTS // when defined will override supplied commandline args
2 william 153 using System;
3 william 150 using System.Collections.Generic;
4     using System.Text;
5 william 159 using System.Linq;
6 william 153 using Enterprise.Logging;
7     using System.Windows.Forms;
8     using System.Diagnostics;
9     using System.IO;
10     using libxmltv.Core;
11 william 159 using libxmltv.Interfaces;
12     using libxmltv;
13 william 150 namespace GBPVRProgramDatabaseFixer
14     {
15     class Program
16     {
17 william 163 static ISQLLITE sqlite;
18 william 153 static string XMLTV_FILE = "";
19     static string GBPVR_DATABASE = "";
20 william 155
21 william 166 //static bool LOADING_XMLTV_FILE = false;
22 william 153 static bool XMLTV_INSTANCE_CREATED = false;
23 william 166 //static int LAST_REPORTED_XMLTV_PROGRESS = 0;
24 william 153
25    
26 william 166 //static bool LOADING_GBPVR_DATABASE = false;
27 william 155 static bool SQLLIST_INSTANCE_CREATED = false;
28 william 166 //static int LAST_REPORTED_SQLLITE_PROGRESS = 0;
29 william 155
30 william 166 static int LAST_REPORTED_PROGRESS = 0;
31    
32 william 153 static FileInfo fi_XMLTVFILE;
33 william 155 static FileInfo fi_GBPVRDATABASE;
34 william 153 static void CreateLog()
35     {
36     string log_path = Application.StartupPath;
37     string log_filename = string.Format("{0}.log", typeof(Program).Assembly.GetName().Name);
38     gLog.CreateLog(string.Format(@"{0}\{1}", log_path, log_filename), false, LogLevel.kLogLevel_All);
39     #if DEBUG
40     LogLevel gLevel = gLog.LogLevel;
41     gLevel |= LogLevel.kLogLevel_VerboseDebug;
42     gLevel |= LogLevel.kLogLevel_Debug;
43     gLog.SetLogLevel(gLevel);
44     #else
45     LogLevel gLevel = LogLevel.kLogLevel_Default; // set the default log level: Info, Warn, Error, Debug
46     // it is OK for kLogLevel_Debug to be set in Release mode ... must of the chatty messages are from kLogLevel_VerboseDebug
47     gLevel &= ~LogLevel.kLogLevel_Debug; // ensure this is not set, ever in release mode
48     gLevel &= ~LogLevel.kLogLevel_VerboseDebug; // ensure this is not set, ever in release mode
49     gLog.SetLogLevel(gLevel);
50     #endif
51     gLog.ReportProgressEvent += new EventHandler<ReportProgressEventArgs>(gLog_ReportProgress);
52     }
53    
54 william 154 [Conditional("OVERRIDE_COMMANDLINE_ARGUMENTS")]
55 william 153 static void CreateDebugCommandlineArgs(ref List<string> cargs)
56     {
57     cargs = new List<string>();
58     cargs.Add(string.Format("--xmltv_file=\"{0}\"", @"c:\EPG Guide\Guide\tvguide.xml"));
59     cargs.Add(string.Format("--gbprv_db=\"{0}\"", @"c:\Program Files (x86)\Devnz\GBPVR\gbpvr.db3"));
60     }
61    
62     static List<string> CreateCommandlineArgs(string[] args)
63     {
64     List<string> cargs = new List<string>(args);
65     CreateDebugCommandlineArgs(ref cargs);
66     return cargs;
67     }
68    
69 william 150 static void Main(string[] args)
70     {
71 william 153 CreateLog();
72     var cargs = CreateCommandlineArgs(args);
73     foreach (var carg in cargs)
74     {
75     if (carg.ToLower().Contains("--xmltv_file="))
76     {
77     XMLTV_FILE = carg.Replace("--xmltv_file=", "").Replace("\"","");
78     gLog.Info.WriteLine("XMLTV FILE: '{0}'", XMLTV_FILE);
79     }
80     else if (carg.ToLower().Contains("--gbprv_db="))
81     {
82     GBPVR_DATABASE = carg.Replace("--gbprv_db=", "").Replace("\"", "");
83     gLog.Info.WriteLine("GBPVR DATABASE: '{0}'", GBPVR_DATABASE);
84     }
85     else
86     {
87     gLog.Warn.WriteLine("Unknown commandline option: {0}", carg);
88     }
89     }
90    
91 william 157 LoadXMLTVFile();
92 william 155 LoadGBPVRDatabase();
93 william 163 PerformDatabaseProcessing();
94 william 150 }
95 william 153
96 william 155 static void ConsoleOverwritePreviosLine(string format, params object[] args)
97     {
98     Console.WriteLine(format,args);
99     Console.SetCursorPosition(0, Console.CursorTop - 1);
100     }
101    
102 william 153 static void gLog_ReportProgress(object sender, ReportProgressEventArgs e)
103     {
104 william 166 //if (!XMLTV_INSTANCE_CREATED && LOADING_XMLTV_FILE)
105     //{
106     // if (LAST_REPORTED_XMLTV_PROGRESS != e.Progress)
107     // {
108     // ConsoleOverwritePreviosLine("XMLTV: {0} Loading: {1:00}%", fi_XMLTVFILE.Name, e.Progress);
109     // LAST_REPORTED_XMLTV_PROGRESS = e.Progress;
110     // if (e.Progress == 100)
111     // {
112     // Console.WriteLine("XMLTV: {0} Loading: {1:00}%", fi_XMLTVFILE.Name, e.Progress);
113     // }
114     // }
115     //}
116     //if (!SQLLIST_INSTANCE_CREATED && LOADING_GBPVR_DATABASE)
117     //{
118     // if (LAST_REPORTED_SQLLITE_PROGRESS != e.Progress)
119     // {
120     // ConsoleOverwritePreviosLine("SQLLITE: {0} Loading: {1:00}%", fi_XMLTVFILE.Name, e.Progress);
121     // LAST_REPORTED_SQLLITE_PROGRESS = e.Progress;
122     // if (e.Progress == 100)
123     // {
124     // Console.WriteLine("SQLLITE: {0} Loading: {1:00}%", fi_XMLTVFILE.Name, e.Progress);
125     // }
126     // }
127     //}
128    
129 william 205 object k = e.UserState; // we will assum that is a status message
130     if (k == null) // assume we do not have a user status message (ie: is only a progress message)
131 william 153 {
132 william 166 if (sender == null)
133 william 155 {
134 william 205 //if (LAST_REPORTED_PROGRESS != e.Progress)
135     //{
136     // ConsoleOverwritePreviosLine("Progress: {0} ", e.Progress);
137     // LAST_REPORTED_PROGRESS = e.Progress;
138     // if (e.Progress == 100)
139     // {
140     // Console.WriteLine("Progress: {0} ", e.Progress);
141     // }
142     //}
143     ConsoleOverwritePreviosLine("Progress: {0} ", e.Progress);
144 william 155 }
145 william 166 else
146     {
147 william 205 //if (LAST_REPORTED_PROGRESS != e.Progress)
148     //{
149     // ConsoleOverwritePreviosLine("{0}: Progress: {1:00}%", sender.GetType().Name, e.Progress);
150     // LAST_REPORTED_PROGRESS = e.Progress;
151     // if (e.Progress == 100)
152     // {
153     // Console.WriteLine("{0}: Progress: {1:00}%", sender.GetType().Name, e.Progress);
154     // }
155     //}
156     ConsoleOverwritePreviosLine("{0}: Progress: {1:00}%", sender.GetType().Name, e.Progress);
157 william 166 }
158 william 153 }
159 william 205 else // assume we have a user status message
160 william 155 {
161 william 166 if (sender == null)
162 william 155 {
163 william 205 //if (LAST_REPORTED_PROGRESS != e.Progress)
164     //{
165     // ConsoleOverwritePreviosLine("Progress: {0} ", k.ToString());
166     // LAST_REPORTED_PROGRESS = e.Progress;
167     // if (e.Progress == 100)
168     // {
169     // Console.WriteLine("Progress: {0} ", k.ToString());
170     // }
171     //}
172     ConsoleOverwritePreviosLine("{0} ", k.ToString());
173 william 155 }
174 william 166 else
175     {
176 william 205 //if (LAST_REPORTED_PROGRESS != e.Progress)
177     //{
178     // ConsoleOverwritePreviosLine("{0}: {1}", sender.GetType().Name, k.ToString());
179     // LAST_REPORTED_PROGRESS = e.Progress;
180     // if (e.Progress == 100)
181     // {
182     // Console.WriteLine("{0}: {1}", sender.GetType().Name, k.ToString());
183     // }
184     //}
185     ConsoleOverwritePreviosLine("{0}: {1}", sender.GetType().Name, k.ToString());
186 william 166 }
187 william 155 }
188 william 153 }
189     static void LoadXMLTVFile()
190     {
191 william 166 //LOADING_XMLTV_FILE = true;
192 william 153 if (string.IsNullOrEmpty(XMLTV_FILE))
193     {
194     gLog.Error.WriteLine("XMLTV File is either a null or empty string.");
195 william 166 //LOADING_XMLTV_FILE = false;
196 william 153 return;
197     }
198     fi_XMLTVFILE = new FileInfo(XMLTV_FILE);
199     if (!fi_XMLTVFILE.Exists)
200     {
201     gLog.Error.WriteLine("Could not find XMLTV File: {0}", fi_XMLTVFILE.FullName);
202 william 166 //LOADING_XMLTV_FILE = false;
203 william 153 return;
204     }
205    
206     XMLTV.Create(fi_XMLTVFILE.FullName, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
207     while (!XMLTV_INSTANCE_CREATED) { Application.DoEvents(); }
208 william 166 //LOADING_XMLTV_FILE = false;
209 william 153 }
210 william 155 static void XMLTV_OnInstanceCreated(object sender, EventArgs e) { XMLTV_INSTANCE_CREATED = true; }
211 william 153
212 william 155 static void LoadGBPVRDatabase()
213     {
214 william 166 //LOADING_GBPVR_DATABASE = true;
215 william 155 if (string.IsNullOrEmpty(GBPVR_DATABASE))
216     {
217     gLog.Error.WriteLine("GBPVR Datbase is either a null or empty string.");
218 william 166 //LOADING_GBPVR_DATABASE = false;
219 william 155 return;
220     }
221     fi_GBPVRDATABASE = new FileInfo(GBPVR_DATABASE);
222     if (!fi_GBPVRDATABASE.Exists)
223     {
224     gLog.Error.WriteLine("Could not find GBPVR Database: {0}", fi_GBPVRDATABASE.FullName);
225 william 166 //LOADING_GBPVR_DATABASE = false;
226 william 155 return;
227     }
228 william 163 sqlite = SQLLITE.Create(fi_GBPVRDATABASE.FullName, new EventHandler<EventArgs>(SQLLITE_OnInstanceCreated));
229 william 156 while (!SQLLIST_INSTANCE_CREATED) { Application.DoEvents(); }
230 william 166 //LOADING_GBPVR_DATABASE = false;
231 william 155 }
232 william 156 static void SQLLITE_OnInstanceCreated(object sender, EventArgs e) { SQLLIST_INSTANCE_CREATED = true; }
233 william 157
234    
235 william 166 //static List<SQLLITE.IPROGRAMME> gbpvr_invalid_programs;
236     //static List<SQLLITE.IPROGRAMME> gbpvr_valid_programs;
237     //static List<SQLLITE.ICHANNEL> gbpvr_channels;
238     //static List<SQLLITE.IRECORDING_SCHEDULE> gbpvr_recordings;
239     //static List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs;
240 william 157 static void PerformDatabaseProcessing()
241     {
242 william 166 var sqllite_channels = sqlite.Channels;
243     var sqllite_recordings = sqlite.Recordings;
244     var sqllite_programs = sqlite.Programs;
245 william 159 Type datatype;
246 william 162 var xmlt_raw_program_data = (IProgramDefinitionList)XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out datatype);
247 william 166 var xmltv_programs = xmlt_raw_program_data.ToList().OrderBy(p => DateTime.Parse(p.Start)).ToList();
248     var sqllite_programs_startdate_range = sqlite.GetProgramsDateRange(sqllite_programs);
249 william 165 var xmltv_programs_startdate_range = GetXMLTVProgramStartDateRange(xmltv_programs);
250 william 166 var gbpvr_invalid_programs = new List<SQLLITE.IPROGRAMME>();
251 william 174 var gbpvr_valid_programs = sqlite.FixGBPVRProgramsDatabase(sqllite_programs, xmltv_programs, out gbpvr_invalid_programs);
252     //xmltv_programs = xmltv_programs.OrderBy(s => s.ChannelNumber).ThenBy(s => DateTime.Parse(s.Start)).ToList();
253 william 166 //sqllite_channels.TrimExcess();
254     //gbpvr_invalid_programs.TrimExcess();
255     //gbpvr_valid_programs.TrimExcess();
256     //sqllite_programs.TrimExcess();
257     //xmltv_programs.TrimExcess();
258     //CheckScheduledRecordingsForDiscrepancies(sqllite_recordings);
259     //CheckProgramsForDiscrepancies(gbpvr_valid_programs, xmltv_programs);
260 william 183
261     sqlite.RemoveOldGBPVRPrograms(gbpvr_invalid_programs);
262     sqlite.UpdateGBPVRPrograms(gbpvr_valid_programs);
263    
264 william 157 }
265 william 158
266 william 163 static IDateTimeRange GetXMLTVProgramStartDateRange(List<libxmltv.Interfaces.IProgramDefinition> programs)
267     {
268     IDateTimeRange range = DateTimeRange.Create();
269     var list = new List<libxmltv.Interfaces.IProgramDefinition>(programs.ToArray());
270     DateTime first = new DateTime();
271     DateTime last = new DateTime();
272     first = DateTime.Parse(list.OrderBy(s => DateTime.Parse(s.Start)).ToList().First().Start);
273     last = DateTime.Parse(list.OrderBy(s => DateTime.Parse(s.Start)).ToList().Last().Start);
274 william 167 gLog.Verbose.Debug.WriteLine("\tFirst: {0} = ({1})", first.ToString("yyyy/MM/dd HH:mm:ss.fffffff"), first.ToDateTimeString());
275     gLog.Verbose.Debug.WriteLine("\tLast: {0} = ({1})", last.ToString("yyyy/MM/dd HH:mm:ss.fffffff"), last.ToDateTimeString());
276 william 163 range = DateTimeRange.Create(first, last);
277     return range;
278     }
279    
280 william 165 static void CheckScheduledRecordingsForDiscrepancies(List<SQLLITE.IRECORDING_SCHEDULE> gbpvr)
281 william 158 {
282 william 166 ////gLog.Warn.WriteLine("CheckScheduledRecordingsForDiscrepancies() has not been implemented");
283     //var manual_gbpvr_recordings = gbpvr.FindAll(p => p.recording_type == 0).ToList();
284 william 159
285 william 166 //foreach (var manual_recording in manual_gbpvr_recordings)
286     //{
287     // string title = manual_recording.filename;
288     // DateTime start = manual_recording.manual_start_time;
289     // DateTime end = manual_recording.manual_end_time;
290     // long channel_oid = manual_recording.manual_channel_oid;
291     // var found_channel = gbpvr_channels.Find(p => p.oid == channel_oid);
292     // var channel_number = found_channel.channel_number;
293     // var found_program = xmltv_programs.ToList().Find(p =>
294     // p.Title.ToLower() == title.ToLower() &&
295     // p.Start == start.ToDateTimeString() &&
296     // p.Stop == end.ToDateTimeString() &&
297     // p.ChannelNumber == channel_number
298     // );
299     // if (found_program != null)
300     // {
301     // 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());
302     // }
303     // else
304     // {
305     // 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());
306     // // find the program on the channel at the start time
307     // var correct_program = xmltv_programs.ToList().Find(p =>
308     // p.ChannelNumber == channel_number &&
309     // p.Start == start.ToDateTimeString()
310     // );
311     // if (correct_program != null)
312     // {
313     // gLog.Warn.WriteLine("\tThis will be recorded: {0}", correct_program.ToString());
314     // }
315     // }
316     //}
317 william 158 }
318 william 159
319 william 166 static void CheckProgramsForDiscrepancies(List<SQLLITE.IPROGRAMME> list_gbpvr, List<libxmltv.Interfaces.IProgramDefinition> list_xmltv)
320 william 158 {
321 william 166 ////gLog.Warn.WriteLine("CheckProgramsForDiscrepancies() has not been implemented");
322     //var gbpvr = list_gbpvr.ToArray();
323     //var xmltv = list_xmltv.ToArray();
324     ////if (gbpvr.Count() != xmltv.Count())
325     ////{
326     //// throw new ArgumentOutOfRangeException("gbpvr.Count/xmltv.Count", string.Format("gbpvr.Count: '0x{0:x8}' != xmltv.Count: '0x{1:x8}'", gbpvr.Count(),xmltv.Count()));
327     ////}
328     ////for (int i = 0; i < gbpvr.Count(); i++)
329     ////{
330     //// var gbpvr_entry = gbpvr[i];
331     //// var xmltv_entry = gbpvr_entry.AsXMLTVProgramDefinition(sqlite);
332     //// if (!xmltv[i].Equals(xmltv_entry))
333     //// {
334     //// gLog.Warn.WriteLine("Warning GBPVR Program oid: {0} might be invalid", gbpvr_entry.oid);
335     //// }
336     ////}
337 william 158 }
338 william 150 }
339     }

  ViewVC Help
Powered by ViewVC 1.1.22