Exemplo n.º 1
0
        //parse page(s) containing relations
        /// <summary>
        /// Parses page containing the relation data
        /// </summary>
        /// <param name="url">URL of the page to parse</param>
        /// <param name="Showname">Showname</param>
        public static void GetRelations(string url, string Showname, TvShowInfoProvider provider, BackgroundWorker worker, DoWorkEventArgs dwea)
        {
            if (provider == null) {
                log.Error("GetRelations: No relation provider found/selected");
                return;
            }
            if (String.IsNullOrEmpty(url)) {
                log.Error("GetRelations: URL is null or empty");
                return;
            }
            if (String.IsNullOrEmpty(Showname)) {
                log.Error("GetRelations: Showname is null or empty");
                return;
            }
            log.Debug("Trying to get relations from " + url);
            //if episode infos are stored on a new page for each season, this should be marked with %S in url, so we can iterate through all those pages
            int season = 1;
            string url2 = url;
            //Create new RelationCollection
            TvShow rc = new TvShow(Showname);
            while (true) {
                if (url2.Contains("%S")) {
                    url = url2.Replace("%S", season.ToString());
                }

                if (url == null || url == "")
                    return;
                // request
                url = System.Web.HttpUtility.UrlPathEncode(url);
                log.Debug("Trying to get relations for season " + season + " from " + url);
                HttpWebRequest requestHtml = null;
                try {
                    requestHtml = (HttpWebRequest)(HttpWebRequest.Create(url));
                } catch (Exception ex) {
                    log.Error(ex.Message);
                    requestHtml.Abort();
                    return;
                }
                requestHtml.Timeout = Helper.ReadInt(AppProperties.CONNECTION_TIMEOUT_KEY);
                // get response
                HttpWebResponse responseHtml = null;
                try {
                    responseHtml = (HttpWebResponse)(requestHtml.GetResponse());
                } catch (WebException ex) {
                    //Serienjunkies returns "(300) Mehrdeutige Umleitung" when an inexistant season is requested
                    if (ex.Message.Contains("(300)"))
                        break;
                    log.Error(ex.Message);
                    if (responseHtml != null) {
                        responseHtml.Close();
                    }
                    return;
                }
                log.Debug("Response URL: " + responseHtml.ResponseUri.AbsoluteUri);
                //if we get redirected, lets assume this page does not exist
                if (responseHtml.ResponseUri.AbsoluteUri != url) {
                    log.Debug("Response URL doesn't match request URL, page doesn't seem to exist");
                    responseHtml.Close();
                    requestHtml.Abort();
                    break;
                }
                // and download
                //Logger.Instance.LogMessage("charset=" + responseHtml.CharacterSet, LogLevel.INFO);
                Encoding enc;
                if (provider.Encoding != null && provider.Encoding != "") {
                    try {
                        enc = Encoding.GetEncoding(provider.Encoding);
                    } catch (Exception ex) {
                        log.Error("Invalid encoding in config file: " + ex.Message);
                        enc = Encoding.GetEncoding(responseHtml.CharacterSet);
                    }
                } else {
                    enc = Encoding.GetEncoding(responseHtml.CharacterSet);
                }
                StreamReader r = new StreamReader(responseHtml.GetResponseStream(), enc);
                string source = r.ReadToEnd();
                r.Close();
                responseHtml.Close();

                //Source cropping
                source = source.Substring(Math.Max(source.IndexOf(provider.RelationsStart), 0));
                source = source.Substring(0, Math.Max(source.LastIndexOf(provider.RelationsEnd), 0));

                string pattern = provider.RelationsRegExp;

                //for some reason, responseHtml.ResponseUri is null for some providers when running on mono
                if (!Settings.Instance.isMonoCompatibilityMode) {
                    log.Debug("Trying to match source from " + responseHtml.ResponseUri.AbsoluteUri + " with " + pattern);
                }
                RegexOptions ro = RegexOptions.IgnoreCase | RegexOptions.Singleline;
                if (provider.RelationsRightToLeft)
                    ro |= RegexOptions.RightToLeft;
                MatchCollection mc = Regex.Matches(source, pattern, ro);

                string CleanupRegex = provider.RelationsRemove;
                for (int i = 0; i < mc.Count; i++) {
                    Match m = mc[i];
                    string result = Regex.Replace(System.Web.HttpUtility.HtmlDecode(m.Groups["Title"].Value), CleanupRegex, "");
                    //RELATIONS_REMOVE_KEY may be used to filter out results completely, for example if they are a html tag
                    if (result != "") {
                        //parse season and episode numbers
                        int s, e;

                        Int32.TryParse(m.Groups["Season"].Value, out s);
                        Int32.TryParse(m.Groups["Episode"].Value, out e);
                        //if we are iterating through season pages, take season from page url directly
                        if (url != url2) {
                            rc.AddEpisode(new ShowEpisode(season, e, result));
                            log.Debug("Found Relation: " + "S" + s.ToString() + "E" + e.ToString() + " - " + result);
                        } else {
                            rc.AddEpisode(new ShowEpisode(s, e, result));
                            log.Debug("Found Relation: " + "S" + s.ToString() + "E" + e.ToString() + " - " + result);
                        }
                    }
                }
                TvShowManager.Instance.addTvShow(rc);

                // THOU SHALL NOT FORGET THE BREAK
                if (!url2.Contains("%S"))
                    break;
                season++;
            }
            log.Debug("" + (season - 1) + " Seasons, " + rc.CountEpisodes + " relations found");
        }
Exemplo n.º 2
0
        //parse page(s) containing relations
        /// <summary>
        /// Parses page containing the relation data
        /// </summary>
        /// <param name="url">URL of the page to parse</param>
        /// <param name="Showname">Showname</param>
        public static void GetRelations(string url, string Showname, TvShowInfoProvider provider, BackgroundWorker worker, DoWorkEventArgs dwea)
        {
            if (provider == null)
            {
                log.Error("GetRelations: No relation provider found/selected");
                return;
            }
            if (String.IsNullOrEmpty(url))
            {
                log.Error("GetRelations: URL is null or empty");
                return;
            }
            if (String.IsNullOrEmpty(Showname))
            {
                log.Error("GetRelations: Showname is null or empty");
                return;
            }
            log.Debug("Trying to get relations from " + url);
            //if episode infos are stored on a new page for each season, this should be marked with %S in url, so we can iterate through all those pages
            int    season = 1;
            string url2   = url;
            //Create new RelationCollection
            TvShow rc = new TvShow(Showname);

            while (true)
            {
                if (url2.Contains("%S"))
                {
                    url = url2.Replace("%S", season.ToString());
                }

                if (url == null || url == "")
                {
                    return;
                }
                // request
                url = System.Web.HttpUtility.UrlPathEncode(url);
                log.Debug("Trying to get relations for season " + season + " from " + url);
                HttpWebRequest requestHtml = null;
                try {
                    requestHtml = (HttpWebRequest)(HttpWebRequest.Create(url));
                } catch (Exception ex) {
                    log.Error(ex.Message);
                    requestHtml.Abort();
                    return;
                }
                requestHtml.Timeout = Helper.ReadInt(AppProperties.CONNECTION_TIMEOUT_KEY);
                // get response
                HttpWebResponse responseHtml = null;
                try {
                    responseHtml = (HttpWebResponse)(requestHtml.GetResponse());
                } catch (WebException ex) {
                    //Serienjunkies returns "(300) Mehrdeutige Umleitung" when an inexistant season is requested
                    if (ex.Message.Contains("(300)"))
                    {
                        break;
                    }
                    log.Error(ex.Message);
                    if (responseHtml != null)
                    {
                        responseHtml.Close();
                    }
                    return;
                }
                log.Debug("Response URL: " + responseHtml.ResponseUri.AbsoluteUri);
                //if we get redirected, lets assume this page does not exist
                if (responseHtml.ResponseUri.AbsoluteUri != url)
                {
                    log.Debug("Response URL doesn't match request URL, page doesn't seem to exist");
                    responseHtml.Close();
                    requestHtml.Abort();
                    break;
                }
                // and download
                //Logger.Instance.LogMessage("charset=" + responseHtml.CharacterSet, LogLevel.INFO);
                Encoding enc;
                if (provider.Encoding != null && provider.Encoding != "")
                {
                    try {
                        enc = Encoding.GetEncoding(provider.Encoding);
                    } catch (Exception ex) {
                        log.Error("Invalid encoding in config file: " + ex.Message);
                        enc = Encoding.GetEncoding(responseHtml.CharacterSet);
                    }
                }
                else
                {
                    enc = Encoding.GetEncoding(responseHtml.CharacterSet);
                }
                StreamReader r      = new StreamReader(responseHtml.GetResponseStream(), enc);
                string       source = r.ReadToEnd();
                r.Close();
                responseHtml.Close();


                //Source cropping
                source = source.Substring(Math.Max(source.IndexOf(provider.RelationsStart), 0));
                source = source.Substring(0, Math.Max(source.LastIndexOf(provider.RelationsEnd), 0));

                string pattern = provider.RelationsRegExp;

                //for some reason, responseHtml.ResponseUri is null for some providers when running on mono
                if (!Settings.Instance.isMonoCompatibilityMode)
                {
                    log.Debug("Trying to match source from " + responseHtml.ResponseUri.AbsoluteUri + " with " + pattern);
                }
                RegexOptions ro = RegexOptions.IgnoreCase | RegexOptions.Singleline;
                if (provider.RelationsRightToLeft)
                {
                    ro |= RegexOptions.RightToLeft;
                }
                MatchCollection mc = Regex.Matches(source, pattern, ro);

                string CleanupRegex = provider.RelationsRemove;
                for (int i = 0; i < mc.Count; i++)
                {
                    Match  m      = mc[i];
                    string result = Regex.Replace(System.Web.HttpUtility.HtmlDecode(m.Groups["Title"].Value), CleanupRegex, "");
                    //RELATIONS_REMOVE_KEY may be used to filter out results completely, for example if they are a html tag
                    if (result != "")
                    {
                        //parse season and episode numbers
                        int s, e;

                        Int32.TryParse(m.Groups["Season"].Value, out s);
                        Int32.TryParse(m.Groups["Episode"].Value, out e);
                        //if we are iterating through season pages, take season from page url directly
                        if (url != url2)
                        {
                            rc.AddEpisode(new ShowEpisode(season, e, result));
                            log.Debug("Found Relation: " + "S" + s.ToString() + "E" + e.ToString() + " - " + result);
                        }
                        else
                        {
                            rc.AddEpisode(new ShowEpisode(s, e, result));
                            log.Debug("Found Relation: " + "S" + s.ToString() + "E" + e.ToString() + " - " + result);
                        }
                    }
                }
                TvShowManager.Instance.addTvShow(rc);

                // THOU SHALL NOT FORGET THE BREAK
                if (!url2.Contains("%S"))
                {
                    break;
                }
                season++;
            }
            log.Debug("" + (season - 1) + " Seasons, " + rc.CountEpisodes + " relations found");
        }