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

Annotation of /trunk/GBPVRProgramDatabaseFixer/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 159 - (hide annotations) (download)
Thu Mar 14 20:26:05 2013 UTC (6 years, 8 months ago) by william
File size: 10488 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 156 static SQLLITE 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 157 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    
167 william 156 sqlite = new SQLLITE(fi_GBPVRDATABASE.FullName, new EventHandler<EventArgs>(SQLLITE_OnInstanceCreated));
168     while (!SQLLIST_INSTANCE_CREATED) { Application.DoEvents(); }
169 william 155 LOADING_GBPVR_DATABASE = false;
170     }
171 william 156 static void SQLLITE_OnInstanceCreated(object sender, EventArgs e) { SQLLIST_INSTANCE_CREATED = true; }
172 william 157
173    
174 william 158 static List<SQLLITE.IPROGRAMME> gbpvr_programs;
175 william 159 static List<SQLLITE.ICHANNEL> gbpvr_channels;
176 william 158 static List<SQLLITE.IRECORDING_SCHEDULE> gbpvr_recordings;
177 william 159 static IProgramDefinitionList xmltv_programs;
178 william 157 static void PerformDatabaseProcessing()
179     {
180 william 159 gbpvr_channels = sqlite.Channels;
181 william 158 gbpvr_programs = sqlite.Programs;
182 william 159 gbpvr_recordings = sqlite.Recordings;
183     Type datatype;
184     xmltv_programs = (IProgramDefinitionList)XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out datatype);
185 william 158 CheckScheduledRecordingsForDiscrepancies();
186     CheckProgramsForDiscrepancies();
187 william 157 }
188 william 158
189     static void CheckScheduledRecordingsForDiscrepancies()
190     {
191 william 159 //gLog.Warn.WriteLine("CheckScheduledRecordingsForDiscrepancies() has not been implemented");
192     var manual_gbpvr_recordings = gbpvr_recordings.FindAll(p => p.recording_type == 0).ToList();
193    
194     foreach (var manual_recording in manual_gbpvr_recordings)
195     {
196     string title = manual_recording.filename;
197     DateTime start = manual_recording.manual_start_time;
198     DateTime end = manual_recording.manual_end_time;
199    
200     var found_program = xmltv_programs.ToList().Find(p =>
201     p.Title.ToLower() == title.ToLower() &&
202     p.Start == start.ToDateTimeString() &&
203     p.Stop == end.ToDateTimeString()
204     );
205    
206     if (found_program != null)
207     {
208     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());
209     }
210     else
211     {
212     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());
213     }
214     //bool entry_valid = false;
215     //if (found_programs.Count == 0)
216     //{
217     // gLog.Warn.WriteLine("Could not find an XMLTV Program with title: '{0}'", title);
218     //}
219     //foreach (var found_program in found_programs)
220     //{
221     // entry_valid = (found_program.Start == start.ToDateTimeString());
222     // entry_valid = (found_program.Stop == end.ToDateTimeString());
223     //}
224     //if (!entry_valid)
225     //{
226     // gLog.Warn.WriteLine("Recording: oid='{0}' programme_oid='{1}' title='{2}' start='{3}', end='{4}' may not be valid", manual_recording.oid, manual_recording.programme_oid, title, start.ToDateTimeString(), end.ToDateTimeString());
227     //}
228     }
229 william 158 }
230 william 159
231 william 158 static void CheckProgramsForDiscrepancies()
232     {
233     gLog.Warn.WriteLine("CheckProgramsForDiscrepancies() has not been implemented");
234     }
235 william 150 }
236     }

  ViewVC Help
Powered by ViewVC 1.1.22