public void AddRelationCollection(RelationCollection rc)
 {
     this.relations.Add(rc);
     InfoEntryManager manager = InfoEntryManager.Instance;
     List<InfoEntry> infoEntryList = manager.GetVideos(rc.Showname);
     foreach (InfoEntry ie in infoEntryList) {
         ie.findEpisodeName(rc);
     }
 }
        //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, RelationProvider provider,BackgroundWorker worker, DoWorkEventArgs dwea)
        {
            if (provider == null) {
                Logger.Instance.LogMessage("GetRelations: No relation provider found/selected", LogLevel.ERROR);
                return;
            }
            if (String.IsNullOrEmpty(url))
            {
                Logger.Instance.LogMessage("GetRelations: URL is null or empty", LogLevel.ERROR);
                return;
            }
            if (String.IsNullOrEmpty(Showname))
            {
                Logger.Instance.LogMessage("GetRelations: Showname is null or empty", LogLevel.ERROR);
                return;
            }
            Logger.Instance.LogMessage("Trying to get relations from " + url, LogLevel.DEBUG);
            //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
            RelationCollection rc = new RelationCollection(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);
                Logger.Instance.LogMessage("Trying to get relations for season " + season + " from " + url, LogLevel.DEBUG);
                HttpWebRequest requestHtml = null;
                try {
                    requestHtml = (HttpWebRequest)(HttpWebRequest.Create(url));
                }

                catch (Exception ex) {
                    Logger.Instance.LogMessage(ex.Message, LogLevel.ERROR);
                    requestHtml.Abort();
                    return;
                }
                requestHtml.Timeout = Helper.ReadInt(Config.Timeout);
                // 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;
                    Logger.Instance.LogMessage(ex.Message, LogLevel.ERROR);
                    if (responseHtml != null) {
                        responseHtml.Close();
                    }
                    return;
                }
                Logger.Instance.LogMessage("Response URL: " + responseHtml.ResponseUri.AbsoluteUri, LogLevel.DEBUG);
                //if we get redirected, lets assume this page does not exist
                if (responseHtml.ResponseUri.AbsoluteUri != url) {
                    Logger.Instance.LogMessage("Response URL doesn't match request URL, page doesn't seem to exist", LogLevel.DEBUG);
                    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) {
                        Logger.Instance.LogMessage("Invalid encoding in config file: " + ex.Message, LogLevel.ERROR);
                        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){
                    Logger.Instance.LogMessage("Trying to match source from " + responseHtml.ResponseUri.AbsoluteUri + " with " + pattern, LogLevel.DEBUG);
                }
                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, "");
                    //RelationsRemove 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.AddRelation(new Relation(season, e, result));
                            Logger.Instance.LogMessage("Found Relation: " + "S" + s.ToString() + "E" + e.ToString() + " - " + result, LogLevel.DEBUG);
                        }
                        else
                        {
                            rc.AddRelation(new Relation(s, e, result));
                            Logger.Instance.LogMessage("Found Relation: " + "S" + s.ToString() + "E" + e.ToString() + " - " + result, LogLevel.DEBUG);
                        }
                    }
                }
                RelationManager.Instance.AddRelationCollection(rc);

                // THOU SHALL NOT FORGET THE BREAK
                if (!url2.Contains("%S"))
                    break;
                season++;
            }
            Logger.Instance.LogMessage("" + (season - 1) + " Seasons, " + rc.Count + " relations found", LogLevel.DEBUG);
        }
Example #3
0
        public void findEpisodeName(RelationCollection rc)
        {
            resetName();
            if (rc == null)
                return;

            for (int i = 0; i < rc.Count; i++) {
                if (isValidRelation(rc[i])) {
                    Name = rc[i].Name;
                    break;
                }
                if (isInValidSeason(rc[i]) || isInValidEpisode(rc[i]))
                    Name = rc[i].Name;
            }
        }