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

Contents of /trunk/GBPVRProgramDatabaseFixer/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 162 - (show annotations) (download)
Thu Mar 14 21:40:19 2013 UTC (6 years, 6 months ago) by william
File size: 10891 byte(s)

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 SQLLITE 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
167 sqlite = new SQLLITE(fi_GBPVRDATABASE.FullName, new EventHandler<EventArgs>(SQLLITE_OnInstanceCreated));
168 while (!SQLLIST_INSTANCE_CREATED) { Application.DoEvents(); }
169 LOADING_GBPVR_DATABASE = false;
170 }
171 static void SQLLITE_OnInstanceCreated(object sender, EventArgs e) { SQLLIST_INSTANCE_CREATED = true; }
172
173
174 static List<SQLLITE.IPROGRAMME> gbpvr_programs;
175 static List<SQLLITE.ICHANNEL> gbpvr_channels;
176 static List<SQLLITE.IRECORDING_SCHEDULE> gbpvr_recordings;
177 static List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs;
178 static void PerformDatabaseProcessing()
179 {
180 gbpvr_channels = sqlite.Channels;
181 gbpvr_programs = sqlite.Programs;
182 gbpvr_recordings = sqlite.Recordings;
183 Type datatype;
184 var xmlt_raw_program_data = (IProgramDefinitionList)XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out datatype);
185 xmltv_programs = xmlt_raw_program_data.ToList().OrderBy(p => p.ChannelNumber).ThenBy(p => DateTime.Parse(p.Start)).ToList();
186
187
188 xmltv_programs = xmltv_programs.FindAll(p => p.Title == "Law & Order: Criminal Intent").ToList();
189
190
191
192 //var data = xmltv_programs[0];
193 //DateTime p = DateTime.Parse(data.Start);
194 //string strP = p.ToString("yyyy/MM/dd HH:mm:ss.fffffff");
195
196 CheckScheduledRecordingsForDiscrepancies();
197 CheckProgramsForDiscrepancies();
198 }
199
200 static void CheckScheduledRecordingsForDiscrepancies()
201 {
202 //gLog.Warn.WriteLine("CheckScheduledRecordingsForDiscrepancies() has not been implemented");
203 var manual_gbpvr_recordings = gbpvr_recordings.FindAll(p => p.recording_type == 0).ToList();
204
205 foreach (var manual_recording in manual_gbpvr_recordings)
206 {
207 string title = manual_recording.filename;
208 DateTime start = manual_recording.manual_start_time;
209 DateTime end = manual_recording.manual_end_time;
210
211 long channel_oid = manual_recording.manual_channel_oid;
212
213 var found_channel = gbpvr_channels.Find(p => p.oid == channel_oid);
214 var channel_number = found_channel.channel_number;
215
216 var found_program = xmltv_programs.ToList().Find(p =>
217 p.Title.ToLower() == title.ToLower() &&
218 p.Start == start.ToDateTimeString() &&
219 p.Stop == end.ToDateTimeString() &&
220 p.ChannelNumber == channel_number
221 );
222
223 if (found_program != null)
224 {
225 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());
226 }
227 else
228 {
229 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());
230
231 // find the program on the channel at the start time
232 var correct_program = xmltv_programs.ToList().Find(p =>
233 p.ChannelNumber == channel_number &&
234 p.Start == start.ToDateTimeString()
235 );
236 if (correct_program != null)
237 {
238 gLog.Warn.WriteLine("\tThis will be recorded: {0}", correct_program.ToString());
239 }
240 }
241 }
242 }
243
244 static void CheckProgramsForDiscrepancies()
245 {
246 gLog.Warn.WriteLine("CheckProgramsForDiscrepancies() has not been implemented");
247 }
248 }
249 }

  ViewVC Help
Powered by ViewVC 1.1.22