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

Annotation of /trunk/GBPVRProgramDatabaseFixer/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 247 - (hide annotations) (download)
Wed Mar 20 06:54:00 2013 UTC (6 years, 8 months ago) by william
File size: 13620 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 233 using GBPVR.Backend.Common;
14 william 150 namespace GBPVRProgramDatabaseFixer
15     {
16     class Program
17     {
18 william 241 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 william 163 static ISQLLITE sqlite;
48 william 153 static string XMLTV_FILE = "";
49     static string GBPVR_DATABASE = "";
50 william 155
51 william 166 //static bool LOADING_XMLTV_FILE = false;
52 william 153 static bool XMLTV_INSTANCE_CREATED = false;
53 william 166 //static int LAST_REPORTED_XMLTV_PROGRESS = 0;
54 william 153
55    
56 william 166 //static bool LOADING_GBPVR_DATABASE = false;
57 william 155 static bool SQLLIST_INSTANCE_CREATED = false;
58 william 166 //static int LAST_REPORTED_SQLLITE_PROGRESS = 0;
59 william 155
60 william 212 //static int LAST_REPORTED_PROGRESS = 0;
61 william 166
62 william 153 static FileInfo fi_XMLTVFILE;
63 william 155 static FileInfo fi_GBPVRDATABASE;
64 william 153 static void CreateLog()
65     {
66     string log_path = Application.StartupPath;
67     string log_filename = string.Format("{0}.log", typeof(Program).Assembly.GetName().Name);
68 william 241 FileInfo log_file = new FileInfo(string.Format(@"{0}\{1}", log_path, log_filename));
69     TruncateLogFile(log_file);
70 william 153 #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 william 154 [Conditional("OVERRIDE_COMMANDLINE_ARGUMENTS")]
86 william 153 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 william 150 static void Main(string[] args)
101     {
102 william 153 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 william 157 LoadXMLTVFile();
123 william 155 LoadGBPVRDatabase();
124 william 163 PerformDatabaseProcessing();
125 william 150 }
126 william 153
127 william 155 static void ConsoleOverwritePreviosLine(string format, params object[] args)
128     {
129     Console.WriteLine(format,args);
130     Console.SetCursorPosition(0, Console.CursorTop - 1);
131     }
132    
133 william 232 static void gLog_ReportProgress(object sender, ReportProgressEventArgs e)
134 william 153 {
135 william 205 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 william 153 {
138 william 232 if (sender == null) { ConsoleOverwritePreviosLine("Progress: {0} ", e.Progress); }
139     else { ConsoleOverwritePreviosLine("{0}: Progress: {1:00}%", sender.GetType().Name, e.Progress); }
140 william 153 }
141 william 205 else // assume we have a user status message
142 william 155 {
143 william 232 if (sender == null) { ConsoleOverwritePreviosLine("{0} ", k.ToString()); }
144     else { ConsoleOverwritePreviosLine("{0}: {1}", sender.GetType().Name, k.ToString()); }
145 william 155 }
146 william 153 }
147     static void LoadXMLTVFile()
148     {
149 william 166 //LOADING_XMLTV_FILE = true;
150 william 153 if (string.IsNullOrEmpty(XMLTV_FILE))
151     {
152     gLog.Error.WriteLine("XMLTV File is either a null or empty string.");
153 william 166 //LOADING_XMLTV_FILE = false;
154 william 153 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 william 166 //LOADING_XMLTV_FILE = false;
161 william 153 return;
162     }
163    
164     XMLTV.Create(fi_XMLTVFILE.FullName, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
165     while (!XMLTV_INSTANCE_CREATED) { Application.DoEvents(); }
166 william 166 //LOADING_XMLTV_FILE = false;
167 william 153 }
168 william 155 static void XMLTV_OnInstanceCreated(object sender, EventArgs e) { XMLTV_INSTANCE_CREATED = true; }
169 william 153
170 william 155 static void LoadGBPVRDatabase()
171     {
172 william 166 //LOADING_GBPVR_DATABASE = true;
173 william 155 if (string.IsNullOrEmpty(GBPVR_DATABASE))
174     {
175     gLog.Error.WriteLine("GBPVR Datbase is either a null or empty string.");
176 william 166 //LOADING_GBPVR_DATABASE = false;
177 william 155 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 william 166 //LOADING_GBPVR_DATABASE = false;
184 william 155 return;
185     }
186 william 163 sqlite = SQLLITE.Create(fi_GBPVRDATABASE.FullName, new EventHandler<EventArgs>(SQLLITE_OnInstanceCreated));
187 william 156 while (!SQLLIST_INSTANCE_CREATED) { Application.DoEvents(); }
188 william 166 //LOADING_GBPVR_DATABASE = false;
189 william 155 }
190 william 156 static void SQLLITE_OnInstanceCreated(object sender, EventArgs e) { SQLLIST_INSTANCE_CREATED = true; }
191 william 157
192     static void PerformDatabaseProcessing()
193     {
194 william 166 var sqllite_channels = sqlite.Channels;
195     var sqllite_recordings = sqlite.Recordings;
196     var sqllite_programs = sqlite.Programs;
197 william 159 Type datatype;
198 william 162 var xmlt_raw_program_data = (IProgramDefinitionList)XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out datatype);
199 william 166 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 william 165 var xmltv_programs_startdate_range = GetXMLTVProgramStartDateRange(xmltv_programs);
202 william 166 var gbpvr_invalid_programs = new List<SQLLITE.IPROGRAMME>();
203 william 232 var gbpvr_valid_programs = sqlite.FixGBPVRProgramsDatabase(sqllite_programs, xmltv_programs, out gbpvr_invalid_programs);
204 william 183 sqlite.RemoveOldGBPVRPrograms(gbpvr_invalid_programs);
205     sqlite.UpdateGBPVRPrograms(gbpvr_valid_programs);
206    
207 william 233 sqllite_programs.Clear();
208     gbpvr_valid_programs.ForEach(s=>sqllite_programs.Add(s.NewProgram));
209    
210     CheckScheduledRecordingsForDiscrepancies(sqllite_recordings,sqllite_programs);
211 william 157 }
212 william 158
213 william 163 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 william 167 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 william 163 range = DateTimeRange.Create(first, last);
224     return range;
225     }
226    
227 william 233 static void CheckScheduledRecordingsForDiscrepancies(List<SQLLITE.IRECORDING_SCHEDULE> recordings, List<SQLLITE.IPROGRAMME> programs)
228 william 158 {
229 william 233 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     if (type == RecordingType.TYPE_RECORD_SEASON)
241     {
242     continue; // ignore TYPE_RECORD_SEASON
243     }
244     if (status != RecordingStatus.STATUS_PENDING)
245     {
246     continue; // ignore any status except STATUS_PENDING
247     }
248     if (recording.recording_group == 0 && recording.programme_oid == 0)
249     {
250     continue; // ignore manual recordings
251     }
252     // verify data for this recording
253     var recording_oid = recording.oid;
254     var programme_oid = recording.programme_oid;
255     var filename = recording.filename;
256     var manual_start_time = recording.manual_start_time;
257     var manual_end_time = recording.manual_end_time;
258     var manual_channel_oid = recording.manual_channel_oid;
259    
260     // lookup program by oid
261     var entry = programs.Find(s => s.oid == programme_oid);
262     if (entry == null)
263     {
264     gLog.Warn.WriteLine("Unable to find program with oid: '{0}' for recording with oid: '{1}' -- assuming it should be deleted", programme_oid, recording_oid);
265     //throw new NullReferenceException(string.Format("Unable to find program with oid: '{0}' for recording with oid: '{1}'", programme_oid, recording_oid));
266     //deleted_recordings.Add(recording);
267     }
268     else
269     {
270     recordings_to_delete.Add(recording);
271     }
272     }
273     recordings_to_delete.TrimExcess();
274     if (recordings_to_delete.Count > 0)
275     {
276     gLog.Info.WriteLine("Found {0} recordings to delete.", recordings_to_delete.Count);
277     sqlite.DeleteGBPVRScheduledRecordings(recordings_to_delete);
278     }
279    
280     try
281     {
282 william 239
283     // taken from: Config.EpgUpdateProgress.method_1()
284 william 247 //Zap2itEpgReader.smethod_0();
285     //GClass4.smethod_0().method_14();
286     Zap2itEpgReader.x9a6781c43845a63b();
287     x396614b3dae3b1da.x479a7b07c9d5bd70().xf03e555cf1814572();
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