Utilities.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.Text;
  18. using System.IO;
  19. using System.Threading;
  20. using System.Threading.Tasks;
  21. using MediaBrowser.Common.Net;
  22. using MediaBrowser.Model.Cryptography;
  23. using MediaBrowser.Model.Text;
  24. namespace OpenSubtitlesHandler
  25. {
  26. /// <summary>
  27. /// Include helper methods. All member are statics.
  28. /// </summary>
  29. public sealed class Utilities
  30. {
  31. public static ICryptoProvider CryptographyProvider { get; set; }
  32. public static IHttpClient HttpClient { get; set; }
  33. public static ITextEncoding EncodingHelper { get; set; }
  34. private static string XML_RPC_SERVER = "https://api.opensubtitles.org/xml-rpc";
  35. //private static string XML_RPC_SERVER = "https://92.240.234.122/xml-rpc";
  36. private static string HostHeader = "api.opensubtitles.org:443";
  37. /// <summary>
  38. /// Compute movie hash
  39. /// </summary>
  40. /// <returns>The hash as Hexadecimal string</returns>
  41. public static string ComputeHash(Stream stream)
  42. {
  43. byte[] hash = MovieHasher.ComputeMovieHash(stream);
  44. return MovieHasher.ToHexadecimal(hash);
  45. }
  46. /// <summary>
  47. /// Decompress data using GZip
  48. /// </summary>
  49. /// <param name="dataToDecompress">The stream that hold the data</param>
  50. /// <returns>Bytes array of decompressed data</returns>
  51. public static byte[] Decompress(Stream dataToDecompress)
  52. {
  53. using (MemoryStream target = new MemoryStream())
  54. {
  55. using (System.IO.Compression.GZipStream decompressionStream = new System.IO.Compression.GZipStream(dataToDecompress, System.IO.Compression.CompressionMode.Decompress))
  56. {
  57. decompressionStream.CopyTo(target);
  58. }
  59. return target.ToArray();
  60. }
  61. }
  62. /// <summary>
  63. /// Compress data using GZip (the retunred buffer will be WITHOUT HEADER)
  64. /// </summary>
  65. /// <param name="dataToCompress">The stream that hold the data</param>
  66. /// <returns>Bytes array of compressed data WITHOUT HEADER bytes</returns>
  67. public static byte[] Compress(Stream dataToCompress)
  68. {
  69. /*using (var compressed = new MemoryStream())
  70. {
  71. using (var compressor = new System.IO.Compression.GZipStream(compressed,
  72. System.IO.Compression.CompressionMode.Compress))
  73. {
  74. dataToCompress.CopyTo(compressor);
  75. }
  76. // Get the compressed bytes only after closing the GZipStream
  77. return compressed.ToArray();
  78. }*/
  79. //using (var compressedOutput = new MemoryStream())
  80. //{
  81. // using (var compressedStream = new ZlibStream(compressedOutput,
  82. // Ionic.Zlib.CompressionMode.Compress,
  83. // CompressionLevel.Default, false))
  84. // {
  85. // var buffer = new byte[4096];
  86. // int byteCount;
  87. // do
  88. // {
  89. // byteCount = dataToCompress.Read(buffer, 0, buffer.Length);
  90. // if (byteCount > 0)
  91. // {
  92. // compressedStream.Write(buffer, 0, byteCount);
  93. // }
  94. // } while (byteCount > 0);
  95. // }
  96. // return compressedOutput.ToArray();
  97. //}
  98. throw new NotImplementedException();
  99. }
  100. /// <summary>
  101. /// Handle server response stream and decode it as given encoding string.
  102. /// </summary>
  103. /// <returns>The string of the stream after decode using given encoding</returns>
  104. public static string GetStreamString(Stream responseStream)
  105. {
  106. using (responseStream)
  107. {
  108. // Handle response, should be XML text.
  109. List<byte> data = new List<byte>();
  110. while (true)
  111. {
  112. int r = responseStream.ReadByte();
  113. if (r < 0)
  114. break;
  115. data.Add((byte)r);
  116. }
  117. var bytes = data.ToArray();
  118. return EncodingHelper.GetASCIIEncoding().GetString(bytes, 0, bytes.Length);
  119. }
  120. }
  121. public static byte[] GetASCIIBytes(string text)
  122. {
  123. return EncodingHelper.GetASCIIEncoding().GetBytes(text);
  124. }
  125. /// <summary>
  126. /// Send a request to the server
  127. /// </summary>
  128. /// <param name="request">The request buffer to send as bytes array.</param>
  129. /// <param name="userAgent">The user agent value.</param>
  130. /// <returns>Response of the server or stream of error message as string started with 'ERROR:' keyword.</returns>
  131. public static Stream SendRequest(byte[] request, string userAgent)
  132. {
  133. return SendRequestAsync(request, userAgent, CancellationToken.None).Result;
  134. }
  135. public static async Task<Stream> SendRequestAsync(byte[] request, string userAgent, CancellationToken cancellationToken)
  136. {
  137. var options = new HttpRequestOptions
  138. {
  139. RequestContentBytes = request,
  140. RequestContentType = "text/xml",
  141. UserAgent = userAgent,
  142. Host = HostHeader,
  143. Url = XML_RPC_SERVER,
  144. // Response parsing will fail with this enabled
  145. EnableHttpCompression = false,
  146. CancellationToken = cancellationToken,
  147. BufferContent = false
  148. };
  149. if (string.IsNullOrEmpty(options.UserAgent))
  150. {
  151. options.UserAgent = "xmlrpc-epi-php/0.2 (PHP)";
  152. }
  153. var result = await HttpClient.Post(options).ConfigureAwait(false);
  154. return result.Content;
  155. }
  156. }
  157. }