/// <summary> /// Asynchronously downloads the specified link. /// </summary> /// <param name="link"> /// The object containing the link. /// This can be an URL in a string or a <c>Link</c>/<c>Subtitle</c> object. /// </param> /// <param name="target">The target location.</param> /// <param name="token">The user token.</param> public void Download(object link, string target, string token = null) { var id = Utils.Rand.Next(short.MaxValue); var st = DateTime.Now; _wc = new Utils.SmarterWebClient(); Uri uri; if (link is string) { uri = new Uri(link as string); } else if (link is Link) { uri = new Uri((link as Link).FileURL); if (!string.IsNullOrWhiteSpace((link as Link).Source.Cookies)) { _wc.Headers[HttpRequestHeader.Cookie] = (link as Link).Source.Cookies; } } else if (link is Subtitle) { uri = new Uri((link as Subtitle).FileURL); if (!string.IsNullOrWhiteSpace((link as Subtitle).Source.Cookies)) { _wc.Headers[HttpRequestHeader.Cookie] = (link as Subtitle).Source.Cookies; } } else { throw new Exception("The link object is an unsupported type."); } var domain = uri.Host.Replace("www.", string.Empty); Log.Debug("HTTP#{0} GET {1}", new[] { id.ToString(), uri.ToString() }); _wc.Headers[HttpRequestHeader.Referer] = "http://" + uri.DnsSafeHost + "/"; _wc.DownloadProgressChanged += (s, e) => DownloadProgressChanged.Fire(this, e.ProgressPercentage); _wc.DownloadFileCompleted += (s, e) => { Log.Debug("HTTP#" + id + " [" + domain + "] is " + Utils.GetFileSize(new FileInfo(target).Length) + " and took " + (DateTime.Now - st).TotalSeconds + "s."); if (Log.IsTraceEnabled) { Log.Trace("HTTP#" + id + " [" + domain + "] is " + (s as Utils.SmarterWebClient).ContentType + ", saved to " + target + " with token " + token); } DownloadFileCompleted.Fire(this, target, (s as Utils.SmarterWebClient).FileName, token ?? string.Empty); }; var proxy = default(string); var proxyId = default(object); if (Settings.Get <Dictionary <string, object> >("Proxied Domains").TryGetValue(domain, out proxyId)) { proxy = (string)Settings.Get <Dictionary <string, object> >("Proxies")[(string)proxyId]; } if (proxy != null) { var proxyUri = new Uri(proxy.Replace("$domain.", string.Empty)); switch (proxyUri.Scheme.ToLower()) { case "http": if (proxy.Contains("$url")) { uri = new Uri(proxy.Replace("$url", Utils.EncodeURL(uri.ToString()))); } else if (proxy.Contains("$domain") && proxy.Contains("$path")) { uri = new Uri(proxy.Replace("$domain", uri.DnsSafeHost).Replace("$path", uri.AbsolutePath)); } else { _wc.Proxy = new WebProxy(proxyUri.Host + ":" + proxyUri.Port); } break; case "socks4": case "socks4a": case "socks5": var tunnel = new HttpToSocks { RemoteProxy = HttpToSocks.Proxy.ParseUri(proxyUri) }; tunnel.Listen(); _wc.Proxy = (WebProxy)tunnel.LocalProxy; break; } Log.Debug("HTTP#" + id + " [" + domain + "] is proxied through " + proxyId + " (" + proxyUri + ")"); } _wc.DownloadFileAsync(uri, target); }
/// <summary> /// Searches for videos on Netflix. /// </summary> /// <param name="ep">The episode.</param> /// <returns> /// URL of the video. /// </returns> public override string Search(Episode ep) { var rest = new RestClient { QueryHandling = QueryHandling.AppendToParameters, DecompressionMethods = DecompressionMethods.GZip, UserAgent = Signature.Software + "/" + Signature.Version, FollowRedirects = true, Authority = "http://api.netflix.com/", Credentials = new OAuthCredentials { Type = OAuthType.ProtectedResource, SignatureMethod = OAuthSignatureMethod.HmacSha1, ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader, ConsumerKey = ConsumerKey, ConsumerSecret = SharedSecret } }; #region Detect and set proxy string proxy = null; object proxyId; if (Settings.Get<Dictionary<string, object>>("Proxied Domains").TryGetValue("netflix.com", out proxyId) || Settings.Get<Dictionary<string, object>>("Proxied Domains").TryGetValue("api.netflix.com", out proxyId)) { proxy = (string)Settings.Get<Dictionary<string, object>>("Proxies")[(string)proxyId]; } if (proxy != null) { var proxyUri = new Uri(proxy.Replace("$domain.", string.Empty)); switch (proxyUri.Scheme.ToLower()) { case "http": if (proxy.Contains("$url") || (proxy.Contains("$domain") && proxy.Contains("$path"))) { throw new Exception("Web-based proxies are not supported with Netflix for now, because of OAuth signatures."); } else { rest.Proxy = proxyUri.Host + ":" + proxyUri.Port; } break; case "socks4": case "socks4a": case "socks5": var tunnel = new HttpToSocks { RemoteProxy = HttpToSocks.Proxy.ParseUri(proxyUri) }; tunnel.Listen(); rest.Proxy = tunnel.LocalProxy.Host + ":" + tunnel.LocalProxy.Port; break; } } #endregion var request = new RestRequest { Path = "catalog/titles" }; request.AddParameter("term", ep.Show.Title); request.AddParameter("max_results", "1"); request.AddParameter("expand", "seasons"); var resp = XDocument.Parse(rest.Request(request).Content); var links = resp.XPathSelectElements("//link[@rel='http://schemas.netflix.com/catalog/title.season']"); string seasonid = null; foreach (var link in links) { if (link.Attribute("title").Value.EndsWith(" " + ep.Season)) { seasonid = link.Attribute("href").Value; break; } } if (seasonid != null) { request = new RestRequest { Path = seasonid.Replace("http://api.netflix.com/", string.Empty) + "/episodes" }; resp = XDocument.Parse(rest.Request(request).Content); var ids = resp.XPathSelectElements("//id").ToList(); if (ids.Count >= ep.Number) { return ids[ep.Number - 1].Value.Replace("http://api.netflix.com/catalog/titles/programs/", "http://movies.netflix.com/WiPlayer?movieid="); } } throw new OnlineVideoNotFoundException("No matching videos were found.", "Open Netflix search page", "http://movies.netflix.com/WiSearch?v1=" + Utils.EncodeURL(ep.Show.Title)); }
/// <summary> /// Searches for videos on Netflix. /// </summary> /// <param name="ep">The episode.</param> /// <returns> /// URL of the video. /// </returns> public override string Search(Episode ep) { var rest = new RestClient { QueryHandling = QueryHandling.AppendToParameters, DecompressionMethods = DecompressionMethods.GZip, UserAgent = Signature.Software + "/" + Signature.Version, FollowRedirects = true, Authority = "http://api.netflix.com/", Credentials = new OAuthCredentials { Type = OAuthType.ProtectedResource, SignatureMethod = OAuthSignatureMethod.HmacSha1, ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader, ConsumerKey = ConsumerKey, ConsumerSecret = SharedSecret } }; #region Detect and set proxy string proxy = null; object proxyId; if (Settings.Get <Dictionary <string, object> >("Proxied Domains").TryGetValue("netflix.com", out proxyId) || Settings.Get <Dictionary <string, object> >("Proxied Domains").TryGetValue("api.netflix.com", out proxyId)) { proxy = (string)Settings.Get <Dictionary <string, object> >("Proxies")[(string)proxyId]; } if (proxy != null) { var proxyUri = new Uri(proxy.Replace("$domain.", string.Empty)); switch (proxyUri.Scheme.ToLower()) { case "http": if (proxy.Contains("$url") || (proxy.Contains("$domain") && proxy.Contains("$path"))) { throw new Exception("Web-based proxies are not supported with Netflix for now, because of OAuth signatures."); } else { rest.Proxy = proxyUri.Host + ":" + proxyUri.Port; } break; case "socks4": case "socks4a": case "socks5": var tunnel = new HttpToSocks { RemoteProxy = HttpToSocks.Proxy.ParseUri(proxyUri) }; tunnel.Listen(); rest.Proxy = tunnel.LocalProxy.Host + ":" + tunnel.LocalProxy.Port; break; } } #endregion var request = new RestRequest { Path = "catalog/titles" }; request.AddParameter("term", ep.Show.Title); request.AddParameter("max_results", "1"); request.AddParameter("expand", "seasons"); var resp = XDocument.Parse(rest.Request(request).Content); var links = resp.XPathSelectElements("//link[@rel='http://schemas.netflix.com/catalog/title.season']"); string seasonid = null; foreach (var link in links) { if (link.Attribute("title").Value.EndsWith(" " + ep.Season)) { seasonid = link.Attribute("href").Value; break; } } if (seasonid != null) { request = new RestRequest { Path = seasonid.Replace("http://api.netflix.com/", string.Empty) + "/episodes" }; resp = XDocument.Parse(rest.Request(request).Content); var ids = resp.XPathSelectElements("//id").ToList(); if (ids.Count >= ep.Number) { return(ids[ep.Number - 1].Value.Replace("http://api.netflix.com/catalog/titles/programs/", "http://movies.netflix.com/WiPlayer?movieid=")); } } throw new OnlineVideoNotFoundException("No matching videos were found.", "Open Netflix search page", "http://movies.netflix.com/WiSearch?v1=" + Utils.EncodeURL(ep.Show.Title)); }
/// <summary> /// Asynchronously downloads the specified link. /// </summary> /// <param name="link"> /// The object containing the link. /// This can be an URL in a string or a <c>Link</c>/<c>Subtitle</c> object. /// </param> /// <param name="target">The target location.</param> /// <param name="token">The user token.</param> public void Download(object link, string target, string token = null) { _wc = new Utils.SmarterWebClient(); Uri uri; if (link is string) { uri = new Uri(link as string); } else if (link is Link) { uri = new Uri((link as Link).FileURL); if (!string.IsNullOrWhiteSpace((link as Link).Source.Cookies)) { _wc.Headers[HttpRequestHeader.Cookie] = (link as Link).Source.Cookies; } } else if (link is Subtitle) { uri = new Uri((link as Subtitle).FileURL); if (!string.IsNullOrWhiteSpace((link as Subtitle).Source.Cookies)) { _wc.Headers[HttpRequestHeader.Cookie] = (link as Subtitle).Source.Cookies; } } else { throw new Exception("The link object is an unsupported type."); } _wc.Headers[HttpRequestHeader.Referer] = "http://" + uri.DnsSafeHost + "/"; _wc.DownloadProgressChanged += (s, e) => DownloadProgressChanged.Fire(this, e.ProgressPercentage); _wc.DownloadFileCompleted += (s, e) => DownloadFileCompleted.Fire(this, target, (s as Utils.SmarterWebClient).FileName, token ?? string.Empty); var proxy = Settings.Get(uri.Host.Replace("www.", string.Empty) + " proxy"); if (!string.IsNullOrEmpty(proxy)) { var proxyUri = new Uri(proxy.Replace("$domain.", string.Empty)); switch (proxyUri.Scheme.ToLower()) { case "http": if (proxy.Contains("$url")) { uri = new Uri(proxy.Replace("$url", Utils.EncodeURL(uri.ToString()))); } else if (proxy.Contains("$domain") && proxy.Contains("$path")) { uri = new Uri(proxy.Replace("$domain", uri.DnsSafeHost).Replace("$path", uri.AbsolutePath)); } else { _wc.Proxy = new WebProxy(proxyUri.Host + ":" + proxyUri.Port); } break; case "socks4": case "socks4a": case "socks5": var tunnel = new HttpToSocks { RemoteProxy = HttpToSocks.Proxy.ParseUri(proxyUri) }; tunnel.Listen(); _wc.Proxy = (WebProxy)tunnel.LocalProxy; break; } } _wc.DownloadFileAsync(uri, target); }
/// <summary> /// Asynchronously downloads the specified link. /// </summary> /// <param name="link"> /// The object containing the link. /// This can be an URL in a string or a <c>Link</c>/<c>Subtitle</c> object. /// </param> /// <param name="target">The target location.</param> /// <param name="token">The user token.</param> public void Download(object link, string target, string token = null) { var id = Utils.Rand.Next(short.MaxValue); var st = DateTime.Now; _wc = new Utils.SmarterWebClient(); Uri uri; if (link is string) { uri = new Uri(link as string); } else if (link is Link) { uri = new Uri((link as Link).FileURL); if (!string.IsNullOrWhiteSpace((link as Link).Source.Cookies)) { _wc.Headers[HttpRequestHeader.Cookie] = (link as Link).Source.Cookies; } } else if (link is Subtitle) { uri = new Uri((link as Subtitle).FileURL); if (!string.IsNullOrWhiteSpace((link as Subtitle).Source.Cookies)) { _wc.Headers[HttpRequestHeader.Cookie] = (link as Subtitle).Source.Cookies; } } else { throw new Exception("The link object is an unsupported type."); } var domain = uri.Host.Replace("www.", string.Empty); Log.Debug("HTTP#{0} GET {1}", new[] { id.ToString(), uri.ToString() }); _wc.Headers[HttpRequestHeader.Referer] = "http://" + uri.DnsSafeHost + "/"; _wc.DownloadProgressChanged += (s, e) => DownloadProgressChanged.Fire(this, e.ProgressPercentage); _wc.DownloadFileCompleted += (s, e) => { Log.Debug("HTTP#" + id + " [" + domain + "] is " + Utils.GetFileSize(new FileInfo(target).Length) + " and took " + (DateTime.Now - st).TotalSeconds + "s."); if (Log.IsTraceEnabled) Log.Trace("HTTP#" + id + " [" + domain + "] is " + (s as Utils.SmarterWebClient).ContentType + ", saved to " + target + " with token " + token); DownloadFileCompleted.Fire(this, target, (s as Utils.SmarterWebClient).FileName, token ?? string.Empty); }; var proxy = default(string); var proxyId = default(object); if (Settings.Get<Dictionary<string, object>>("Proxied Domains").TryGetValue(domain, out proxyId)) { proxy = (string)Settings.Get<Dictionary<string, object>>("Proxies")[(string)proxyId]; } if (proxy != null) { var proxyUri = new Uri(proxy.Replace("$domain.", string.Empty)); switch (proxyUri.Scheme.ToLower()) { case "http": if (proxy.Contains("$url")) { uri = new Uri(proxy.Replace("$url", Utils.EncodeURL(uri.ToString()))); } else if (proxy.Contains("$domain") && proxy.Contains("$path")) { uri = new Uri(proxy.Replace("$domain", uri.DnsSafeHost).Replace("$path", uri.AbsolutePath)); } else { _wc.Proxy = new WebProxy(proxyUri.Host + ":" + proxyUri.Port); } break; case "socks4": case "socks4a": case "socks5": var tunnel = new HttpToSocks { RemoteProxy = HttpToSocks.Proxy.ParseUri(proxyUri) }; tunnel.Listen(); _wc.Proxy = (WebProxy)tunnel.LocalProxy; break; } Log.Debug("HTTP#" + id + " [" + domain + "] is proxied through " + proxyId + " (" + proxyUri + ")"); } _wc.DownloadFileAsync(uri, target); }