1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
/* 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.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Net;
namespace OpenSubtitlesHandler
{
/// <summary>
/// Include helper methods. All member are statics.
/// </summary>
public sealed class Utilities
{
private const string XML_RPC_SERVER = "http://api.opensubtitles.org/xml-rpc";
/// <summary>
/// Compute movie hash
/// </summary>
/// <param name="fileName">The complete media file path</param>
/// <returns>The hash as Hexadecimal string</returns>
public static string ComputeHash(string fileName)
{
byte[] hash = MovieHasher.ComputeMovieHash(fileName);
return MovieHasher.ToHexadecimal(hash);
}
/// <summary>
/// Compute md5 for a file
/// </summary>
/// <param name="filename">The complete file path</param>
/// <returns>MD5 of the file</returns>
public static string ComputeMd5(string filename)
{
var md5 = MD5.Create();
var sb = new StringBuilder();
Stream str = new FileStream(filename, FileMode.Open, FileAccess.Read);
foreach (var b in md5.ComputeHash(str))
sb.Append(b.ToString("x2").ToLower());
str.Close();
return sb.ToString();
}
/// <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)
{
MemoryStream target = new MemoryStream();
using (System.IO.Compression.GZipStream decompressionStream = new System.IO.Compression.GZipStream(dataToDecompress,
System.IO.Compression.CompressionMode.Decompress))
{
decompressionStream.CopyTo(target);
}
return target.GetBuffer();
}
/// <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>
/// <param name="responseStream">The response stream. Expects a stream that doesn't support seek.</param>
/// <param name="encoding">The encoding that should be used to decode buffer</param>
/// <returns>The string of the stream after decode using given encoding</returns>
public static string GetStreamString(Stream responseStream, Encoding encoding)
{
// 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);
}
responseStream.Close();
return encoding.GetString(data.ToArray());
}
/// <summary>
/// Handle server response stream and decode it as ASCII encoding string.
/// </summary>
/// <param name="responseStream">The response stream. Expects a stream that doesn't support seek.</param>
/// <returns>The string of the stream after decode using ASCII encoding</returns>
public static string GetStreamString(Stream responseStream)
{
return GetStreamString(responseStream, Encoding.ASCII);
}
public static IHttpClient HttpClient { get; set; }
/// <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)
{
return SendRequestAsync(request, userAgent, CancellationToken.None).Result;
//HttpWebRequest req = (HttpWebRequest)WebRequest.Create(XML_RPC_SERVER);
//req.ContentType = "text/xml";
//req.Host = "api.opensubtitles.org:80";
//req.Method = "POST";
//req.UserAgent = "xmlrpc-epi-php/0.2 (PHP)";
//req.ContentLength = request.Length;
//ServicePointManager.Expect100Continue = false;
//try
//{
// using (Stream stm = req.GetRequestStream())
// {
// stm.Write(request, 0, request.Length);
// }
// WebResponse response = req.GetResponse();
// return response.GetResponseStream();
//}
//catch (Exception ex)
//{
// Stream errorStream = new MemoryStream();
// byte[] dd = Encoding.ASCII.GetBytes("ERROR: " + ex.Message);
// errorStream.Write(dd, 0, dd.Length);
// errorStream.Position = 0;
// return errorStream;
//}
}
public static async Task<Stream> SendRequestAsync(byte[] request, string userAgent, CancellationToken cancellationToken)
{
var options = new HttpRequestOptions
{
RequestContentBytes = request,
RequestContentType = "text/xml",
UserAgent = userAgent,
Host = "api.opensubtitles.org:80",
Url = XML_RPC_SERVER,
// Response parsing will fail with this enabled
EnableHttpCompression = false,
CancellationToken = cancellationToken
};
if (string.IsNullOrEmpty(options.UserAgent))
{
options.UserAgent = "xmlrpc-epi-php/0.2 (PHP)";
}
var result = await HttpClient.Post(options).ConfigureAwait(false);
return result.Content;
//HttpWebRequest req = (HttpWebRequest)WebRequest.Create(XML_RPC_SERVER);
//req.ContentType = "text/xml";
//req.Host = "api.opensubtitles.org:80";
//req.Method = "POST";
//req.UserAgent = "xmlrpc-epi-php/0.2 (PHP)";
//req.ContentLength = request.Length;
//ServicePointManager.Expect100Continue = false;
//try
//{
// using (Stream stm = req.GetRequestStream())
// {
// stm.Write(request, 0, request.Length);
// }
// WebResponse response = req.GetResponse();
// return response.GetResponseStream();
//}
//catch (Exception ex)
//{
// Stream errorStream = new MemoryStream();
// byte[] dd = Encoding.ASCII.GetBytes("ERROR: " + ex.Message);
// errorStream.Write(dd, 0, dd.Length);
// errorStream.Position = 0;
// return errorStream;
//}
}
}
}
|