/// <summary> /// Parses search results from a series search /// </summary> /// <param name="source">Source code of the search results page</param> /// <param name="showname">Showname</param> /// <param name="sourceUrl">URL of the page source</param> public ParsedSearch parseSearch(ref string source, string sourceUrl, string showname, string searchString) { ParsedSearch ps = new ParsedSearch(); ps.Showname = showname; ps.SearchString = searchString; ps.provider = theProvider; if (String.IsNullOrEmpty(source)) { return(ps); } if (theProvider == null) { log.Error("No relation provider found/selected"); return(ps); } log.Debug("Trying to match source at " + sourceUrl + " with " + getSearchRegExp()); RegexOptions ro = RegexOptions.IgnoreCase | RegexOptions.Singleline; if (getSearchRightToLeft()) { ro |= RegexOptions.RightToLeft; } MatchCollection mc = Regex.Matches(source, getSearchRegExp(), ro); if (mc.Count == 0) { log.Info("No results found"); return(ps); } else { log.Info("Search engine found multiple results at " + sourceUrl.Replace(" ", "%20")); ps.Results = new Hashtable(); foreach (Match m in mc) { string url = getSeriesUrl(); url = url.Replace(RenamingConstants.SHOW_LINK_MARKER, m.Groups["link"].Value); url = System.Web.HttpUtility.HtmlDecode(url); string name = System.Web.HttpUtility.HtmlDecode(m.Groups["name"].Value + " " + m.Groups["year"].Value); //temporary fix, this should be externalized in the titleProvider configs if (name.ToLower().Contains("poster")) { continue; } string CleanedName = cleanSearchResultName(name, getSearchRemove()); try { ps.Results.Add(CleanedName, url); } catch (Exception) { log.Error("Can't add " + CleanedName + " to search results because an entry of same name already exists"); } } return(ps); } }
public ParsedSearch ParseKey(string key) { AdType typeParsed = AdType.Flat; int idParsed; if (String.IsNullOrEmpty(key) || key.Count() < 2) { idParsed = 0; } else { var id = key.Substring(0, key.Count() - 2); Int32.TryParse(id, out idParsed); if (idParsed % 9999 != 0) { idParsed = 0; } else { idParsed = idParsed / 9999; } var type = key.Substring(key.Count() - 2, 2); if (type == "12") { typeParsed = AdType.Flat; } else if (type == "14") { typeParsed = AdType.House; } else if (type == "18") { typeParsed = AdType.Land; } else { idParsed = 0; } } var parsedSearch = new ParsedSearch() { AdType = typeParsed, Id = idParsed }; return(parsedSearch); }
public ParsedSearch search(string showname, string searchString) { ParsedSearch ps = new ParsedSearch(); ps.provider = theProvider; ps.SearchString = searchString; ps.Showname = showname; // request if (theProvider == null) { log.Error("No relation provider found/selected"); return(ps); } string url = getSearchUrl(); log.Debug("Search URL: " + url); if (string.IsNullOrEmpty(url)) { log.Error("Can't search because no search URL is specified for this provider"); return(ps); } url = url.Replace(RenamingConstants.SHOWNAME_MARKER, searchString); url = System.Web.HttpUtility.UrlPathEncode(url); log.Debug("Encoded Search URL: " + url); WebRequest requestHtml = null; try { requestHtml = WebRequest.Create(url); } catch (Exception ex) { log.Error(ex.Message); requestHtml?.Abort(); return(ps); } //SetProxy(requestHtml, url); log.Info("Searching at " + url.Replace(" ", "%20")); requestHtml.Timeout = Convert.ToInt32( Settings.Instance.getAppConfiguration().getSingleNumberProperty(AppProperties.CONNECTION_TIMEOUT_KEY)); // get response WebResponse responseHtml = null; try { responseHtml = requestHtml.GetResponse(); } catch (Exception ex) { log.Error(ex.Message); responseHtml?.Close(); requestHtml.Abort(); return(ps); } log.Debug("Search Results URL: " + responseHtml.ResponseUri.AbsoluteUri); //if search engine directs us straight to the result page, skip parsing search results string seriesURL = getSeriesUrl(); if (responseHtml.ResponseUri.AbsoluteUri.Contains(seriesURL)) { log.Debug("Search Results URL contains Series URL: " + seriesURL); ps.Results = new Hashtable(); string cleanedName = cleanSearchResultName(showname, getSearchRemove()); if (getSearchResultsBlacklist() == "" || !Regex.Match(cleanedName, getSearchResultsBlacklist()).Success) { ps.Results.Add(cleanedName, responseHtml.ResponseUri.AbsoluteUri + getEpisodesUrl()); log.Info("Search engine forwarded directly to single result: " + responseHtml.ResponseUri.AbsoluteUri.Replace(" ", "%20") + getEpisodesUrl().Replace(" ", "%20")); } return(ps); } log.Debug("Search Results URL doesn't contain Series URL: " + seriesURL + ", this is a proper search results page"); // and download StreamReader r = null; try { r = new StreamReader(responseHtml.GetResponseStream()); } catch (Exception ex) { r?.Close(); responseHtml.Close(); requestHtml.Abort(); log.Error(ex.Message); return(ps); } string source = r.ReadToEnd(); r.Close(); //Source cropping source = source.Substring(Math.Max(source.IndexOf(getSearchStart(), StringComparison.Ordinal), 0)); source = source.Substring(0, Math.Max(source.LastIndexOf(getSearchEnd(), StringComparison.Ordinal), source.Length - 1)); ps = parseSearch(ref source, responseHtml.ResponseUri.AbsoluteUri, showname, searchString); responseHtml.Close(); return(ps); }
public static ParsedSearch Search(RelationProvider provider, string SearchString, string Showname) { ParsedSearch ps = new ParsedSearch(); ps.provider = provider; ps.SearchString = SearchString; ps.Showname = Showname; //once for (int a = 0; a < 1; a++) { // request if (provider == null) { Logger.Instance.LogMessage("No relation provider found/selected", LogLevel.ERROR); return ps; } /* //get rid of old relations RelationManager.Instance.RemoveRelationCollection(Showname); foreach (InfoEntry ie in InfoEntryManager.Instance) { if (ie.Showname == Showname && ie.ProcessingRequested) { ie.Name = ""; ie.NewFileName = ""; ie.Language = provider.Language; } }*/ string url = provider.SearchUrl; Logger.Instance.LogMessage("Search URL: " + url, LogLevel.DEBUG); if (url == null || url == "") { Logger.Instance.LogMessage("Can't search because no search URL is specified for this provider", LogLevel.ERROR); break; } url = url.Replace("%T", SearchString); url = System.Web.HttpUtility.UrlPathEncode(url); Logger.Instance.LogMessage("Encoded Search URL: " + url, LogLevel.DEBUG); HttpWebRequest requestHtml = null; try { requestHtml = (HttpWebRequest)(HttpWebRequest.Create(url)); } catch (Exception ex) { Logger.Instance.LogMessage(ex.Message, LogLevel.ERROR); if (requestHtml != null) requestHtml.Abort(); break; } //SetProxy(requestHtml, url); Logger.Instance.LogMessage("Searching at " + url.Replace(" ", "%20"), LogLevel.INFO); requestHtml.Timeout = Convert.ToInt32(Helper.ReadProperty(Config.Timeout)); // get response HttpWebResponse responseHtml = null; try { responseHtml = (HttpWebResponse)(requestHtml.GetResponse()); } catch (Exception ex) { Logger.Instance.LogMessage(ex.Message, LogLevel.ERROR); if (responseHtml != null) responseHtml.Close(); if (requestHtml != null) requestHtml.Abort(); break; } Logger.Instance.LogMessage("Search Results URL: " + responseHtml.ResponseUri.AbsoluteUri, LogLevel.DEBUG); //if search engine directs us straight to the result page, skip parsing search results string seriesURL = provider.SeriesUrl; if (responseHtml.ResponseUri.AbsoluteUri.Contains(seriesURL)) { Logger.Instance.LogMessage("Search Results URL contains Series URL: " + seriesURL, LogLevel.DEBUG); ps.Results = new Hashtable(); string CleanedName=CleanSearchResultName(Showname,provider); if(provider.SearchResultsBlacklist=="" || !Regex.Match(CleanedName,provider.SearchResultsBlacklist).Success){ ps.Results.Add(CleanedName, responseHtml.ResponseUri.AbsoluteUri + provider.EpisodesUrl); Logger.Instance.LogMessage("Search engine forwarded directly to single result: " + responseHtml.ResponseUri.AbsoluteUri.Replace(" ", "%20") + provider.EpisodesUrl.Replace(" ", "%20"), LogLevel.INFO); } return ps; } else { Logger.Instance.LogMessage("Search Results URL doesn't contain Series URL: " + seriesURL + ", this is a proper search results page", LogLevel.DEBUG); // and download StreamReader r = null; try { r = new StreamReader(responseHtml.GetResponseStream()); } catch (Exception ex) { if (r != null) r.Close(); if (responseHtml != null) { responseHtml.Close(); } if (requestHtml != null) { requestHtml.Abort(); } Logger.Instance.LogMessage(ex.Message, LogLevel.ERROR); break; } string source = r.ReadToEnd(); r.Close(); //Source cropping source = source.Substring(Math.Max(source.IndexOf(provider.SearchStart), 0)); source = source.Substring(0, Math.Max(source.LastIndexOf(provider.SearchEnd), source.Length - 1)); ps = ParseSearch(ref source, responseHtml.ResponseUri.AbsoluteUri, Showname, SearchString, provider); } responseHtml.Close(); } return ps; }
/// <summary> /// Parses search results from a series search /// </summary> /// <param name="source">Source code of the search results page</param> /// <param name="Showname">Showname</param> /// <param name="SourceURL">URL of the page source</param> public static ParsedSearch ParseSearch(ref string source, string SourceURL, string Showname, string SearchString, RelationProvider provider) { ParsedSearch ps = new ParsedSearch(); ps.Showname = Showname; ps.SearchString = SearchString; ps.provider = provider; if (String.IsNullOrEmpty(source)){ return ps; } if (provider == null) { Logger.Instance.LogMessage("No relation provider found/selected", LogLevel.ERROR); return ps; } Logger.Instance.LogMessage("Trying to match source at " + SourceURL + " with " + provider.SearchRegExp, LogLevel.DEBUG); RegexOptions ro = RegexOptions.IgnoreCase | RegexOptions.Singleline; if (provider.SearchRightToLeft){ ro |= RegexOptions.RightToLeft; } MatchCollection mc = Regex.Matches(source, provider.SearchRegExp, ro); if (mc.Count == 0) { Logger.Instance.LogMessage("No results found", LogLevel.INFO); return ps; } /*else if (mc.Count == 1) { string url = provider.RelationsPage; Logger.Instance.LogMessage("One result found on search page, going to " + url.Replace(" ", "%20") + " with %L=" + mc[0].Groups["link"].Value, LogLevel.DEBUG); url = url.Replace("%L", mc[0].Groups["link"].Value); url = System.Web.HttpUtility.HtmlDecode(url); ps.Results = new Hashtable(); string CleanedName=CleanSearchResultName(Showname,provider); if(!Regex.Match(CleanedName, provider.SearchResultsBlacklist).Success){ ps.Results.Add(CleanedName, url); Logger.Instance.LogMessage("Search engine found one result: " + url.Replace(" ", "%20"), LogLevel.INFO); } return ps; //GetRelations(url, Showname); }*/ else { Logger.Instance.LogMessage("Search engine found multiple results at " + SourceURL.Replace(" ", "%20"), LogLevel.INFO); ps.Results = new Hashtable(); foreach (Match m in mc) { string url = provider.RelationsPage; url = url.Replace("%L", m.Groups["link"].Value); url = System.Web.HttpUtility.HtmlDecode(url); string name=System.Web.HttpUtility.HtmlDecode(m.Groups["name"].Value + " " + m.Groups["year"].Value); //temporary fix, this should be externalized in the provider configs if(name.ToLower().Contains("poster")) continue; string CleanedName = CleanSearchResultName(name, provider); try { ps.Results.Add(CleanedName, url); } catch (Exception) { Logger.Instance.LogMessage("Can't add " + CleanedName + " to search results because an entry of same name already exists", LogLevel.ERROR); } } return ps; /* SelectResult sr = new SelectResult(mc, provider, false); if (sr.ShowDialog() == DialogResult.Cancel || sr.url == "") return; //Apply language of selected result to matching episodes if (provider.Language == Helper.Languages.None) { foreach (InfoEntry ie in InfoEntryManager.Instance) { if (ie.Showname == Showname && ie.ProcessingRequested) { ie.Language = sr.Language; } } } string url = provider.RelationsPage; Logger.Instance.LogMessage("User selected " + provider.RelationsPage + "with %L=" + sr.url, LogLevel.DEBUG); url = url.Replace("%L", sr.url); url = System.Web.HttpUtility.HtmlDecode(url); GetRelations(url, Showname);*/ } }