Beispiel #1
0
        /// <summary>
        /// Starts the importation process
        /// </summary>
        /// <returns>bool - sucess/fail</returns>
        public bool Import()
        {
            _status.Status    = "Loading configuration...";
            _status.Channels  = 0;
            _status.Programs  = 0;
            _status.StartTime = DateTime.Now;
            _status.EndTime   = new DateTime(1971, 11, 6);
            if (ShowProgress != null)
            {
                ShowProgress(_status);
            }

            if (!LoadConfig())
            {
                return(false);
            }

            _epgDataSink.Open();

            // Collect HttpStatistic
            IHttpStatistics httpStats;

            if (GlobalServiceProvider.Instance.IsRegistered <IHttpStatistics>())
            {
                httpStats = GlobalServiceProvider.Instance.Get <IHttpStatistics>();
            }
            else
            {
                httpStats = new HttpStatistics();
                GlobalServiceProvider.Instance.Add <IHttpStatistics>(httpStats);
            }

            Log.Info("WebEPG: Loading Channel Config");
            _grabList = new Dictionary <string, List <grabInfo> >();
            // for each channel write info xmltv file.
            List <ChannelMap> allChannels = new List <ChannelMap>(_config.Channels);

            allChannels.AddRange(_config.RadioChannels);
            foreach (ChannelMap channel in allChannels)
            {
                if (channel.id == null && channel.merged == null)
                {
                    Log.Info(" Ignoring Channel Name: {0} - No Channel id", channel.displayName);
                    continue;
                }

                if (channel.merged == null || channel.merged.Count == 0)
                {
                    if (channel.grabber != null)
                    {
                        Log.Debug(" Loading Channel {0} ID: {1}", channel.displayName, channel.id);
                        //xmltv.WriteChannel(channel.id, channel.displayName);
                        _epgDataSink.WriteChannel(channel.id, channel.displayName);

                        grabInfo grab = new grabInfo();
                        grab.name     = channel.displayName;
                        grab.id       = channel.id;
                        grab.grabber  = channel.grabber;
                        grab.merged   = false;
                        grab.linked   = true;
                        grab.linkTime = new TimeRange("00:00", "23:00");

                        if (!_grabList.ContainsKey(channel.id))
                        {
                            List <grabInfo> grabs = new List <grabInfo>();
                            grabs.Add(grab);
                            _grabList.Add(channel.id, grabs);
                        }
                        else
                        {
                            _grabList[channel.id].Add(grab);
                        }
                    }
                    else
                    {
                        Log.Info(" Ignoring Channel Name: {0} - No Grabber id", channel.displayName);
                    }
                }
                else
                {
                    Log.Debug(" Loading Merged Channel {0}", channel.displayName);
                    //xmltv.WriteChannel("[Merged]", channel.displayName);
                    _epgDataSink.WriteChannel("[Merged]", channel.displayName);

                    foreach (MergedChannel merged in channel.merged)
                    {
                        if (merged.grabber != null)
                        {
                            grabInfo grab = new grabInfo();
                            grab.name     = channel.displayName;
                            grab.id       = merged.id;
                            grab.grabber  = merged.grabber;
                            grab.merged   = true;
                            grab.linked   = true;
                            grab.linkTime = new TimeRange(merged.start, merged.end);
                            Log.Debug("  Loading Merged Sub-channel: {0} Time range: {1}", merged.id,
                                      grab.linkTime.ToString());

                            if (!_grabList.ContainsKey(merged.id))
                            {
                                List <grabInfo> grabs = new List <grabInfo>();
                                grabs.Add(grab);
                                _grabList.Add(merged.id, grabs);
                            }
                            else
                            {
                                _grabList[merged.id].Add(grab);
                            }
                        }
                        else
                        {
                            Log.Info("  Ignoring Merged Sub-channel: {0}/{1} - No Grabber id", channel.displayName,
                                     merged.id);
                        }
                    }
                }
            }

            string             grabberLast = "";
            List <ProgramData> programs    = null;
            bool initResult = false;

            _epgGrabber = new WebListingGrabber(_baseDirectory + "\\WebEPG\\grabbers\\");

            // For each channel get listing
            int i = 1;

            foreach (string channelid in _grabList.Keys)
            {
                _status.Status = string.Format("Getting Channel ID: {0} [{1} of {2}]", channelid, i, _grabList.Count);
                if (ShowProgress != null)
                {
                    ShowProgress(_status);
                }

                Log.Info("WebEPG: Getting Channel ID: {0}", channelid);
                Log.Info("        [{0} of {1}]", i++, _grabList.Count);

                if (_grabList[channelid].Count > 0)
                {
                    if (_grabList[channelid][0].grabber != grabberLast)
                    {
                        initResult = _epgGrabber.Initalise(_grabList[channelid][0].grabber, _config.Info.GrabDays);
                    }

                    grabberLast = _grabList[channelid][0].grabber;


                    // Get channel listing
                    if (initResult)
                    {
                        programs = _epgGrabber.GetGuide(channelid, _grabList[channelid][0].name, _grabList[channelid][0].linked,
                                                        _grabList[channelid][0].linkTime);
                    }

                    if (programs != null)
                    {
                        // write programs
                        foreach (grabInfo grab in _grabList[channelid])
                        {
                            _status.Status = string.Format("Writing channel {0}", grab.name);
                            if (ShowProgress != null)
                            {
                                ShowProgress(_status);
                            }

                            if (grab.merged)
                            {
                                Log.Info("WebEPG: Writing Merged Channel Part: {0}", grab.name);
                                Log.Info("        [{0}]", grab.linkTime);
                                if (_epgDataSink.StartChannelPrograms("[Merged]", grab.name))
                                {
                                    _epgDataSink.SetTimeWindow(grab.linkTime);
                                    for (int p = 0; p < programs.Count; p++)
                                    {
                                        if (grab.linkTime.IsInRange(programs[p].StartTime.ToLocalTime()))
                                        {
                                            //xmltv.WriteProgram(programs[p], grab.name, true);
                                            _epgDataSink.WriteProgram(programs[p], true);
                                            _status.Programs++;
                                        }
                                    }
                                    _epgDataSink.EndChannelPrograms("[Merged]", grab.name);
                                }
                            }
                            else
                            {
                                Log.Info("WebEPG: Writing Channel: {0}", grab.name);
                                if (_epgDataSink.StartChannelPrograms(channelid, grab.name))
                                {
                                    for (int p = 0; p < programs.Count; p++)
                                    {
                                        //xmltv.WriteProgram(programs[p], grab.name, false);
                                        _epgDataSink.WriteProgram(programs[p], false);
                                        _status.Programs++;
                                    }
                                    _epgDataSink.EndChannelPrograms(channelid, grab.name);
                                }
                            }

                            _status.Channels++;
                            if (ShowProgress != null)
                            {
                                ShowProgress(_status);
                            }
                        }
                    }
                    else
                    {
                        foreach (grabInfo grab in _grabList[channelid])
                        {
                            Log.Info("WebEPG: Grabber failed for: {0}", grab.name);
                        }
                    }
                }
            }

            _status.Status = "Finishing...";
            if (ShowProgress != null)
            {
                ShowProgress(_status);
            }
            //xmltv.Close();
            _epgDataSink.Close();

            // log Http statistics
            for (int h = 0; h < httpStats.Count; h++)
            {
                SiteStatistics site = httpStats.GetbyIndex(h);
                Log.Info("HTTP Statistics: {0}", site.ToString());
                httpStats.Clear(site.Site);
            }

            _status.EndTime = DateTime.Now;
            _status.Status  = "Finished grabbing.";
            if (ShowProgress != null)
            {
                ShowProgress(_status);
            }

            return(true);
        }
Beispiel #2
0
    /// <summary>
    /// Starts the importation process
    /// </summary>
    /// <returns>bool - sucess/fail</returns>
    public bool Import()
    {
      _status.Status = "Loading configuration...";
      _status.Channels = 0;
      _status.Programs = 0;
      _status.StartTime = DateTime.Now;
      _status.EndTime = new DateTime(1971, 11, 6);
      if (ShowProgress != null) ShowProgress(_status);

      if (!LoadConfig())
      {
        return false;
      }

      _epgDataSink.Open();

      // Collect HttpStatistic
      IHttpStatistics httpStats;
      if (GlobalServiceProvider.Instance.IsRegistered<IHttpStatistics>())
      {
        httpStats = GlobalServiceProvider.Instance.Get<IHttpStatistics>();
      }
      else
      {
        httpStats = new HttpStatistics();
        GlobalServiceProvider.Instance.Add<IHttpStatistics>(httpStats);
      }

      Log.Info("WebEPG: Loading Channel Config");
      _grabList = new Dictionary<string, List<grabInfo>>();
      // for each channel write info xmltv file.
      List<ChannelMap> allChannels = new List<ChannelMap>(_config.Channels);
      allChannels.AddRange(_config.RadioChannels);
      foreach (ChannelMap channel in allChannels)
      {
        if (channel.id == null && channel.merged == null)
        {
          Log.Info(" Ignoring Channel Name: {0} - No Channel id", channel.displayName);
          continue;
        }

        if (channel.merged == null || channel.merged.Count == 0)
        {
          if (channel.grabber != null)
          {
            Log.Debug(" Loading Channel {0} ID: {1}", channel.displayName, channel.id);
            //xmltv.WriteChannel(channel.id, channel.displayName);
            _epgDataSink.WriteChannel(channel.id, channel.displayName);

            grabInfo grab = new grabInfo();
            grab.name = channel.displayName;
            grab.id = channel.id;
            grab.grabber = channel.grabber;
            grab.merged = false;
            grab.linked = true;
            grab.linkTime = new TimeRange("00:00", "23:00");

            if (!_grabList.ContainsKey(channel.id))
            {
              List<grabInfo> grabs = new List<grabInfo>();
              grabs.Add(grab);
              _grabList.Add(channel.id, grabs);
            }
            else
            {
              _grabList[channel.id].Add(grab);
            }
          }
          else
          {
            Log.Info(" Ignoring Channel Name: {0} - No Grabber id", channel.displayName);
          }
        }
        else
        {
          Log.Debug(" Loading Merged Channel {0}", channel.displayName);
          //xmltv.WriteChannel("[Merged]", channel.displayName);
          _epgDataSink.WriteChannel("[Merged]", channel.displayName);

          foreach (MergedChannel merged in channel.merged)
          {
            if (merged.grabber != null)
            {
              grabInfo grab = new grabInfo();
              grab.name = channel.displayName;
              grab.id = merged.id;
              grab.grabber = merged.grabber;
              grab.merged = true;
              grab.linked = true;
              grab.linkTime = new TimeRange(merged.start, merged.end);
              Log.Debug("  Loading Merged Sub-channel: {0} Time range: {1}", merged.id,
                        grab.linkTime.ToString());

              if (!_grabList.ContainsKey(merged.id))
              {
                List<grabInfo> grabs = new List<grabInfo>();
                grabs.Add(grab);
                _grabList.Add(merged.id, grabs);
              }
              else
              {
                _grabList[merged.id].Add(grab);
              }
            }
            else
            {
              Log.Info("  Ignoring Merged Sub-channel: {0}/{1} - No Grabber id", channel.displayName,
                       merged.id);
            }
          }
        }
      }

      string grabberLast = "";
      List<ProgramData> programs = null;
      bool initResult = false;
      _epgGrabber = new WebListingGrabber(_baseDirectory + "\\WebEPG\\grabbers\\");

      // For each channel get listing
      int i = 1;
      foreach (string channelid in _grabList.Keys)
      {
        _status.Status = string.Format("Getting Channel ID: {0} [{1} of {2}]", channelid, i, _grabList.Count);
        if (ShowProgress != null) ShowProgress(_status);

        Log.Info("WebEPG: Getting Channel ID: {0}", channelid);
        Log.Info("        [{0} of {1}]", i++, _grabList.Count);

        if (_grabList[channelid].Count > 0)
        {
          if (_grabList[channelid][0].grabber != grabberLast)
          {
            initResult = _epgGrabber.Initalise(_grabList[channelid][0].grabber, _config.Info.GrabDays);
          }

          grabberLast = _grabList[channelid][0].grabber;


          // Get channel listing
          if (initResult)
          {
            programs = _epgGrabber.GetGuide(channelid, _grabList[channelid][0].name, _grabList[channelid][0].linked,
                                            _grabList[channelid][0].linkTime);
          }

          if (programs != null)
          {
            // write programs
            foreach (grabInfo grab in _grabList[channelid])
            {
              _status.Status = string.Format("Writing channel {0}", grab.name);
              if (ShowProgress != null) ShowProgress(_status);

              if (grab.merged)
              {
                Log.Info("WebEPG: Writing Merged Channel Part: {0}", grab.name);
                Log.Info("        [{0}]", grab.linkTime);
                if (_epgDataSink.StartChannelPrograms("[Merged]", grab.name))
                {
                  _epgDataSink.SetTimeWindow(grab.linkTime);
                  for (int p = 0; p < programs.Count; p++)
                  {
                    if (grab.linkTime.IsInRange(programs[p].StartTime.ToLocalTime()))
                    {
                      //xmltv.WriteProgram(programs[p], grab.name, true);
                      _epgDataSink.WriteProgram(programs[p], true);
                      _status.Programs++;
                    }
                  }
                  _epgDataSink.EndChannelPrograms("[Merged]", grab.name);
                }
              }
              else
              {
                Log.Info("WebEPG: Writing Channel: {0}", grab.name);
                if (_epgDataSink.StartChannelPrograms(channelid, grab.name))
                {
                  for (int p = 0; p < programs.Count; p++)
                  {
                    //xmltv.WriteProgram(programs[p], grab.name, false);
                    _epgDataSink.WriteProgram(programs[p], false);
                    _status.Programs++;
                  }
                  _epgDataSink.EndChannelPrograms(channelid, grab.name);
                }
              }

              _status.Channels++;
              if (ShowProgress != null) ShowProgress(_status);
            }
          }
          else
          {
            foreach (grabInfo grab in _grabList[channelid])
            {
              Log.Info("WebEPG: Grabber failed for: {0}", grab.name);
            }
          }
        }
      }

      _status.Status = "Finishing...";
      if (ShowProgress != null) ShowProgress(_status);
      //xmltv.Close();
      _epgDataSink.Close();

      // log Http statistics
      for (int h = 0; h < httpStats.Count; h++)
      {
        SiteStatistics site = httpStats.GetbyIndex(h);
        Log.Info("HTTP Statistics: {0}", site.ToString());
        httpStats.Clear(site.Site);
      }

      _status.EndTime = DateTime.Now;
      _status.Status = "Finished grabbing.";
      if (ShowProgress != null) ShowProgress(_status);

      return true;
    }