Utilities.cs 6.5 KB

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