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

Annotation of /trunk/GBPVRProgramDatabaseFixer/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22