Utilities.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* This file is part of OpenSubtitles Handler
  2. A library that handle OpenSubtitles.org XML-RPC methods.
  3. Copyright © Ala Ibrahim Hadid 2013
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.IO;
  18. using System.Threading;
  19. using System.Threading.Tasks;
  20. using MediaBrowser.Common.Net;
  21. using MediaBrowser.Model.Cryptography;
  22. using MediaBrowser.Model.Text;
  23. namespace OpenSubtitlesHandler
  24. {
  25. /// <summary>
  26. /// Include helper methods. All member are statics.
  27. /// </summary>
  28. public sealed class Utilities
  29. {
  30. public static ICryptoProvider CryptographyProvider { get; set; }
  31. public static IHttpClient HttpClient { get; set; }
  32. public static ITextEncoding EncodingHelper { get; set; }
  33. private static string XML_RPC_SERVER = "https://api.opensubtitles.org/xml-rpc";
  34. //private static string XML_RPC_SERVER = "https://92.240.234.122/xml-rpc";
  35. private static string HostHeader = "api.opensubtitles.org:443";
  36. /// <summary>
  37. /// Compute movie hash
  38. /// </summary>
  39. /// <returns>The hash as Hexadecimal string</returns>
  40. public static string ComputeHash(Stream stream)
  41. {
  42. byte[] hash = MovieHasher.ComputeMovieHash(stream);
  43. return MovieHasher.ToHexadecimal(hash);
  44. }
  45. /// <summary>
  46. /// Decompress data using GZip
  47. /// </summary>
  48. /// <param name="dataToDecompress">The stream that hold the data</param>
  49. /// <returns>Bytes array of decompressed data</returns>
  50. public static byte[] Decompress(Stream dataToDecompress)
  51. {
  52. using (var target = new MemoryStream())
  53. {
  54. using (var decompressionStream = new System.IO.Compression.GZipStream(dataToDecompress, System.IO.Compression.CompressionMode.Decompress))
  55. {
  56. decompressionStream.CopyTo(target);
  57. }
  58. return target.ToArray();
  59. }
  60. }
  61. /// <summary>
  62. /// Compress data using GZip (the retunred buffer will be WITHOUT HEADER)
  63. /// </summary>
  64. /// <param name="dataToCompress">The stream that hold the data</param>
  65. /// <returns>Bytes array of compressed data WITHOUT HEADER bytes</returns>
  66. public static byte[] Compress(Stream dataToCompress)
  67. {
  68. /*using (var compressed = new MemoryStream())
  69. {
  70. using (var compressor = new System.IO.Compression.GZipStream(compressed,
  71. System.IO.Compression.CompressionMode.Compress))
  72. {
  73. dataToCompress.CopyTo(compressor);
  74. }
  75. // Get the compressed bytes only after closing the GZipStream
  76. return compressed.ToArray();
  77. }*/
  78. //using (var compressedOutput = new MemoryStream())
  79. //{
  80. // using (var compressedStream = new ZlibStream(compressedOutput,
  81. // Ionic.Zlib.CompressionMode.Compress,
  82. // CompressionLevel.Default, false))
  83. // {
  84. // var buffer = new byte[4096];
  85. // int byteCount;
  86. // do
  87. // {
  88. // byteCount = dataToCompress.Read(buffer, 0, buffer.Length);
  89. // if (byteCount > 0)
  90. // {
  91. // compressedStream.Write(buffer, 0, byteCount);
  92. // }
  93. // } while (byteCount > 0);
  94. // }
  95. // return compressedOutput.ToArray();
  96. //}
  97. throw new NotImplementedException();
  98. }
  99. /// <summary>
  100. /// Handle server response stream and decode it as given encoding string.
  101. /// </summary>
  102. /// <returns>The string of the stream after decode using given encoding</returns>
  103. public static string GetStreamString(Stream responseStream)
  104. {
  105. using (responseStream)
  106. {
  107. // Handle response, should be XML text.
  108. var data = new List<byte>();
  109. while (true)
  110. {
  111. int r = responseStream.ReadByte();
  112. if (r < 0)
  113. break;
  114. data.Add((byte)r);
  115. }
  116. var bytes = data.ToArray();
  117. return EncodingHelper.GetASCIIEncoding().GetString(bytes, 0, bytes.Length);
  118. }
  119. }
  120. public static byte[] GetASCIIBytes(string text)
  121. {
  122. return EncodingHelper.GetASCIIEncoding().GetBytes(text);
  123. }
  124. /// <summary>
  125. /// Send a request to the server
  126. /// </summary>
  127. /// <param name="request">The request buffer to send as bytes array.</param>
  128. /// <param name="userAgent">The user agent value.</param>
  129. /// <returns>Response of the server or stream of error message as string started with 'ERROR:' keyword.</returns>
  130. public static Stream SendRequest(byte[] request, string userAgent)
  131. {
  132. return SendRequestAsync(request, userAgent, CancellationToken.None).Result;
  133. }
  134. public static async Task<Stream> SendRequestAsync(byte[] request, string userAgent, CancellationToken cancellationToken)
  135. {
  136. var options = new HttpRequestOptions
  137. {
  138. RequestContentBytes = request,
  139. RequestContentType = "text/xml",
  140. UserAgent = userAgent,
  141. Host = HostHeader,
  142. Url = XML_RPC_SERVER,
  143. // Response parsing will fail with this enabled
  144. EnableHttpCompression = false,
  145. CancellationToken = cancellationToken,
  146. BufferContent = false
  147. };
  148. if (string.IsNullOrEmpty(options.UserAgent))
  149. {
  150. options.UserAgent = "xmlrpc-epi-php/0.2 (PHP)";
  151. }
  152. var result = await HttpClient.Post(options).ConfigureAwait(false);
  153. return result.Content;
  154. }
  155. }
  156. }