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

Annotation of /trunk/GBPVRProgramDatabaseFixer/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22