diff --git a/src/Jackett/CookieContainerExtensions.cs b/src/Jackett/CookieContainerExtensions.cs new file mode 100644 index 000000000..23f253f9d --- /dev/null +++ b/src/Jackett/CookieContainerExtensions.cs @@ -0,0 +1,22 @@ +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace Jackett +{ + public static class CookieContainerExtensions + { + public static void FillFromJson(this CookieContainer cookies, Uri uri, JArray json) + { + foreach (var cookie in json) + { + var w = ((string)cookie).Split(':'); + cookies.Add(uri, new Cookie(w[0], w[1])); + } + } + } +} diff --git a/src/Jackett/IndexerInterface.cs b/src/Jackett/IndexerInterface.cs index 644aad426..f833eebc8 100644 --- a/src/Jackett/IndexerInterface.cs +++ b/src/Jackett/IndexerInterface.cs @@ -18,7 +18,7 @@ namespace Jackett Task GetConfigurationForSetup(); // Called when web API wants to apply setup configuration via web API, usually this is where login and storing cookie happens - Task ApplyConfiguration(JToken jsonConfig); + Task ApplyConfiguration(JToken configJson); // Called to check if configuration (cookie) is correct and indexer connection works Task VerifyConnection(); diff --git a/src/Jackett/Indexers/BitMeTV.cs b/src/Jackett/Indexers/BitMeTV.cs index abe7212a3..81fcd9b3f 100644 --- a/src/Jackett/Indexers/BitMeTV.cs +++ b/src/Jackett/Indexers/BitMeTV.cs @@ -133,13 +133,7 @@ namespace Jackett public void LoadFromSavedConfiguration(JToken jsonConfig) { - - foreach (var cookie in jsonConfig["cookies"]) - { - var w = ((string)cookie).Split(':'); - cookies.Add(new Uri(BaseUrl), new Cookie(w[0], w[1])); - } - + cookies.FillFromJson(new Uri(BaseUrl), (JArray)jsonConfig["cookies"]); IsConfigured = true; } } diff --git a/src/Jackett/Indexers/Freshon.cs b/src/Jackett/Indexers/Freshon.cs index a95b61a52..0bfe56636 100644 --- a/src/Jackett/Indexers/Freshon.cs +++ b/src/Jackett/Indexers/Freshon.cs @@ -1,7 +1,11 @@ -using Newtonsoft.Json.Linq; +using CsQuery; +using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; using System.Web.UI.WebControls; @@ -10,47 +14,134 @@ namespace Jackett { public class Freshon : IndexerInterface { - - public string DisplayName + class FreshonConfig : ConfigurationData { - get { return "FreshOnTV"; } + public StringItem Username { get; private set; } + public StringItem Password { get; private set; } + + public FreshonConfig() + { + Username = new StringItem { Name = "Username", ItemType = ItemType.InputString }; + Password = new StringItem { Name = "Password", ItemType = ItemType.InputString }; + } + + public override Item[] GetItems() + { + return new Item[] { Username, Password }; + } } - public string DisplayDescription - { - get { return "Our goal is to provide the latest stuff in the TV show domain"; } - } + static string BaseUrl = "https://freshon.tv"; + static string LoginUrl = BaseUrl + "/login.php"; + static string LoginPostUrl = BaseUrl + "/login.php?action=makelogin"; + static string SearchUrl = BaseUrl + "/browse.php"; - public Uri SiteLink + static string chromeUserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36"; + + CookieContainer cookies; + HttpClientHandler handler; + HttpClient client; + + public bool IsConfigured { get; private set; } + + public string DisplayName { get { return "FreshOnTV"; } } + + public string DisplayDescription { get { return "Our goal is to provide the latest stuff in the TV show domain"; } } + + public Uri SiteLink { get { return new Uri("https://freshon.tv/"); } } + + public event Action OnSaveConfigurationRequested; + + public Freshon() { - get { return new Uri("https://freshon.tv/"); } + IsConfigured = false; + cookies = new CookieContainer(); + handler = new HttpClientHandler + { + CookieContainer = cookies, + AllowAutoRedirect = true, + UseCookies = true, + }; + client = new HttpClient(handler); } public Task GetConfigurationForSetup() { - throw new NotImplementedException(); + return Task.Run(async () => + { + //var loginPage = await client.GetAsync(LoginUrl); + var config = new FreshonConfig(); + return (ConfigurationData)config; + }); } - public Task ApplyConfiguration(JToken jsonConfig) + public Task ApplyConfiguration(JToken configJson) { - throw new NotImplementedException(); + return Task.Run(async () => + { + var config = new FreshonConfig(); + config.LoadValuesFromJson(configJson); + + var pairs = new Dictionary + { + { "username", config.Username.Value}, + { "password", config.Password.Value} + }; + + var content = new FormUrlEncodedContent(pairs); + var message = new HttpRequestMessage(); + message.Method = HttpMethod.Post; + message.Content = content; + message.RequestUri = new Uri(LoginPostUrl); + message.Headers.Referrer = new Uri(LoginUrl); + message.Headers.UserAgent.ParseAdd(chromeUserAgent); + + var response = await client.SendAsync(message); + var responseContent = await response.Content.ReadAsStringAsync(); + + if (!responseContent.Contains("/logout.php")) + { + CQ dom = responseContent; + var messageEl = dom[".error_text"]; + var errorMessage = messageEl.Text().Trim(); + throw new ExceptionWithConfigData(errorMessage, (ConfigurationData)config); + } + else + { + var configSaveData = new JObject(); + configSaveData["cookies"] = new JArray(( + from cookie in cookies.GetCookies(new Uri(BaseUrl)).Cast() + select cookie.Name + ":" + cookie.Value + ).ToArray()); + + if (OnSaveConfigurationRequested != null) + OnSaveConfigurationRequested(this, configSaveData); + + IsConfigured = true; + } + }); } public Task VerifyConnection() { - throw new NotImplementedException(); - } + return Task.Run(async () => + { + var message = new HttpRequestMessage(); + message.Method = HttpMethod.Get; + message.RequestUri = new Uri(SearchUrl); + message.Headers.UserAgent.ParseAdd(chromeUserAgent); - public event Action OnSaveConfigurationRequested; - - public bool IsConfigured - { - get { return false; } + var response = await client.SendAsync(message); + var result = await response.Content.ReadAsStringAsync(); + if (!result.Contains("/logout.php")) + throw new Exception("Detected as not logged in"); + }); } public void LoadFromSavedConfiguration(JToken jsonConfig) { - throw new NotImplementedException(); + cookies.FillFromJson(new Uri(BaseUrl), (JArray)jsonConfig["cookies"]); + IsConfigured = true; } } } diff --git a/src/Jackett/Jackett.csproj b/src/Jackett/Jackett.csproj index 366507780..923d66d14 100644 --- a/src/Jackett/Jackett.csproj +++ b/src/Jackett/Jackett.csproj @@ -70,6 +70,7 @@ +