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; }
/// <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; } }