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

Contents of /trunk/GBPVRProgramDatabaseFixer/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 163 - (show annotations) (download)
Fri Mar 15 16:53:40 2013 UTC (6 years, 6 months ago) by william
File size: 12165 byte(s)
+ filter GBPVR Programs to match the date-range of the XMLTV Programs list

1 #define OVERRIDE_COMMANDLINE_ARGUMENTS // when defined will override supplied commandline args
2 using System;
3 using System.Collections.Generic;
4 using System.Text;
5 using System.Linq;
6 using Enterprise.Logging;
7 using System.Windows.Forms;
8 using System.Diagnostics;
9 using System.IO;
10 using libxmltv.Core;
11 using libxmltv.Interfaces;
12 using libxmltv;
13 namespace GBPVRProgramDatabaseFixer
14 {
15 class Program
16 {
17 static ISQLLITE sqlite;
18 static string XMLTV_FILE = "";
19 static string GBPVR_DATABASE = "";
20
21 static bool LOADING_XMLTV_FILE = false;
22 static bool XMLTV_INSTANCE_CREATED = false;
23 static int LAST_REPORTED_XMLTV_PROGRESS = 0;
24
25
26 static bool LOADING_GBPVR_DATABASE = false;
27 static bool SQLLIST_INSTANCE_CREATED = false;
28 static int LAST_REPORTED_SQLLITE_PROGRESS = 0;
29
30 static FileInfo fi_XMLTVFILE;
31 static FileInfo fi_GBPVRDATABASE;
32 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 [Conditional("OVERRIDE_COMMANDLINE_ARGUMENTS")]
53 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 static void Main(string[] args)
68 {
69 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 LoadXMLTVFile();
90 LoadGBPVRDatabase();
91 PerformDatabaseProcessing();
92 }
93
94 static void ConsoleOverwritePreviosLine(string format, params object[] args)
95 {
96 Console.WriteLine(format,args);
97 Console.SetCursorPosition(0, Console.CursorTop - 1);
98 }
99
100 static void gLog_ReportProgress(object sender, ReportProgressEventArgs e)
101 {
102 if (!XMLTV_INSTANCE_CREATED && LOADING_XMLTV_FILE)
103 {
104 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 }
114 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 }
127 static void LoadXMLTVFile()
128 {
129 LOADING_XMLTV_FILE = true;
130 if (string.IsNullOrEmpty(XMLTV_FILE))
131 {
132 gLog.Error.WriteLine("XMLTV File is either a null or empty string.");
133 LOADING_XMLTV_FILE = false;
134 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 LOADING_XMLTV_FILE = false;
141 return;
142 }
143
144 XMLTV.Create(fi_XMLTVFILE.FullName, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
145 while (!XMLTV_INSTANCE_CREATED) { Application.DoEvents(); }
146 LOADING_XMLTV_FILE = false;
147 }
148 static void XMLTV_OnInstanceCreated(object sender, EventArgs e) { XMLTV_INSTANCE_CREATED = true; }
149
150 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 sqlite = SQLLITE.Create(fi_GBPVRDATABASE.FullName, new EventHandler<EventArgs>(SQLLITE_OnInstanceCreated));
167 while (!SQLLIST_INSTANCE_CREATED) { Application.DoEvents(); }
168 LOADING_GBPVR_DATABASE = false;
169 }
170 static void SQLLITE_OnInstanceCreated(object sender, EventArgs e) { SQLLIST_INSTANCE_CREATED = true; }
171
172
173 static List<SQLLITE.IPROGRAMME> gbpvr_programs;
174 static List<SQLLITE.ICHANNEL> gbpvr_channels;
175 static List<SQLLITE.IRECORDING_SCHEDULE> gbpvr_recordings;
176 static List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs;
177 static void PerformDatabaseProcessing()
178 {
179 gbpvr_channels = sqlite.Channels;
180 gbpvr_programs = sqlite.Programs;
181 gbpvr_recordings = sqlite.Recordings;
182 Type datatype;
183 var xmlt_raw_program_data = (IProgramDefinitionList)XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out datatype);
184 //xmltv_programs = xmlt_raw_program_data.ToList().OrderBy(p => p.ChannelNumber).ThenBy(p => DateTime.Parse(p.Start)).ToList();
185 //xmltv_programs = xmltv_programs.FindAll(p => p.Title == "Law & Order: Criminal Intent").ToList();
186 xmltv_programs = xmlt_raw_program_data.ToList().OrderBy(p => DateTime.Parse(p.Start)).ToList();
187
188
189 var gbpvr_programs_startdate_range = sqlite.GetProgramsDateRange(gbpvr_programs);
190 var xmltv_programs_startdate_range = GetXMLTVProgramStartDateRange(xmltv_programs);
191 gbpvr_programs = sqlite.FilterProgramsByDateRange(gbpvr_programs, xmltv_programs_startdate_range);
192 xmltv_programs = xmltv_programs.OrderBy(s => s.ChannelNumber).ThenBy(s => DateTime.Parse(s.Start)).ToList();
193 CheckScheduledRecordingsForDiscrepancies();
194 CheckProgramsForDiscrepancies();
195 }
196
197 static IDateTimeRange GetXMLTVProgramStartDateRange(List<libxmltv.Interfaces.IProgramDefinition> programs)
198 {
199 IDateTimeRange range = DateTimeRange.Create();
200 var list = new List<libxmltv.Interfaces.IProgramDefinition>(programs.ToArray());
201 DateTime first = new DateTime();
202 DateTime last = new DateTime();
203 first = DateTime.Parse(list.OrderBy(s => DateTime.Parse(s.Start)).ToList().First().Start);
204 last = DateTime.Parse(list.OrderBy(s => DateTime.Parse(s.Start)).ToList().Last().Start);
205 gLog.Info.WriteLine("\tFirst: {0} = ({1})", first.ToString("yyyy/MM/dd HH:mm:ss.fffffff"), first.ToDateTimeString());
206 gLog.Info.WriteLine("\tLast: {0} = ({1})", last.ToString("yyyy/MM/dd HH:mm:ss.fffffff"), last.ToDateTimeString());
207 range = DateTimeRange.Create(first, last);
208 return range;
209 }
210
211 static void CheckScheduledRecordingsForDiscrepancies()
212 {
213 //gLog.Warn.WriteLine("CheckScheduledRecordingsForDiscrepancies() has not been implemented");
214 var manual_gbpvr_recordings = gbpvr_recordings.FindAll(p => p.recording_type == 0).ToList();
215
216 foreach (var manual_recording in manual_gbpvr_recordings)
217 {
218 string title = manual_recording.filename;
219 DateTime start = manual_recording.manual_start_time;
220 DateTime end = manual_recording.manual_end_time;
221
222 long channel_oid = manual_recording.manual_channel_oid;
223
224 var found_channel = gbpvr_channels.Find(p => p.oid == channel_oid);
225 var channel_number = found_channel.channel_number;
226
227 var found_program = xmltv_programs.ToList().Find(p =>
228 p.Title.ToLower() == title.ToLower() &&
229 p.Start == start.ToDateTimeString() &&
230 p.Stop == end.ToDateTimeString() &&
231 p.ChannelNumber == channel_number
232 );
233
234 if (found_program != null)
235 {
236 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());
237 }
238 else
239 {
240 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());
241
242 // find the program on the channel at the start time
243 var correct_program = xmltv_programs.ToList().Find(p =>
244 p.ChannelNumber == channel_number &&
245 p.Start == start.ToDateTimeString()
246 );
247 if (correct_program != null)
248 {
249 gLog.Warn.WriteLine("\tThis will be recorded: {0}", correct_program.ToString());
250 }
251 }
252 }
253 }
254
255 static void CheckProgramsForDiscrepancies()
256 {
257 gLog.Warn.WriteLine("CheckProgramsForDiscrepancies() has not been implemented");
258 }
259 }
260 }

  ViewVC Help
Powered by ViewVC 1.1.22