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

Diff of /trunk/GBPVRProgramDatabaseFixer/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 154 by william, Thu Mar 14 16:56:05 2013 UTC revision 232 by william, Sun Mar 17 05:35:32 2013 UTC
# Line 2  Line 2 
2  using System;  using System;
3  using System.Collections.Generic;  using System.Collections.Generic;
4  using System.Text;  using System.Text;
5    using System.Linq;
6  using Enterprise.Logging;  using Enterprise.Logging;
7  using System.Windows.Forms;  using System.Windows.Forms;
8  using System.Diagnostics;  using System.Diagnostics;
9  using System.IO;  using System.IO;
10  using libxmltv.Core;  using libxmltv.Core;
11    using libxmltv.Interfaces;
12    using libxmltv;
13  namespace GBPVRProgramDatabaseFixer  namespace GBPVRProgramDatabaseFixer
14  {  {
15      class Program      class Program
16      {      {
17            static ISQLLITE sqlite;
18          static string XMLTV_FILE = "";          static string XMLTV_FILE = "";
19          static string GBPVR_DATABASE = "";          static string GBPVR_DATABASE = "";
20    
21            //static bool LOADING_XMLTV_FILE = false;
22          static bool XMLTV_INSTANCE_CREATED = false;          static bool XMLTV_INSTANCE_CREATED = false;
23            //static int LAST_REPORTED_XMLTV_PROGRESS = 0;
24    
         static int LAST_REPORTED_XMLTV_PROGRESS =0;  
25    
26          static FileInfo fi_XMLTVFILE;          //static bool LOADING_GBPVR_DATABASE = false;
27            static bool SQLLIST_INSTANCE_CREATED = false;
28            //static int LAST_REPORTED_SQLLITE_PROGRESS = 0;
29    
30            //static int LAST_REPORTED_PROGRESS = 0;
31    
32            static FileInfo fi_XMLTVFILE;
33            static FileInfo fi_GBPVRDATABASE;
34          static void CreateLog()          static void CreateLog()
35          {          {
36              string log_path = Application.StartupPath;              string log_path = Application.StartupPath;
# Line 78  namespace GBPVRProgramDatabaseFixer Line 89  namespace GBPVRProgramDatabaseFixer
89              }              }
90    
91              LoadXMLTVFile();              LoadXMLTVFile();
92                LoadGBPVRDatabase();
93                PerformDatabaseProcessing();
94            }
95    
96            static void ConsoleOverwritePreviosLine(string format, params object[] args)
97            {
98                Console.WriteLine(format,args);
99                Console.SetCursorPosition(0, Console.CursorTop - 1);
100          }          }
101    
102          static void gLog_ReportProgress(object sender, ReportProgressEventArgs e)          static void gLog_ReportProgress(object sender, ReportProgressEventArgs e)
103          {          {
104              if (LAST_REPORTED_XMLTV_PROGRESS != e.Progress)              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                {
107                    if (sender == null) { ConsoleOverwritePreviosLine("Progress: {0} ", e.Progress); }
108                    else { ConsoleOverwritePreviosLine("{0}: Progress: {1:00}%", sender.GetType().Name, e.Progress); }
109                }
110                else // assume we have a user status message
111              {              {
112                  Console.WriteLine("XMLTV: {0} Loading: {1:00}%", fi_XMLTVFILE.Name, e.Progress);                  if (sender == null) { ConsoleOverwritePreviosLine("{0} ", k.ToString()); }
113                  LAST_REPORTED_XMLTV_PROGRESS = e.Progress;                  else { ConsoleOverwritePreviosLine("{0}: {1}", sender.GetType().Name, k.ToString()); }
114              }              }
115          }          }
116          static void LoadXMLTVFile()          static void LoadXMLTVFile()
117          {          {
118                //LOADING_XMLTV_FILE = true;
119              if (string.IsNullOrEmpty(XMLTV_FILE))              if (string.IsNullOrEmpty(XMLTV_FILE))
120              {              {
121                  gLog.Error.WriteLine("XMLTV File is either a null or empty string.");                  gLog.Error.WriteLine("XMLTV File is either a null or empty string.");
122                    //LOADING_XMLTV_FILE = false;
123                  return;                  return;
124              }              }
125              fi_XMLTVFILE = new FileInfo(XMLTV_FILE);              fi_XMLTVFILE = new FileInfo(XMLTV_FILE);
126              if (!fi_XMLTVFILE.Exists)              if (!fi_XMLTVFILE.Exists)
127              {              {
128                  gLog.Error.WriteLine("Could not find XMLTV File: {0}", fi_XMLTVFILE.FullName);                  gLog.Error.WriteLine("Could not find XMLTV File: {0}", fi_XMLTVFILE.FullName);
129                    //LOADING_XMLTV_FILE = false;
130                  return;                  return;
131              }              }
132    
133              XMLTV.Create(fi_XMLTVFILE.FullName, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));              XMLTV.Create(fi_XMLTVFILE.FullName, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
134              while (!XMLTV_INSTANCE_CREATED) { Application.DoEvents(); }              while (!XMLTV_INSTANCE_CREATED) { Application.DoEvents(); }
135                //LOADING_XMLTV_FILE = false;
136          }          }
   
137          static void XMLTV_OnInstanceCreated(object sender, EventArgs e) { XMLTV_INSTANCE_CREATED = true; }          static void XMLTV_OnInstanceCreated(object sender, EventArgs e) { XMLTV_INSTANCE_CREATED = true; }
138    
139            static void LoadGBPVRDatabase()
140            {
141                //LOADING_GBPVR_DATABASE = true;
142                if (string.IsNullOrEmpty(GBPVR_DATABASE))
143                {
144                    gLog.Error.WriteLine("GBPVR Datbase is either a null or empty string.");
145                    //LOADING_GBPVR_DATABASE = false;
146                    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                    //LOADING_GBPVR_DATABASE = false;
153                    return;
154                }
155                sqlite = SQLLITE.Create(fi_GBPVRDATABASE.FullName, new EventHandler<EventArgs>(SQLLITE_OnInstanceCreated));
156                while (!SQLLIST_INSTANCE_CREATED) { Application.DoEvents(); }
157                //LOADING_GBPVR_DATABASE = false;
158            }
159            static void SQLLITE_OnInstanceCreated(object sender, EventArgs e) { SQLLIST_INSTANCE_CREATED = true; }
160    
161            static void PerformDatabaseProcessing()
162            {
163                var sqllite_channels = sqlite.Channels;
164                var sqllite_recordings = sqlite.Recordings;
165                var sqllite_programs = sqlite.Programs;
166                Type datatype;
167                var xmlt_raw_program_data = (IProgramDefinitionList)XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out datatype);
168                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                var xmltv_programs_startdate_range = GetXMLTVProgramStartDateRange(xmltv_programs);
171                var gbpvr_invalid_programs = new List<SQLLITE.IPROGRAMME>();
172                var gbpvr_valid_programs = sqlite.FixGBPVRProgramsDatabase(sqllite_programs, xmltv_programs, out gbpvr_invalid_programs);    
173                sqlite.RemoveOldGBPVRPrograms(gbpvr_invalid_programs);
174                sqlite.UpdateGBPVRPrograms(gbpvr_valid_programs);
175    
176            }
177    
178            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                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                range = DateTimeRange.Create(first, last);
189                return range;
190            }
191    
192            static void CheckScheduledRecordingsForDiscrepancies(List<SQLLITE.IRECORDING_SCHEDULE> gbpvr)
193            {
194            }
195    
196      }      }
197  }  }

Legend:
Removed from v.154  
changed lines
  Added in v.232

  ViewVC Help
Powered by ViewVC 1.1.22