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

Contents of /trunk/GBPVRProgramDatabaseFixer/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 250 - (show annotations) (download)
Thu Mar 28 06:58:11 2013 UTC (6 years, 5 months ago) by william
File size: 13689 byte(s)
+ fix issue(s) with manual recordings being deleted/updated/changed ... this was only intended for re-ocurring recordings

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

  ViewVC Help
Powered by ViewVC 1.1.22