diff options
Diffstat (limited to 'OpenSubtitlesHandler')
| -rw-r--r-- | OpenSubtitlesHandler/OpenSubtitles.cs | 52 | ||||
| -rw-r--r-- | OpenSubtitlesHandler/Utilities.cs | 20 |
2 files changed, 68 insertions, 4 deletions
diff --git a/OpenSubtitlesHandler/OpenSubtitles.cs b/OpenSubtitlesHandler/OpenSubtitles.cs index ba3c461a1..5353586c8 100644 --- a/OpenSubtitlesHandler/OpenSubtitles.cs +++ b/OpenSubtitlesHandler/OpenSubtitles.cs @@ -20,6 +20,8 @@ using System; using System.Text; using System.Collections.Generic; using System.IO; +using System.Threading; +using System.Threading.Tasks; using OpenSubtitlesHandler.Console; using XmlRpcHandler; @@ -96,6 +98,56 @@ namespace OpenSubtitlesHandler } return new MethodResponseError("Fail", "Log in failed !"); } + + public static async Task<IMethodResponse> LogInAsync(string userName, string password, string language, CancellationToken cancellationToken) + { + // Method call .. + List<IXmlRpcValue> parms = new List<IXmlRpcValue>(); + parms.Add(new XmlRpcValueBasic(userName)); + parms.Add(new XmlRpcValueBasic(password)); + parms.Add(new XmlRpcValueBasic(language)); + parms.Add(new XmlRpcValueBasic(XML_PRC_USERAGENT)); + XmlRpcMethodCall call = new XmlRpcMethodCall("LogIn", parms); + OSHConsole.WriteLine("Sending LogIn request to the server ...", DebugCode.Good); + + //File.WriteAllText(".\\request.txt", Encoding.UTF8.GetString(XmlRpcGenerator.Generate(call))); + // Send the request to the server + var stream = await Utilities.SendRequestAsync(XmlRpcGenerator.Generate(call), XML_PRC_USERAGENT, cancellationToken) + .ConfigureAwait(false); + + string response = Utilities.GetStreamString(stream); + + if (!response.Contains("ERROR:")) + { + // No error occur, get and decode the response. We expect Struct here. + XmlRpcMethodCall[] calls = XmlRpcGenerator.DecodeMethodResponse(response); + if (calls.Length > 0) + { + if (calls[0].Parameters.Count > 0) + { + XmlRpcValueStruct mainStruct = (XmlRpcValueStruct)calls[0].Parameters[0]; + MethodResponseLogIn re = new MethodResponseLogIn("Success", "Log in successful."); + foreach (XmlRpcStructMember MEMBER in mainStruct.Members) + { + switch (MEMBER.Name) + { + case "token": re.Token = TOKEN = MEMBER.Data.Data.ToString(); OSHConsole.WriteLine(MEMBER.Name + "= " + MEMBER.Data.Data.ToString()); break; + case "seconds": re.Seconds = (double)MEMBER.Data.Data; OSHConsole.WriteLine(MEMBER.Name + "= " + MEMBER.Data.Data.ToString()); break; + case "status": re.Status = MEMBER.Data.Data.ToString(); OSHConsole.WriteLine(MEMBER.Name + "= " + MEMBER.Data.Data.ToString()); break; + } + } + return re; + } + } + } + else + { + OSHConsole.WriteLine(response, DebugCode.Error); + return new MethodResponseError("Fail", response); + } + return new MethodResponseError("Fail", "Log in failed !"); + } + /// <summary> /// Log out from the server. Call this to terminate the session. /// </summary> diff --git a/OpenSubtitlesHandler/Utilities.cs b/OpenSubtitlesHandler/Utilities.cs index 5c72f4fde..7f0f93009 100644 --- a/OpenSubtitlesHandler/Utilities.cs +++ b/OpenSubtitlesHandler/Utilities.cs @@ -24,6 +24,7 @@ using System.IO; using System.IO.Compression; using System.Net; using System.Security.Cryptography; +using System.Threading; using System.Threading.Tasks; using MediaBrowser.Common.Net; @@ -161,7 +162,7 @@ namespace OpenSubtitlesHandler /// <returns>Response of the server or stream of error message as string started with 'ERROR:' keyword.</returns> public static Stream SendRequest(byte[] request, string userAgent) { - return SendRequestAsync(request, userAgent).Result; + return SendRequestAsync(request, userAgent, CancellationToken.None).Result; //HttpWebRequest req = (HttpWebRequest)WebRequest.Create(XML_RPC_SERVER); //req.ContentType = "text/xml"; @@ -190,16 +191,27 @@ namespace OpenSubtitlesHandler //} } - public static async Task<Stream> SendRequestAsync(byte[] request, string userAgent) + public static async Task<Stream> SendRequestAsync(byte[] request, string userAgent, CancellationToken cancellationToken) { var options = new HttpRequestOptions { RequestContentBytes = request, RequestContentType = "text/xml", - UserAgent = "xmlrpc-epi-php/0.2 (PHP)", - Url = XML_RPC_SERVER + UserAgent = userAgent, + Host = "api.opensubtitles.org:80", + Url = XML_RPC_SERVER, + + // Response parsing will fail with this enabled + EnableHttpCompression = false, + + CancellationToken = cancellationToken }; + if (string.IsNullOrEmpty(options.UserAgent)) + { + options.UserAgent = "xmlrpc-epi-php/0.2 (PHP)"; + } + var result = await HttpClient.Post(options).ConfigureAwait(false); return result.Content; |
