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

Annotation of /trunk/GBPVRProgramDatabaseFixer/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 241 - (hide annotations) (download)
Sun Mar 17 10:40:09 2013 UTC (6 years, 8 months ago) by william
File size: 13509 byte(s)
+ Truncate Log file with it grows over 250kb in size

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 233 using GBPVR.Public;
14     using GBPVR.Backend.Common;
15 william 150 namespace GBPVRProgramDatabaseFixer
16     {
17     class Program
18     {
19 william 241 const long MAX_LOG_FILESIZE_IN_BYTES = 250000; //250kb max
20     static void TruncateLogFile(FileInfo log_file)
21     {
22     if (!log_file.Exists)
23     {
24     gLog.CreateLog(log_file.FullName, false, LogLevel.kLogLevel_All);
25     }
26     else
27     {
28     if (log_file.Length > MAX_LOG_FILESIZE_IN_BYTES)
29     {
30     string match = string.Format("{0}.*", log_file.Name);
31     var files = Directory.GetFiles(log_file.Directory.FullName, match).ToList();
32     files.RemoveAt(0);
33     files.TrimExcess();
34     int count = files.Count;
35     if (log_file.Exists)
36     {
37     File.Copy(log_file.FullName, string.Format("{0}.{1}", log_file.FullName, count));
38     }
39     gLog.CreateLog(log_file.FullName, true, LogLevel.kLogLevel_All);
40     }
41     else
42     {
43     gLog.CreateLog(log_file.FullName, false, LogLevel.kLogLevel_All);
44     }
45     }
46     }
47    
48 william 163 static ISQLLITE sqlite;
49 william 153 static string XMLTV_FILE = "";
50     static string GBPVR_DATABASE = "";
51 william 155
52 william 166 //static bool LOADING_XMLTV_FILE = false;
53 william 153 static bool XMLTV_INSTANCE_CREATED = false;
54 william 166 //static int LAST_REPORTED_XMLTV_PROGRESS = 0;
55 william 153
56    
57 william 166 //static bool LOADING_GBPVR_DATABASE = false;
58 william 155 static bool SQLLIST_INSTANCE_CREATED = false;
59 william 166 //static int LAST_REPORTED_SQLLITE_PROGRESS = 0;
60 william 155
61 william 212 //static int LAST_REPORTED_PROGRESS = 0;
62 william 166
63 william 153 static FileInfo fi_XMLTVFILE;
64 william 155 static FileInfo fi_GBPVRDATABASE;
65 william 153 static void CreateLog()
66     {
67     string log_path = Application.StartupPath;
68     string log_filename = string.Format("{0}.log", typeof(Program).Assembly.GetName().Name);
69 william 241 FileInfo log_file = new FileInfo(string.Format(@"{0}\{1}", log_path, log_filename));
70     TruncateLogFile(log_file);
71 william 153 #if DEBUG
72     LogLevel gLevel = gLog.LogLevel;
73     gLevel |= LogLevel.kLogLevel_VerboseDebug;
74     gLevel |= LogLevel.kLogLevel_Debug;
75     gLog.SetLogLevel(gLevel);
76     #else
77     LogLevel gLevel = LogLevel.kLogLevel_Default; // set the default log level: Info, Warn, Error, Debug
78     // it is OK for kLogLevel_Debug to be set in Release mode ... must of the chatty messages are from kLogLevel_VerboseDebug
79     gLevel &= ~LogLevel.kLogLevel_Debug; // ensure this is not set, ever in release mode
80     gLevel &= ~LogLevel.kLogLevel_VerboseDebug; // ensure this is not set, ever in release mode
81     gLog.SetLogLevel(gLevel);
82     #endif
83     gLog.ReportProgressEvent += new EventHandler<ReportProgressEventArgs>(gLog_ReportProgress);
84     }
85    
86 william 154 [Conditional("OVERRIDE_COMMANDLINE_ARGUMENTS")]
87 william 153 static void CreateDebugCommandlineArgs(ref List<string> cargs)
88     {
89     cargs = new List<string>();
90     cargs.Add(string.Format("--xmltv_file=\"{0}\"", @"c:\EPG Guide\Guide\tvguide.xml"));
91     cargs.Add(string.Format("--gbprv_db=\"{0}\"", @"c:\Program Files (x86)\Devnz\GBPVR\gbpvr.db3"));
92     }
93    
94     static List<string> CreateCommandlineArgs(string[] args)
95     {
96     List<string> cargs = new List<string>(args);
97     CreateDebugCommandlineArgs(ref cargs);
98     return cargs;
99     }
100    
101 william 150 static void Main(string[] args)
102     {
103 william 153 CreateLog();
104     var cargs = CreateCommandlineArgs(args);
105     foreach (var carg in cargs)
106     {
107     if (carg.ToLower().Contains("--xmltv_file="))
108     {
109     XMLTV_FILE = carg.Replace("--xmltv_file=", "").Replace("\"","");
110     gLog.Info.WriteLine("XMLTV FILE: '{0}'", XMLTV_FILE);
111     }
112     else if (carg.ToLower().Contains("--gbprv_db="))
113     {
114     GBPVR_DATABASE = carg.Replace("--gbprv_db=", "").Replace("\"", "");
115     gLog.Info.WriteLine("GBPVR DATABASE: '{0}'", GBPVR_DATABASE);
116     }
117     else
118     {
119     gLog.Warn.WriteLine("Unknown commandline option: {0}", carg);
120     }
121     }
122    
123 william 157 LoadXMLTVFile();
124 william 155 LoadGBPVRDatabase();
125 william 163 PerformDatabaseProcessing();
126 william 150 }
127 william 153
128 william 155 static void ConsoleOverwritePreviosLine(string format, params object[] args)
129     {
130     Console.WriteLine(format,args);
131     Console.SetCursorPosition(0, Console.CursorTop - 1);
132     }
133    
134 william 232 static void gLog_ReportProgress(object sender, ReportProgressEventArgs e)
135 william 153 {
136 william 205 object k = e.UserState; // we will assum that is a status message
137     if (k == null) // assume we do not have a user status message (ie: is only a progress message)
138 william 153 {
139 william 232 if (sender == null) { ConsoleOverwritePreviosLine("Progress: {0} ", e.Progress); }
140     else { ConsoleOverwritePreviosLine("{0}: Progress: {1:00}%", sender.GetType().Name, e.Progress); }
141 william 153 }
142 william 205 else // assume we have a user status message
143 william 155 {
144 william 232 if (sender == null) { ConsoleOverwritePreviosLine("{0} ", k.ToString()); }
145     else { ConsoleOverwritePreviosLine("{0}: {1}", sender.GetType().Name, k.ToString()); }
146 william 155 }
147 william 153 }
148     static void LoadXMLTVFile()
149     {
150 william 166 //LOADING_XMLTV_FILE = true;
151 william 153 if (string.IsNullOrEmpty(XMLTV_FILE))
152     {
153     gLog.Error.WriteLine("XMLTV File is either a null or empty string.");
154 william 166 //LOADING_XMLTV_FILE = false;
155 william 153 return;
156     }
157     fi_XMLTVFILE = new FileInfo(XMLTV_FILE);
158     if (!fi_XMLTVFILE.Exists)
159     {
160     gLog.Error.WriteLine("Could not find XMLTV File: {0}", fi_XMLTVFILE.FullName);
161 william 166 //LOADING_XMLTV_FILE = false;
162 william 153 return;
163     }
164    
165     XMLTV.Create(fi_XMLTVFILE.FullName, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
166     while (!XMLTV_INSTANCE_CREATED) { Application.DoEvents(); }
167 william 166 //LOADING_XMLTV_FILE = false;
168 william 153 }
169 william 155 static void XMLTV_OnInstanceCreated(object sender, EventArgs e) { XMLTV_INSTANCE_CREATED = true; }
170 william 153
171 william 155 static void LoadGBPVRDatabase()
172     {
173 william 166 //LOADING_GBPVR_DATABASE = true;
174 william 155 if (string.IsNullOrEmpty(GBPVR_DATABASE))
175     {
176     gLog.Error.WriteLine("GBPVR Datbase is either a null or empty string.");
177 william 166 //LOADING_GBPVR_DATABASE = false;
178 william 155 return;
179     }
180     fi_GBPVRDATABASE = new FileInfo(GBPVR_DATABASE);
181     if (!fi_GBPVRDATABASE.Exists)
182     {
183     gLog.Error.WriteLine("Could not find GBPVR Database: {0}", fi_GBPVRDATABASE.FullName);
184 william 166 //LOADING_GBPVR_DATABASE = false;
185 william 155 return;
186     }
187 william 163 sqlite = SQLLITE.Create(fi_GBPVRDATABASE.FullName, new EventHandler<EventArgs>(SQLLITE_OnInstanceCreated));
188 william 156 while (!SQLLIST_INSTANCE_CREATED) { Application.DoEvents(); }
189 william 166 //LOADING_GBPVR_DATABASE = false;
190 william 155 }
191 william 156 static void SQLLITE_OnInstanceCreated(object sender, EventArgs e) { SQLLIST_INSTANCE_CREATED = true; }
192 william 157
193     static void PerformDatabaseProcessing()
194     {
195 william 166 var sqllite_channels = sqlite.Channels;
196     var sqllite_recordings = sqlite.Recordings;
197     var sqllite_programs = sqlite.Programs;
198 william 159 Type datatype;
199 william 162 var xmlt_raw_program_data = (IProgramDefinitionList)XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out datatype);
200 william 166 var xmltv_programs = xmlt_raw_program_data.ToList().OrderBy(p => DateTime.Parse(p.Start)).ToList();
201     var sqllite_programs_startdate_range = sqlite.GetProgramsDateRange(sqllite_programs);
202 william 165 var xmltv_programs_startdate_range = GetXMLTVProgramStartDateRange(xmltv_programs);
203 william 166 var gbpvr_invalid_programs = new List<SQLLITE.IPROGRAMME>();
204 william 232 var gbpvr_valid_programs = sqlite.FixGBPVRProgramsDatabase(sqllite_programs, xmltv_programs, out gbpvr_invalid_programs);
205 william 183 sqlite.RemoveOldGBPVRPrograms(gbpvr_invalid_programs);
206     sqlite.UpdateGBPVRPrograms(gbpvr_valid_programs);
207    
208 william 233 sqllite_programs.Clear();
209     gbpvr_valid_programs.ForEach(s=>sqllite_programs.Add(s.NewProgram));
210    
211     CheckScheduledRecordingsForDiscrepancies(sqllite_recordings,sqllite_programs);
212 william 157 }
213 william 158
214 william 163 static IDateTimeRange GetXMLTVProgramStartDateRange(List<libxmltv.Interfaces.IProgramDefinition> programs)
215     {
216     IDateTimeRange range = DateTimeRange.Create();
217     var list = new List<libxmltv.Interfaces.IProgramDefinition>(programs.ToArray());
218     DateTime first = new DateTime();
219     DateTime last = new DateTime();
220     first = DateTime.Parse(list.OrderBy(s => DateTime.Parse(s.Start)).ToList().First().Start);
221     last = DateTime.Parse(list.OrderBy(s => DateTime.Parse(s.Start)).ToList().Last().Start);
222 william 167 gLog.Verbose.Debug.WriteLine("\tFirst: {0} = ({1})", first.ToString("yyyy/MM/dd HH:mm:ss.fffffff"), first.ToDateTimeString());
223     gLog.Verbose.Debug.WriteLine("\tLast: {0} = ({1})", last.ToString("yyyy/MM/dd HH:mm:ss.fffffff"), last.ToDateTimeString());
224 william 163 range = DateTimeRange.Create(first, last);
225     return range;
226     }
227    
228 william 233 static void CheckScheduledRecordingsForDiscrepancies(List<SQLLITE.IRECORDING_SCHEDULE> recordings, List<SQLLITE.IPROGRAMME> programs)
229 william 158 {
230 william 233 List<SQLLITE.IRECORDING_SCHEDULE> recordings_to_delete = new List<SQLLITE.IRECORDING_SCHEDULE>();
231     //List<SQLLITE.IRECORDING_SCHEDULE> deleted_recordings = new List<SQLLITE.IRECORDING_SCHEDULE>();
232     //List<SQLLITE.IRECORDING_SCHEDULE> reocurring_recordings = new List<SQLLITE.IRECORDING_SCHEDULE>();
233    
234    
235     //reocurring_recordings = recordings.FindAll(s => s.recording_type != (long)RecordingType.TYPE_RECORD_ONCE).ToList();
236    
237     foreach (var recording in recordings)
238     {
239     RecordingStatus status = (RecordingStatus)recording.status;
240     RecordingType type = (RecordingType)recording.recording_type;
241     if (type == RecordingType.TYPE_RECORD_SEASON)
242     {
243     continue; // ignore TYPE_RECORD_SEASON
244     }
245     if (status != RecordingStatus.STATUS_PENDING)
246     {
247     continue; // ignore any status except STATUS_PENDING
248     }
249     if (recording.recording_group == 0 && recording.programme_oid == 0)
250     {
251     continue; // ignore manual recordings
252     }
253     // verify data for this recording
254     var recording_oid = recording.oid;
255     var programme_oid = recording.programme_oid;
256     var filename = recording.filename;
257     var manual_start_time = recording.manual_start_time;
258     var manual_end_time = recording.manual_end_time;
259     var manual_channel_oid = recording.manual_channel_oid;
260    
261     // lookup program by oid
262     var entry = programs.Find(s => s.oid == programme_oid);
263     if (entry == null)
264     {
265     gLog.Warn.WriteLine("Unable to find program with oid: '{0}' for recording with oid: '{1}' -- assuming it should be deleted", programme_oid, recording_oid);
266     //throw new NullReferenceException(string.Format("Unable to find program with oid: '{0}' for recording with oid: '{1}'", programme_oid, recording_oid));
267     //deleted_recordings.Add(recording);
268     }
269     else
270     {
271     recordings_to_delete.Add(recording);
272     }
273     }
274     recordings_to_delete.TrimExcess();
275     if (recordings_to_delete.Count > 0)
276     {
277     gLog.Info.WriteLine("Found {0} recordings to delete.", recordings_to_delete.Count);
278     sqlite.DeleteGBPVRScheduledRecordings(recordings_to_delete);
279     }
280    
281     try
282     {
283 william 239
284     // taken from: Config.EpgUpdateProgress.method_1()
285 william 238 Zap2itEpgReader.smethod_0();
286     GClass4.smethod_0().method_14();
287 william 237
288 william 233 }
289     catch (Exception ex)
290     {
291     gLog.Error.WriteLine(ex.ToString());
292     }
293 william 158 }
294 william 150 }
295     }

  ViewVC Help
Powered by ViewVC 1.1.22