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

Annotation of /trunk/GBPVRProgramDatabaseFixer/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 232 - (hide annotations) (download)
Sun Mar 17 05:35:32 2013 UTC (6 years, 8 months ago) by william
File size: 8981 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 163 static ISQLLITE sqlite;
18 william 153 static string XMLTV_FILE = "";
19     static string GBPVR_DATABASE = "";
20 william 155
21 william 166 //static bool LOADING_XMLTV_FILE = false;
22 william 153 static bool XMLTV_INSTANCE_CREATED = false;
23 william 166 //static int LAST_REPORTED_XMLTV_PROGRESS = 0;
24 william 153
25    
26 william 166 //static bool LOADING_GBPVR_DATABASE = false;
27 william 155 static bool SQLLIST_INSTANCE_CREATED = false;
28 william 166 //static int LAST_REPORTED_SQLLITE_PROGRESS = 0;
29 william 155
30 william 212 //static int LAST_REPORTED_PROGRESS = 0;
31 william 166
32 william 153 static FileInfo fi_XMLTVFILE;
33 william 155 static FileInfo fi_GBPVRDATABASE;
34 william 153 static void CreateLog()
35     {
36     string log_path = Application.StartupPath;
37     string log_filename = string.Format("{0}.log", typeof(Program).Assembly.GetName().Name);
38     gLog.CreateLog(string.Format(@"{0}\{1}", log_path, log_filename), false, LogLevel.kLogLevel_All);
39     #if DEBUG
40     LogLevel gLevel = gLog.LogLevel;
41     gLevel |= LogLevel.kLogLevel_VerboseDebug;
42     gLevel |= LogLevel.kLogLevel_Debug;
43     gLog.SetLogLevel(gLevel);
44     #else
45     LogLevel gLevel = LogLevel.kLogLevel_Default; // set the default log level: Info, Warn, Error, Debug
46     // it is OK for kLogLevel_Debug to be set in Release mode ... must of the chatty messages are from kLogLevel_VerboseDebug
47     gLevel &= ~LogLevel.kLogLevel_Debug; // ensure this is not set, ever in release mode
48     gLevel &= ~LogLevel.kLogLevel_VerboseDebug; // ensure this is not set, ever in release mode
49     gLog.SetLogLevel(gLevel);
50     #endif
51     gLog.ReportProgressEvent += new EventHandler<ReportProgressEventArgs>(gLog_ReportProgress);
52     }
53    
54 william 154 [Conditional("OVERRIDE_COMMANDLINE_ARGUMENTS")]
55 william 153 static void CreateDebugCommandlineArgs(ref List<string> cargs)
56     {
57     cargs = new List<string>();
58     cargs.Add(string.Format("--xmltv_file=\"{0}\"", @"c:\EPG Guide\Guide\tvguide.xml"));
59     cargs.Add(string.Format("--gbprv_db=\"{0}\"", @"c:\Program Files (x86)\Devnz\GBPVR\gbpvr.db3"));
60     }
61    
62     static List<string> CreateCommandlineArgs(string[] args)
63     {
64     List<string> cargs = new List<string>(args);
65     CreateDebugCommandlineArgs(ref cargs);
66     return cargs;
67     }
68    
69 william 150 static void Main(string[] args)
70     {
71 william 153 CreateLog();
72     var cargs = CreateCommandlineArgs(args);
73     foreach (var carg in cargs)
74     {
75     if (carg.ToLower().Contains("--xmltv_file="))
76     {
77     XMLTV_FILE = carg.Replace("--xmltv_file=", "").Replace("\"","");
78     gLog.Info.WriteLine("XMLTV FILE: '{0}'", XMLTV_FILE);
79     }
80     else if (carg.ToLower().Contains("--gbprv_db="))
81     {
82     GBPVR_DATABASE = carg.Replace("--gbprv_db=", "").Replace("\"", "");
83     gLog.Info.WriteLine("GBPVR DATABASE: '{0}'", GBPVR_DATABASE);
84     }
85     else
86     {
87     gLog.Warn.WriteLine("Unknown commandline option: {0}", carg);
88     }
89     }
90    
91 william 157 LoadXMLTVFile();
92 william 155 LoadGBPVRDatabase();
93 william 163 PerformDatabaseProcessing();
94 william 150 }
95 william 153
96 william 155 static void ConsoleOverwritePreviosLine(string format, params object[] args)
97     {
98     Console.WriteLine(format,args);
99     Console.SetCursorPosition(0, Console.CursorTop - 1);
100     }
101    
102 william 232 static void gLog_ReportProgress(object sender, ReportProgressEventArgs e)
103 william 153 {
104 william 205 object k = e.UserState; // we will assum that is a status message
105     if (k == null) // assume we do not have a user status message (ie: is only a progress message)
106 william 153 {
107 william 232 if (sender == null) { ConsoleOverwritePreviosLine("Progress: {0} ", e.Progress); }
108     else { ConsoleOverwritePreviosLine("{0}: Progress: {1:00}%", sender.GetType().Name, e.Progress); }
109 william 153 }
110 william 205 else // assume we have a user status message
111 william 155 {
112 william 232 if (sender == null) { ConsoleOverwritePreviosLine("{0} ", k.ToString()); }
113     else { ConsoleOverwritePreviosLine("{0}: {1}", sender.GetType().Name, k.ToString()); }
114 william 155 }
115 william 153 }
116     static void LoadXMLTVFile()
117     {
118 william 166 //LOADING_XMLTV_FILE = true;
119 william 153 if (string.IsNullOrEmpty(XMLTV_FILE))
120     {
121     gLog.Error.WriteLine("XMLTV File is either a null or empty string.");
122 william 166 //LOADING_XMLTV_FILE = false;
123 william 153 return;
124     }
125     fi_XMLTVFILE = new FileInfo(XMLTV_FILE);
126     if (!fi_XMLTVFILE.Exists)
127     {
128     gLog.Error.WriteLine("Could not find XMLTV File: {0}", fi_XMLTVFILE.FullName);
129 william 166 //LOADING_XMLTV_FILE = false;
130 william 153 return;
131     }
132    
133     XMLTV.Create(fi_XMLTVFILE.FullName, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
134     while (!XMLTV_INSTANCE_CREATED) { Application.DoEvents(); }
135 william 166 //LOADING_XMLTV_FILE = false;
136 william 153 }
137 william 155 static void XMLTV_OnInstanceCreated(object sender, EventArgs e) { XMLTV_INSTANCE_CREATED = true; }
138 william 153
139 william 155 static void LoadGBPVRDatabase()
140     {
141 william 166 //LOADING_GBPVR_DATABASE = true;
142 william 155 if (string.IsNullOrEmpty(GBPVR_DATABASE))
143     {
144     gLog.Error.WriteLine("GBPVR Datbase is either a null or empty string.");
145 william 166 //LOADING_GBPVR_DATABASE = false;
146 william 155 return;
147     }
148     fi_GBPVRDATABASE = new FileInfo(GBPVR_DATABASE);
149     if (!fi_GBPVRDATABASE.Exists)
150     {
151     gLog.Error.WriteLine("Could not find GBPVR Database: {0}", fi_GBPVRDATABASE.FullName);
152 william 166 //LOADING_GBPVR_DATABASE = false;
153 william 155 return;
154     }
155 william 163 sqlite = SQLLITE.Create(fi_GBPVRDATABASE.FullName, new EventHandler<EventArgs>(SQLLITE_OnInstanceCreated));
156 william 156 while (!SQLLIST_INSTANCE_CREATED) { Application.DoEvents(); }
157 william 166 //LOADING_GBPVR_DATABASE = false;
158 william 155 }
159 william 156 static void SQLLITE_OnInstanceCreated(object sender, EventArgs e) { SQLLIST_INSTANCE_CREATED = true; }
160 william 157
161     static void PerformDatabaseProcessing()
162     {
163 william 166 var sqllite_channels = sqlite.Channels;
164     var sqllite_recordings = sqlite.Recordings;
165     var sqllite_programs = sqlite.Programs;
166 william 159 Type datatype;
167 william 162 var xmlt_raw_program_data = (IProgramDefinitionList)XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out datatype);
168 william 166 var xmltv_programs = xmlt_raw_program_data.ToList().OrderBy(p => DateTime.Parse(p.Start)).ToList();
169     var sqllite_programs_startdate_range = sqlite.GetProgramsDateRange(sqllite_programs);
170 william 165 var xmltv_programs_startdate_range = GetXMLTVProgramStartDateRange(xmltv_programs);
171 william 166 var gbpvr_invalid_programs = new List<SQLLITE.IPROGRAMME>();
172 william 232 var gbpvr_valid_programs = sqlite.FixGBPVRProgramsDatabase(sqllite_programs, xmltv_programs, out gbpvr_invalid_programs);
173 william 183 sqlite.RemoveOldGBPVRPrograms(gbpvr_invalid_programs);
174     sqlite.UpdateGBPVRPrograms(gbpvr_valid_programs);
175    
176 william 157 }
177 william 158
178 william 163 static IDateTimeRange GetXMLTVProgramStartDateRange(List<libxmltv.Interfaces.IProgramDefinition> programs)
179     {
180     IDateTimeRange range = DateTimeRange.Create();
181     var list = new List<libxmltv.Interfaces.IProgramDefinition>(programs.ToArray());
182     DateTime first = new DateTime();
183     DateTime last = new DateTime();
184     first = DateTime.Parse(list.OrderBy(s => DateTime.Parse(s.Start)).ToList().First().Start);
185     last = DateTime.Parse(list.OrderBy(s => DateTime.Parse(s.Start)).ToList().Last().Start);
186 william 167 gLog.Verbose.Debug.WriteLine("\tFirst: {0} = ({1})", first.ToString("yyyy/MM/dd HH:mm:ss.fffffff"), first.ToDateTimeString());
187     gLog.Verbose.Debug.WriteLine("\tLast: {0} = ({1})", last.ToString("yyyy/MM/dd HH:mm:ss.fffffff"), last.ToDateTimeString());
188 william 163 range = DateTimeRange.Create(first, last);
189     return range;
190     }
191    
192 william 165 static void CheckScheduledRecordingsForDiscrepancies(List<SQLLITE.IRECORDING_SCHEDULE> gbpvr)
193 william 158 {
194     }
195 william 159
196 william 150 }
197     }

  ViewVC Help
Powered by ViewVC 1.1.22