2014-05-08 13:09:53 -07:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2014-05-06 19:28:19 -07:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2014-05-07 11:38:50 -07:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.Security;
|
2014-05-06 19:28:19 -07:00
|
|
|
|
using MediaBrowser.Controller.Subtitles;
|
2014-05-07 11:38:50 -07:00
|
|
|
|
using MediaBrowser.Model.Configuration;
|
2014-05-05 07:45:45 -07:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2014-05-08 13:09:53 -07:00
|
|
|
|
using MediaBrowser.Model.Events;
|
2014-05-04 21:36:45 -07:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2014-05-06 19:28:19 -07:00
|
|
|
|
using MediaBrowser.Model.Providers;
|
2014-05-04 21:36:45 -07: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 11:38:50 -07:00
|
|
|
|
public class OpenSubtitleDownloader : ISubtitleProvider, IDisposable
|
2014-05-04 21:36:45 -07:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
|
|
|
|
|
2014-05-07 11:38:50 -07:00
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
|
private readonly IEncryptionManager _encryption;
|
|
|
|
|
|
|
|
|
|
public OpenSubtitleDownloader(ILogManager logManager, IHttpClient httpClient, IServerConfigurationManager config, IEncryptionManager encryption)
|
2014-05-04 21:36:45 -07:00
|
|
|
|
{
|
2014-05-06 19:28:19 -07:00
|
|
|
|
_logger = logManager.GetLogger(GetType().Name);
|
2014-05-04 21:36:45 -07:00
|
|
|
|
_httpClient = httpClient;
|
2014-05-07 11:38:50 -07: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-04 21:36:45 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return "Open Subtitles"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<SubtitleMediaType> SupportedMediaTypes
|
|
|
|
|
{
|
2014-05-07 11:38:50 -07: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-04 21:36:45 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-06 19:28:19 -07:00
|
|
|
|
public Task<SubtitleResponse> GetSubtitles(string id, CancellationToken cancellationToken)
|
2014-05-04 21:36:45 -07:00
|
|
|
|
{
|
2014-05-06 19:28:19 -07:00
|
|
|
|
return GetSubtitlesInternal(id, cancellationToken);
|
2014-05-04 21:36:45 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-06 19:28:19 -07:00
|
|
|
|
private async Task<SubtitleResponse> GetSubtitlesInternal(string id,
|
2014-05-05 07:45:45 -07:00
|
|
|
|
CancellationToken cancellationToken)
|
2014-05-04 21:36:45 -07:00
|
|
|
|
{
|
2014-05-06 19:28:19 -07: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 11:38:50 -07:00
|
|
|
|
await Login(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var resultDownLoad = await OpenSubtitles.DownloadSubtitlesAsync(downloadsList, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
2014-05-06 19:28:19 -07:00
|
|
|
|
if (!(resultDownLoad is MethodResponseSubtitleDownload))
|
|
|
|
|
{
|
|
|
|
|
throw new ApplicationException("Invalid response type");
|
|
|
|
|
}
|
2014-05-04 21:36:45 -07:00
|
|
|
|
|
2014-05-07 19:16:11 -07:00
|
|
|
|
var results = ((MethodResponseSubtitleDownload)resultDownLoad).Results;
|
|
|
|
|
|
|
|
|
|
if (results.Count == 0)
|
|
|
|
|
{
|
2014-05-07 22:04:39 -07:00
|
|
|
|
var msg = string.Format("Subtitle with Id {0} was not found. Name: {1}. Status: {2}. Message: {3}",
|
|
|
|
|
ossId,
|
|
|
|
|
resultDownLoad.Name ?? string.Empty,
|
|
|
|
|
resultDownLoad.Message ?? string.Empty,
|
|
|
|
|
resultDownLoad.Status ?? string.Empty);
|
|
|
|
|
|
|
|
|
|
throw new ResourceNotFoundException(msg);
|
2014-05-07 19:16:11 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var data = Convert.FromBase64String(results.First().Data);
|
2014-05-06 19:28:19 -07:00
|
|
|
|
|
|
|
|
|
return new SubtitleResponse
|
|
|
|
|
{
|
|
|
|
|
Format = format,
|
|
|
|
|
Language = language,
|
|
|
|
|
|
|
|
|
|
Stream = new MemoryStream(Utilities.Decompress(new MemoryStream(data)))
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-07 13:16:57 -07:00
|
|
|
|
private DateTime _lastLogin;
|
2014-05-07 11:38:50 -07:00
|
|
|
|
private async Task Login(CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-05-07 13:16:57 -07:00
|
|
|
|
if ((DateTime.UtcNow - _lastLogin).TotalSeconds < 60)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-07 11:38:50 -07: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 13:16:57 -07:00
|
|
|
|
|
|
|
|
|
_lastLogin = DateTime.UtcNow;
|
2014-05-07 11:38:50 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-06 19:28:19 -07:00
|
|
|
|
public async Task<IEnumerable<RemoteSubtitleInfo>> SearchSubtitles(SubtitleSearchRequest request, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-05-05 07:45:45 -07:00
|
|
|
|
var imdbIdText = request.GetProviderId(MetadataProviders.Imdb);
|
2014-05-08 20:24:58 -07:00
|
|
|
|
long imdbId = 0;
|
2014-05-06 19:28:19 -07:00
|
|
|
|
|
2014-05-05 00:36:26 -07:00
|
|
|
|
switch (request.ContentType)
|
2014-05-04 21:36:45 -07:00
|
|
|
|
{
|
2014-05-05 00:36:26 -07:00
|
|
|
|
case SubtitleMediaType.Episode:
|
|
|
|
|
if (!request.IndexNumber.HasValue || !request.ParentIndexNumber.HasValue || string.IsNullOrEmpty(request.SeriesName))
|
|
|
|
|
{
|
2014-05-06 19:28:19 -07:00
|
|
|
|
_logger.Debug("Episode information missing");
|
|
|
|
|
return new List<RemoteSubtitleInfo>();
|
2014-05-05 00:36:26 -07:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case SubtitleMediaType.Movie:
|
|
|
|
|
if (string.IsNullOrEmpty(request.Name))
|
|
|
|
|
{
|
2014-05-06 19:28:19 -07:00
|
|
|
|
_logger.Debug("Movie name missing");
|
|
|
|
|
return new List<RemoteSubtitleInfo>();
|
2014-05-05 00:36:26 -07:00
|
|
|
|
}
|
2014-05-08 20:24:58 -07:00
|
|
|
|
if (string.IsNullOrWhiteSpace(imdbIdText) || !long.TryParse(imdbIdText.TrimStart('t'), NumberStyles.Any, _usCulture, out imdbId))
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Imdb id missing");
|
|
|
|
|
return new List<RemoteSubtitleInfo>();
|
|
|
|
|
}
|
2014-05-05 00:36:26 -07:00
|
|
|
|
break;
|
2014-05-04 21:36:45 -07:00
|
|
|
|
}
|
2014-05-05 00:36:26 -07:00
|
|
|
|
|
2014-05-04 21:36:45 -07:00
|
|
|
|
if (string.IsNullOrEmpty(request.MediaPath))
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Path Missing");
|
2014-05-06 19:28:19 -07:00
|
|
|
|
return new List<RemoteSubtitleInfo>();
|
2014-05-04 21:36:45 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utilities.HttpClient = _httpClient;
|
|
|
|
|
OpenSubtitles.SetUserAgent("OS Test User Agent");
|
2014-05-06 19:28:19 -07:00
|
|
|
|
|
2014-05-07 11:38:50 -07:00
|
|
|
|
await Login(cancellationToken).ConfigureAwait(false);
|
2014-05-04 21:36:45 -07:00
|
|
|
|
|
|
|
|
|
var subLanguageId = request.Language;
|
|
|
|
|
var hash = Utilities.ComputeHash(request.MediaPath);
|
|
|
|
|
var fileInfo = new FileInfo(request.MediaPath);
|
|
|
|
|
var movieByteSize = fileInfo.Length;
|
2014-05-08 20:24:58 -07:00
|
|
|
|
var searchImdbId = request.ContentType == SubtitleMediaType.Movie ? imdbId.ToString(_usCulture) : "";
|
2014-05-05 00:36:26 -07:00
|
|
|
|
var subtitleSearchParameters = request.ContentType == SubtitleMediaType.Episode
|
2014-05-08 20:24:58 -07:00
|
|
|
|
? new List<SubtitleSearchParameters> {
|
|
|
|
|
new SubtitleSearchParameters(subLanguageId,
|
|
|
|
|
query: request.SeriesName,
|
|
|
|
|
season: request.ParentIndexNumber.Value.ToString(_usCulture),
|
|
|
|
|
episode: request.IndexNumber.Value.ToString(_usCulture))
|
|
|
|
|
}
|
|
|
|
|
: new List<SubtitleSearchParameters> {
|
|
|
|
|
new SubtitleSearchParameters(subLanguageId, imdbid: searchImdbId),
|
|
|
|
|
new SubtitleSearchParameters(subLanguageId, query: request.Name, imdbid: searchImdbId)
|
|
|
|
|
};
|
2014-05-04 21:36:45 -07:00
|
|
|
|
var parms = new List<SubtitleSearchParameters> {
|
2014-05-08 20:24:58 -07:00
|
|
|
|
new SubtitleSearchParameters( subLanguageId,
|
|
|
|
|
movieHash: hash,
|
|
|
|
|
movieByteSize: movieByteSize,
|
|
|
|
|
imdbid: searchImdbId ),
|
2014-05-04 21:36:45 -07:00
|
|
|
|
};
|
2014-05-08 20:24:58 -07:00
|
|
|
|
parms.AddRange(subtitleSearchParameters);
|
2014-05-08 21:39:43 -07:00
|
|
|
|
var result = await OpenSubtitles.SearchSubtitlesAsync(parms.ToArray(), cancellationToken).ConfigureAwait(false);
|
2014-05-04 21:36:45 -07:00
|
|
|
|
if (!(result is MethodResponseSubtitleSearch))
|
|
|
|
|
{
|
2014-05-06 19:28:19 -07:00
|
|
|
|
_logger.Debug("Invalid response type");
|
|
|
|
|
return new List<RemoteSubtitleInfo>();
|
2014-05-04 21:36:45 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-05 00:36:26 -07:00
|
|
|
|
Predicate<SubtitleSearchResult> mediaFilter =
|
|
|
|
|
x =>
|
|
|
|
|
request.ContentType == SubtitleMediaType.Episode
|
2014-05-06 19:28:19 -07:00
|
|
|
|
? int.Parse(x.SeriesSeason, _usCulture) == request.ParentIndexNumber && int.Parse(x.SeriesEpisode, _usCulture) == request.IndexNumber
|
|
|
|
|
: long.Parse(x.IDMovieImdb, _usCulture) == imdbId;
|
2014-05-05 00:36:26 -07:00
|
|
|
|
|
2014-05-04 21:36:45 -07:00
|
|
|
|
var results = ((MethodResponseSubtitleSearch)result).Results;
|
|
|
|
|
|
2014-05-06 19:28:19 -07:00
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var hasCopy = hash;
|
2014-05-04 21:36:45 -07:00
|
|
|
|
|
2014-05-06 19:28:19 -07: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,
|
|
|
|
|
|
2014-05-07 22:04:39 -07:00
|
|
|
|
Id = i.SubFormat + "-" + i.SubLanguageID + "-" + i.IDSubtitleFile,
|
2014-05-06 19:28:19 -07:00
|
|
|
|
|
|
|
|
|
Name = i.SubFileName,
|
|
|
|
|
DateCreated = DateTime.Parse(i.SubAddDate, _usCulture),
|
|
|
|
|
IsHashMatch = i.MovieHash == hasCopy
|
|
|
|
|
});
|
2014-05-04 21:36:45 -07:00
|
|
|
|
}
|
2014-05-07 11:38:50 -07:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_config.ConfigurationUpdating -= _config_ConfigurationUpdating;
|
|
|
|
|
}
|
2014-05-04 21:36:45 -07:00
|
|
|
|
}
|
|
|
|
|
}
|