2014-05-07 18:38:50 +00:00
|
|
|
|
using MediaBrowser.Common.Events;
|
2014-05-07 02:28:19 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2014-05-07 18:38:50 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.Security;
|
2014-05-07 02:28:19 +00:00
|
|
|
|
using MediaBrowser.Controller.Subtitles;
|
2014-05-07 18:38:50 +00:00
|
|
|
|
using MediaBrowser.Model.Configuration;
|
2014-05-05 14:45:45 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2014-05-05 04:36:45 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2014-05-07 02:28:19 +00:00
|
|
|
|
using MediaBrowser.Model.Providers;
|
2014-05-05 04:36:45 +00:00
|
|
|
|
using OpenSubtitlesHandler;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.Subtitles
|
|
|
|
|
{
|
2014-05-07 18:38:50 +00:00
|
|
|
|
public class OpenSubtitleDownloader : ISubtitleProvider, IDisposable
|
2014-05-05 04:36:45 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
|
|
|
|
|
2014-05-07 18:38:50 +00:00
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
|
private readonly IEncryptionManager _encryption;
|
|
|
|
|
|
|
|
|
|
public OpenSubtitleDownloader(ILogManager logManager, IHttpClient httpClient, IServerConfigurationManager config, IEncryptionManager encryption)
|
2014-05-05 04:36:45 +00:00
|
|
|
|
{
|
2014-05-07 02:28:19 +00:00
|
|
|
|
_logger = logManager.GetLogger(GetType().Name);
|
2014-05-05 04:36:45 +00:00
|
|
|
|
_httpClient = httpClient;
|
2014-05-07 18:38:50 +00:00
|
|
|
|
_config = config;
|
|
|
|
|
_encryption = encryption;
|
|
|
|
|
|
|
|
|
|
_config.ConfigurationUpdating += _config_ConfigurationUpdating;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private const string PasswordHashPrefix = "h:";
|
|
|
|
|
void _config_ConfigurationUpdating(object sender, GenericEventArgs<ServerConfiguration> e)
|
|
|
|
|
{
|
|
|
|
|
var options = e.Argument.SubtitleOptions;
|
|
|
|
|
|
|
|
|
|
if (options != null &&
|
|
|
|
|
!string.IsNullOrWhiteSpace(options.OpenSubtitlesPasswordHash) &&
|
|
|
|
|
!options.OpenSubtitlesPasswordHash.StartsWith(PasswordHashPrefix, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
options.OpenSubtitlesPasswordHash = EncryptPassword(options.OpenSubtitlesPasswordHash);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string EncryptPassword(string password)
|
|
|
|
|
{
|
|
|
|
|
return PasswordHashPrefix + _encryption.EncryptString(password);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string DecryptPassword(string password)
|
|
|
|
|
{
|
|
|
|
|
if (password == null ||
|
|
|
|
|
!password.StartsWith(PasswordHashPrefix, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _encryption.DecryptString(password.Substring(2));
|
2014-05-05 04:36:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return "Open Subtitles"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<SubtitleMediaType> SupportedMediaTypes
|
|
|
|
|
{
|
2014-05-07 18:38:50 +00:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(_config.Configuration.SubtitleOptions.OpenSubtitlesUsername) ||
|
|
|
|
|
string.IsNullOrWhiteSpace(_config.Configuration.SubtitleOptions.OpenSubtitlesPasswordHash))
|
|
|
|
|
{
|
|
|
|
|
return new SubtitleMediaType[] { };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new[] { SubtitleMediaType.Episode, SubtitleMediaType.Movie };
|
|
|
|
|
}
|
2014-05-05 04:36:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-07 02:28:19 +00:00
|
|
|
|
public Task<SubtitleResponse> GetSubtitles(string id, CancellationToken cancellationToken)
|
2014-05-05 04:36:45 +00:00
|
|
|
|
{
|
2014-05-07 02:28:19 +00:00
|
|
|
|
return GetSubtitlesInternal(id, cancellationToken);
|
2014-05-05 04:36:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-07 02:28:19 +00:00
|
|
|
|
private async Task<SubtitleResponse> GetSubtitlesInternal(string id,
|
2014-05-05 14:45:45 +00:00
|
|
|
|
CancellationToken cancellationToken)
|
2014-05-05 04:36:45 +00:00
|
|
|
|
{
|
2014-05-07 02:28:19 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(id))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("id");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var idParts = id.Split(new[] { '-' }, 3);
|
|
|
|
|
|
|
|
|
|
var format = idParts[0];
|
|
|
|
|
var language = idParts[1];
|
|
|
|
|
var ossId = idParts[2];
|
|
|
|
|
|
|
|
|
|
var downloadsList = new[] { int.Parse(ossId, _usCulture) };
|
|
|
|
|
|
2014-05-07 18:38:50 +00:00
|
|
|
|
await Login(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var resultDownLoad = await OpenSubtitles.DownloadSubtitlesAsync(downloadsList, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
2014-05-07 02:28:19 +00:00
|
|
|
|
if (!(resultDownLoad is MethodResponseSubtitleDownload))
|
|
|
|
|
{
|
|
|
|
|
throw new ApplicationException("Invalid response type");
|
|
|
|
|
}
|
2014-05-05 04:36:45 +00:00
|
|
|
|
|
2014-05-07 02:28:19 +00:00
|
|
|
|
var res = ((MethodResponseSubtitleDownload)resultDownLoad).Results.First();
|
|
|
|
|
var data = Convert.FromBase64String(res.Data);
|
|
|
|
|
|
|
|
|
|
return new SubtitleResponse
|
|
|
|
|
{
|
|
|
|
|
Format = format,
|
|
|
|
|
Language = language,
|
|
|
|
|
|
|
|
|
|
Stream = new MemoryStream(Utilities.Decompress(new MemoryStream(data)))
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-07 20:16:57 +00:00
|
|
|
|
private DateTime _lastLogin;
|
2014-05-07 18:38:50 +00:00
|
|
|
|
private async Task Login(CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-05-07 20:16:57 +00:00
|
|
|
|
if ((DateTime.UtcNow - _lastLogin).TotalSeconds < 60)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-07 18:38:50 +00:00
|
|
|
|
var options = _config.Configuration.SubtitleOptions ?? new SubtitleOptions();
|
|
|
|
|
|
|
|
|
|
var user = options.OpenSubtitlesUsername ?? string.Empty;
|
|
|
|
|
var password = DecryptPassword(options.OpenSubtitlesPasswordHash);
|
|
|
|
|
|
|
|
|
|
var loginResponse = await OpenSubtitles.LogInAsync(user, password, "en", cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (!(loginResponse is MethodResponseLogIn))
|
|
|
|
|
{
|
|
|
|
|
throw new UnauthorizedAccessException("Authentication to OpenSubtitles failed.");
|
|
|
|
|
}
|
2014-05-07 20:16:57 +00:00
|
|
|
|
|
|
|
|
|
_lastLogin = DateTime.UtcNow;
|
2014-05-07 18:38:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-07 02:28:19 +00:00
|
|
|
|
public async Task<IEnumerable<RemoteSubtitleInfo>> SearchSubtitles(SubtitleSearchRequest request, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-05-05 14:45:45 +00:00
|
|
|
|
var imdbIdText = request.GetProviderId(MetadataProviders.Imdb);
|
|
|
|
|
long imdbId;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(imdbIdText) ||
|
2014-05-07 02:28:19 +00:00
|
|
|
|
!long.TryParse(imdbIdText.TrimStart('t'), NumberStyles.Any, _usCulture, out imdbId))
|
2014-05-05 14:45:45 +00:00
|
|
|
|
{
|
2014-05-07 02:28:19 +00:00
|
|
|
|
_logger.Debug("Imdb id missing");
|
|
|
|
|
return new List<RemoteSubtitleInfo>();
|
2014-05-05 14:45:45 +00:00
|
|
|
|
}
|
2014-05-07 02:28:19 +00:00
|
|
|
|
|
2014-05-05 07:36:26 +00:00
|
|
|
|
switch (request.ContentType)
|
2014-05-05 04:36:45 +00:00
|
|
|
|
{
|
2014-05-05 07:36:26 +00:00
|
|
|
|
case SubtitleMediaType.Episode:
|
|
|
|
|
if (!request.IndexNumber.HasValue || !request.ParentIndexNumber.HasValue || string.IsNullOrEmpty(request.SeriesName))
|
|
|
|
|
{
|
2014-05-07 02:28:19 +00:00
|
|
|
|
_logger.Debug("Episode information missing");
|
|
|
|
|
return new List<RemoteSubtitleInfo>();
|
2014-05-05 07:36:26 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case SubtitleMediaType.Movie:
|
|
|
|
|
if (string.IsNullOrEmpty(request.Name))
|
|
|
|
|
{
|
2014-05-07 02:28:19 +00:00
|
|
|
|
_logger.Debug("Movie name missing");
|
|
|
|
|
return new List<RemoteSubtitleInfo>();
|
2014-05-05 07:36:26 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
2014-05-05 04:36:45 +00:00
|
|
|
|
}
|
2014-05-05 07:36:26 +00:00
|
|
|
|
|
2014-05-05 04:36:45 +00:00
|
|
|
|
if (string.IsNullOrEmpty(request.MediaPath))
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Path Missing");
|
2014-05-07 02:28:19 +00:00
|
|
|
|
return new List<RemoteSubtitleInfo>();
|
2014-05-05 04:36:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utilities.HttpClient = _httpClient;
|
|
|
|
|
OpenSubtitles.SetUserAgent("OS Test User Agent");
|
2014-05-07 02:28:19 +00:00
|
|
|
|
|
2014-05-07 18:38:50 +00:00
|
|
|
|
await Login(cancellationToken).ConfigureAwait(false);
|
2014-05-05 04:36:45 +00:00
|
|
|
|
|
|
|
|
|
var subLanguageId = request.Language;
|
|
|
|
|
var hash = Utilities.ComputeHash(request.MediaPath);
|
|
|
|
|
var fileInfo = new FileInfo(request.MediaPath);
|
|
|
|
|
var movieByteSize = fileInfo.Length;
|
|
|
|
|
|
2014-05-05 07:36:26 +00:00
|
|
|
|
var subtitleSearchParameters = request.ContentType == SubtitleMediaType.Episode
|
|
|
|
|
? new SubtitleSearchParameters(subLanguageId, request.SeriesName, request.ParentIndexNumber.Value.ToString(_usCulture), request.IndexNumber.Value.ToString(_usCulture))
|
|
|
|
|
: new SubtitleSearchParameters(subLanguageId, request.Name);
|
|
|
|
|
|
2014-05-05 04:36:45 +00:00
|
|
|
|
var parms = new List<SubtitleSearchParameters> {
|
|
|
|
|
new SubtitleSearchParameters(subLanguageId, hash, movieByteSize),
|
2014-05-05 07:36:26 +00:00
|
|
|
|
subtitleSearchParameters
|
2014-05-05 04:36:45 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = OpenSubtitles.SearchSubtitles(parms.ToArray());
|
|
|
|
|
if (!(result is MethodResponseSubtitleSearch))
|
|
|
|
|
{
|
2014-05-07 02:28:19 +00:00
|
|
|
|
_logger.Debug("Invalid response type");
|
|
|
|
|
return new List<RemoteSubtitleInfo>();
|
2014-05-05 04:36:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-05 07:36:26 +00:00
|
|
|
|
Predicate<SubtitleSearchResult> mediaFilter =
|
|
|
|
|
x =>
|
|
|
|
|
request.ContentType == SubtitleMediaType.Episode
|
2014-05-07 02:28:19 +00:00
|
|
|
|
? int.Parse(x.SeriesSeason, _usCulture) == request.ParentIndexNumber && int.Parse(x.SeriesEpisode, _usCulture) == request.IndexNumber
|
|
|
|
|
: long.Parse(x.IDMovieImdb, _usCulture) == imdbId;
|
2014-05-05 07:36:26 +00:00
|
|
|
|
|
2014-05-05 04:36:45 +00:00
|
|
|
|
var results = ((MethodResponseSubtitleSearch)result).Results;
|
|
|
|
|
|
2014-05-07 02:28:19 +00:00
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var hasCopy = hash;
|
2014-05-05 04:36:45 +00:00
|
|
|
|
|
2014-05-07 02:28:19 +00:00
|
|
|
|
return results.Where(x => x.SubBad == "0" && mediaFilter(x))
|
|
|
|
|
.OrderBy(x => x.MovieHash == hash)
|
|
|
|
|
.ThenBy(x => Math.Abs(long.Parse(x.MovieByteSize, _usCulture) - movieByteSize))
|
|
|
|
|
.ThenByDescending(x => int.Parse(x.SubDownloadsCnt, _usCulture))
|
|
|
|
|
.ThenByDescending(x => double.Parse(x.SubRating, _usCulture))
|
|
|
|
|
.Select(i => new RemoteSubtitleInfo
|
|
|
|
|
{
|
|
|
|
|
Author = i.UserNickName,
|
|
|
|
|
Comment = i.SubAuthorComment,
|
|
|
|
|
CommunityRating = float.Parse(i.SubRating, _usCulture),
|
|
|
|
|
DownloadCount = int.Parse(i.SubDownloadsCnt, _usCulture),
|
|
|
|
|
Format = i.SubFormat,
|
|
|
|
|
ProviderName = Name,
|
|
|
|
|
Language = i.SubLanguageID,
|
|
|
|
|
|
|
|
|
|
Id = i.SubFormat + "-" + i.SubLanguageID + "-" + i.IDSubtitle,
|
|
|
|
|
|
|
|
|
|
Name = i.SubFileName,
|
|
|
|
|
DateCreated = DateTime.Parse(i.SubAddDate, _usCulture),
|
|
|
|
|
IsHashMatch = i.MovieHash == hasCopy
|
|
|
|
|
});
|
2014-05-05 04:36:45 +00:00
|
|
|
|
}
|
2014-05-07 18:38:50 +00:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_config.ConfigurationUpdating -= _config_ConfigurationUpdating;
|
|
|
|
|
}
|
2014-05-05 04:36:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|