Exemple #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="WebParser"/> class.
        /// </summary>
        /// <param name="webTemplate">The web template.</param>
        public WebParser(WebParserTemplate webTemplate)
        {
            // Store the template
            _template = webTemplate;

            // Get default template -> currently only a default is supported
            // In the future template per channel ID can be added.
            HtmlParserTemplate listingTemplate = _template.GetTemplate("default");

            _listingPreference = _template.GetPreference("default");

            // Create dictionary for month strings
            Dictionary <string, int> monthsDict = null;

            if (_template.months != null)
            {
                // template contains months list -> load into dictionary
                monthsDict = new Dictionary <string, int>();
                for (int i = 0; i < _template.months.Length; i++)
                {
                    monthsDict.Add(_template.months[i], i + 1);
                    ;
                }
            }


            // create new Html Parser using default template
            _listingParser = new HtmlParser(listingTemplate, typeof(ProgramData), monthsDict);

            // setup sublink parser if template and config exists
            _sublinkParser = null;
            if (_template.sublinks != null && _template.sublinks.Count > 0)
            {
                // Load first sublink template -> only one supported now
                // future support for multiple sublinks possible
                SublinkInfo        sublink         = _template.sublinks[0];
                HtmlParserTemplate sublinkTemplate = _template.GetTemplate(sublink.template);
                _sublinkPreference = _template.GetPreference(sublinkTemplate.Name);

                if (sublinkTemplate != null)
                {
                    // create sublink parser using template
                    _sublinkParser  = new HtmlParser(sublinkTemplate, typeof(ProgramData));
                    _sublinkMatch   = sublink.search;
                    _sublinkRequest = sublink.url;
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Initalises the ListingGrabber class with a grabber config file
        /// </summary>
        /// <param name="File">The grabber config file file.</param>
        /// <returns>bool - success/fail loading the config file</returns>
        public bool Initalise(string File, int maxGrabDays)
        {
            _maxGrabDays = maxGrabDays;

            // Load configuration file
            Log.Info("WebEPG: Opening {0}", File);

            try
            {
                //_grabber = new GrabberConfig(_strBaseDir + File);

                XmlSerializer s = new XmlSerializer(typeof(GrabberConfigFile));

                TextReader r = new StreamReader(_strBaseDir + File);
                _grabber = (GrabberConfigFile)s.Deserialize(r);
            }
            catch (InvalidOperationException ex)
            {
                Log.Error("WebEPG: Config Error {0}: {1}", File, ex.Message);
                return(false);
            }

            if (_grabber.Info.Version == null || _grabber.Info.Version == string.Empty)
            {
                Log.Info("WebEPG: Unknown Version");
            }
            else
            {
                Log.Info("WebEPG: Version: {0}", _grabber.Info.Version);
            }

            if (_grabber.Listing.SearchParameters == null)
            {
                _grabber.Listing.SearchParameters = new RequestData();
            }

            _reqData = _grabber.Listing.SearchParameters;

            // Setup timezone
            Log.Info("WebEPG: TimeZone, Local: {0}", TimeZone.CurrentTimeZone.StandardName);

            _siteTimeZone = null;
            if (_grabber.Info.TimeZone != null && _grabber.Info.TimeZone != string.Empty)
            {
                try
                {
                    Log.Info("WebEPG: TimeZone, Site : {0}", _grabber.Info.TimeZone);
                    _siteTimeZone = new WorldTimeZone(_grabber.Info.TimeZone);
                }
                catch (ArgumentException)
                {
                    Log.Error("WebEPG: TimeZone Not valid");
                    _siteTimeZone = null;
                }
            }

            if (_siteTimeZone == null)
            {
                Log.Info("WebEPG: No site TimeZone, using Local: {0}", TimeZone.CurrentTimeZone.StandardName);
                _siteTimeZone = new WorldTimeZone(TimeZone.CurrentTimeZone.StandardName);
            }

            switch (_grabber.Listing.listingType)
            {
            case ListingInfo.Type.Xml:
                _parser = new XmlParser(_grabber.Listing.XmlTemplate);
                break;

            case ListingInfo.Type.Data:

                if (_grabber.Listing.DataTemplate.Template == null)
                {
                    Log.Error("WebEPG: {0}: No Template", File);
                    return(false);
                }
                _parser = new DataParser(_grabber.Listing.DataTemplate);
                break;

            case ListingInfo.Type.Html:
                HtmlParserTemplate defaultTemplate = _grabber.Listing.HtmlTemplate.GetTemplate("default");
                if (defaultTemplate == null ||
                    defaultTemplate.SectionTemplate == null ||
                    defaultTemplate.SectionTemplate.Template == null)
                {
                    Log.Error("WebEPG: {0}: No Template", File);
                    return(false);
                }
                _parser = new WebParser(_grabber.Listing.HtmlTemplate);
                if (_grabber.Info.GrabDays < _maxGrabDays)
                {
                    Log.Info("WebEPG: Grab days ({0}) more than Guide days ({1}), limiting grab to {1} days",
                             _maxGrabDays, _grabber.Info.GrabDays);
                    _maxGrabDays = _grabber.Info.GrabDays;
                }

                break;
            }

            return(true);
        }