Utilities.cs 6.5 KB

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