2014-05-05 04:36:45 +00:00
|
|
|
|
/* This file is part of OpenSubtitles Handler
|
|
|
|
|
A library that handle OpenSubtitles.org XML-RPC methods.
|
|
|
|
|
|
|
|
|
|
Copyright © Ala Ibrahim Hadid 2013
|
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.IO;
|
2014-05-07 02:28:19 +00:00
|
|
|
|
using System.Threading;
|
2014-05-05 04:36:45 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
2016-10-27 22:54:56 +00:00
|
|
|
|
using MediaBrowser.Model.Cryptography;
|
2016-11-08 18:44:23 +00:00
|
|
|
|
using MediaBrowser.Model.Text;
|
2014-05-05 04:36:45 +00:00
|
|
|
|
|
|
|
|
|
namespace OpenSubtitlesHandler
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Include helper methods. All member are statics.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class Utilities
|
|
|
|
|
{
|
2016-11-08 18:44:23 +00:00
|
|
|
|
public static ICryptoProvider CryptographyProvider { get; set; }
|
2016-10-30 06:36:38 +00:00
|
|
|
|
public static IHttpClient HttpClient { get; set; }
|
2016-11-08 18:44:23 +00:00
|
|
|
|
public static ITextEncoding EncodingHelper { get; set; }
|
2016-10-30 06:36:38 +00:00
|
|
|
|
|
2016-12-27 08:15:29 +00:00
|
|
|
|
//private static string XML_RPC_SERVER = "https://api.opensubtitles.org/xml-rpc";
|
|
|
|
|
private static string XML_RPC_SERVER = "https://92.240.234.122/xml-rpc";
|
|
|
|
|
private static string HostHeader = "api.opensubtitles.org:443";
|
2014-05-05 04:36:45 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Compute movie hash
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The hash as Hexadecimal string</returns>
|
2016-10-27 22:54:56 +00:00
|
|
|
|
public static string ComputeHash(Stream stream)
|
2014-05-05 04:36:45 +00:00
|
|
|
|
{
|
2016-10-27 22:54:56 +00:00
|
|
|
|
byte[] hash = MovieHasher.ComputeMovieHash(stream);
|
2014-05-05 04:36:45 +00:00
|
|
|
|
return MovieHasher.ToHexadecimal(hash);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Decompress data using GZip
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dataToDecompress">The stream that hold the data</param>
|
|
|
|
|
/// <returns>Bytes array of decompressed data</returns>
|
|
|
|
|
public static byte[] Decompress(Stream dataToDecompress)
|
|
|
|
|
{
|
2016-10-27 22:54:56 +00:00
|
|
|
|
using (MemoryStream target = new MemoryStream())
|
2014-05-05 04:36:45 +00:00
|
|
|
|
{
|
2016-10-27 22:54:56 +00:00
|
|
|
|
using (System.IO.Compression.GZipStream decompressionStream = new System.IO.Compression.GZipStream(dataToDecompress, System.IO.Compression.CompressionMode.Decompress))
|
|
|
|
|
{
|
|
|
|
|
decompressionStream.CopyTo(target);
|
|
|
|
|
}
|
|
|
|
|
return target.ToArray();
|
2014-05-05 04:36:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Compress data using GZip (the retunred buffer will be WITHOUT HEADER)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dataToCompress">The stream that hold the data</param>
|
|
|
|
|
/// <returns>Bytes array of compressed data WITHOUT HEADER bytes</returns>
|
|
|
|
|
public static byte[] Compress(Stream dataToCompress)
|
|
|
|
|
{
|
|
|
|
|
/*using (var compressed = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
using (var compressor = new System.IO.Compression.GZipStream(compressed,
|
|
|
|
|
System.IO.Compression.CompressionMode.Compress))
|
|
|
|
|
{
|
|
|
|
|
dataToCompress.CopyTo(compressor);
|
|
|
|
|
}
|
|
|
|
|
// Get the compressed bytes only after closing the GZipStream
|
|
|
|
|
return compressed.ToArray();
|
|
|
|
|
}*/
|
|
|
|
|
//using (var compressedOutput = new MemoryStream())
|
|
|
|
|
//{
|
|
|
|
|
// using (var compressedStream = new ZlibStream(compressedOutput,
|
|
|
|
|
// Ionic.Zlib.CompressionMode.Compress,
|
|
|
|
|
// CompressionLevel.Default, false))
|
|
|
|
|
// {
|
|
|
|
|
// var buffer = new byte[4096];
|
|
|
|
|
// int byteCount;
|
|
|
|
|
// do
|
|
|
|
|
// {
|
|
|
|
|
// byteCount = dataToCompress.Read(buffer, 0, buffer.Length);
|
|
|
|
|
|
|
|
|
|
// if (byteCount > 0)
|
|
|
|
|
// {
|
|
|
|
|
// compressedStream.Write(buffer, 0, byteCount);
|
|
|
|
|
// }
|
|
|
|
|
// } while (byteCount > 0);
|
|
|
|
|
// }
|
|
|
|
|
// return compressedOutput.ToArray();
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handle server response stream and decode it as given encoding string.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The string of the stream after decode using given encoding</returns>
|
2016-10-30 06:36:38 +00:00
|
|
|
|
public static string GetStreamString(Stream responseStream)
|
2014-05-05 04:36:45 +00:00
|
|
|
|
{
|
2016-10-27 22:54:56 +00:00
|
|
|
|
using (responseStream)
|
2014-05-05 04:36:45 +00:00
|
|
|
|
{
|
2016-10-27 22:54:56 +00:00
|
|
|
|
// Handle response, should be XML text.
|
|
|
|
|
List<byte> data = new List<byte>();
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
int r = responseStream.ReadByte();
|
|
|
|
|
if (r < 0)
|
|
|
|
|
break;
|
|
|
|
|
data.Add((byte)r);
|
|
|
|
|
}
|
2016-10-30 06:36:38 +00:00
|
|
|
|
var bytes = data.ToArray();
|
2016-11-08 18:44:23 +00:00
|
|
|
|
return EncodingHelper.GetASCIIEncoding().GetString(bytes, 0, bytes.Length);
|
2014-05-05 04:36:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-30 06:36:38 +00:00
|
|
|
|
|
|
|
|
|
public static byte[] GetASCIIBytes(string text)
|
2014-05-05 04:36:45 +00:00
|
|
|
|
{
|
2016-11-08 18:44:23 +00:00
|
|
|
|
return EncodingHelper.GetASCIIEncoding().GetBytes(text);
|
2014-05-05 04:36:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Send a request to the server
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request buffer to send as bytes array.</param>
|
|
|
|
|
/// <param name="userAgent">The user agent value.</param>
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
2014-05-07 02:28:19 +00:00
|
|
|
|
return SendRequestAsync(request, userAgent, CancellationToken.None).Result;
|
2014-05-05 04:36:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-07 02:28:19 +00:00
|
|
|
|
public static async Task<Stream> SendRequestAsync(byte[] request, string userAgent, CancellationToken cancellationToken)
|
2014-05-05 04:36:45 +00:00
|
|
|
|
{
|
|
|
|
|
var options = new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
RequestContentBytes = request,
|
|
|
|
|
RequestContentType = "text/xml",
|
2014-05-07 02:28:19 +00:00
|
|
|
|
UserAgent = userAgent,
|
2016-12-27 08:15:29 +00:00
|
|
|
|
Host = HostHeader,
|
2014-05-07 02:28:19 +00:00
|
|
|
|
Url = XML_RPC_SERVER,
|
|
|
|
|
|
|
|
|
|
// Response parsing will fail with this enabled
|
|
|
|
|
EnableHttpCompression = false,
|
|
|
|
|
|
2016-10-06 18:55:01 +00:00
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
BufferContent = false
|
2014-05-05 04:36:45 +00:00
|
|
|
|
};
|
|
|
|
|
|
2014-05-07 02:28:19 +00:00
|
|
|
|
if (string.IsNullOrEmpty(options.UserAgent))
|
|
|
|
|
{
|
|
|
|
|
options.UserAgent = "xmlrpc-epi-php/0.2 (PHP)";
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-05 04:36:45 +00:00
|
|
|
|
var result = await HttpClient.Post(options).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return result.Content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|