jellyfin-server/MediaBrowser.Providers/Subtitles/OpenSubtitleDownloader.cs

351 lines
14 KiB
C#
Raw Normal View History

2014-08-10 22:13:17 +00:00
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Extensions;
2014-05-07 02:28:19 +00:00
using MediaBrowser.Common.Net;
2014-05-07 18:38:50 +00:00
using MediaBrowser.Controller.Configuration;
2014-05-11 22:38:10 +00:00
using MediaBrowser.Controller.Providers;
2014-05-07 18:38:50 +00:00
using MediaBrowser.Controller.Security;
2014-05-07 02:28:19 +00:00
using MediaBrowser.Controller.Subtitles;
2015-06-11 21:22:44 +00:00
using MediaBrowser.Model.Dto;
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;
2015-06-11 21:22:44 +00:00
using MediaBrowser.Model.Serialization;
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;
2014-07-11 04:27:46 +00:00
private Timer _dailyTimer;
// This is limited to 200 per day
private int _dailyDownloadCount;
// It's 200 but this will be in-exact so buffer a little
// And the user may restart the server
private const int MaxDownloadsPerDay = 150;
2015-06-11 21:22:44 +00:00
private readonly IJsonSerializer _json;
public OpenSubtitleDownloader(ILogManager logManager, IHttpClient httpClient, IServerConfigurationManager config, IEncryptionManager encryption, IJsonSerializer json)
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;
2015-06-11 21:22:44 +00:00
_json = json;
2014-05-07 18:38:50 +00:00
2014-08-10 22:13:17 +00:00
_config.NamedConfigurationUpdating += _config_NamedConfigurationUpdating;
2014-07-11 04:27:46 +00:00
// Reset the count every 24 hours
_dailyTimer = new Timer(state => _dailyDownloadCount = 0, null, TimeSpan.FromHours(24), TimeSpan.FromHours(24));
2015-06-11 21:22:44 +00:00
Utilities.HttpClient = httpClient;
OpenSubtitles.SetUserAgent("mediabrowser.tv");
2014-05-07 18:38:50 +00:00
}
private const string PasswordHashPrefix = "h:";
2014-08-10 22:13:17 +00:00
void _config_NamedConfigurationUpdating(object sender, ConfigurationUpdateEventArgs e)
2014-05-07 18:38:50 +00:00
{
2014-08-10 22:13:17 +00:00
if (!string.Equals(e.Key, "subtitles", StringComparison.OrdinalIgnoreCase))
{
return;
}
var options = (SubtitleOptions)e.NewConfiguration;
2014-05-07 18:38:50 +00:00
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"; }
}
2014-08-10 22:13:17 +00:00
private SubtitleOptions GetOptions()
{
return _config.GetSubtitleConfiguration();
}
2014-05-11 22:38:10 +00:00
public IEnumerable<VideoContentType> SupportedMediaTypes
2014-05-05 04:36:45 +00:00
{
2014-05-07 18:38:50 +00:00
get
{
2014-08-10 22:13:17 +00:00
var options = GetOptions();
if (string.IsNullOrWhiteSpace(options.OpenSubtitlesUsername) ||
string.IsNullOrWhiteSpace(options.OpenSubtitlesPasswordHash))
2014-05-07 18:38:50 +00:00
{
2014-05-11 22:38:10 +00:00
return new VideoContentType[] { };
2014-05-07 18:38:50 +00:00
}
2014-05-11 22:38:10 +00:00
return new[] { VideoContentType.Episode, VideoContentType.Movie };
2014-05-07 18:38:50 +00:00
}
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-08-10 22:13:17 +00:00
return GetSubtitlesInternal(id, GetOptions(), 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-08-10 22:13:17 +00:00
SubtitleOptions options,
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");
}
2014-07-11 04:27:46 +00:00
if (_dailyDownloadCount >= MaxDownloadsPerDay &&
2014-08-10 22:13:17 +00:00
!options.IsOpenSubtitleVipAccount)
2014-07-11 04:27:46 +00:00
{
throw new InvalidOperationException("Open Subtitle's daily download limit has been exceeded. Please try again tomorrow.");
}
2014-05-07 02:28:19 +00:00
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-08 02:16:11 +00:00
var results = ((MethodResponseSubtitleDownload)resultDownLoad).Results;
if (results.Count == 0)
{
2014-05-08 05:04:39 +00: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-08 02:16:11 +00:00
}
var data = Convert.FromBase64String(results.First().Data);
2014-05-07 02:28:19 +00:00
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-08-10 22:13:17 +00:00
var options = GetOptions();
2014-05-07 18:38:50 +00:00
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))
{
2015-09-26 02:31:13 +00:00
throw new Exception("Authentication to OpenSubtitles failed.");
2014-05-07 18:38:50 +00:00
}
2014-05-07 20:16:57 +00:00
_lastLogin = DateTime.UtcNow;
2014-05-07 18:38:50 +00:00
}
2015-06-11 21:22:44 +00:00
public async Task<IEnumerable<NameIdPair>> GetSupportedLanguages(CancellationToken cancellationToken)
{
await Login(cancellationToken).ConfigureAwait(false);
2015-11-25 17:06:50 +00:00
2015-06-11 21:22:44 +00:00
var result = OpenSubtitles.GetSubLanguages("en");
if (!(result is MethodResponseGetSubLanguages))
{
_logger.Error("Invalid response type");
return new List<NameIdPair>();
}
var results = ((MethodResponseGetSubLanguages)result).Languages;
return results.Select(i => new NameIdPair
{
Name = i.LanguageName,
Id = i.SubLanguageID
});
}
private string NormalizeLanguage(string language)
{
// Problem with Greek subtitle download #1349
if (string.Equals (language, "gre", StringComparison.OrdinalIgnoreCase)) {
return "ell";
}
return language;
}
2014-05-11 22:38:10 +00:00
public async Task<IEnumerable<RemoteSubtitleInfo>> Search(SubtitleSearchRequest request, CancellationToken cancellationToken)
2014-05-07 02:28:19 +00:00
{
2014-05-05 14:45:45 +00:00
var imdbIdText = request.GetProviderId(MetadataProviders.Imdb);
2014-05-09 03:24:58 +00:00
long imdbId = 0;
2014-05-07 02:28:19 +00:00
switch (request.ContentType)
2014-05-05 04:36:45 +00:00
{
2014-05-11 22:38:10 +00:00
case VideoContentType.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>();
}
break;
2014-05-11 22:38:10 +00:00
case VideoContentType.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-09 03:24:58 +00:00
if (string.IsNullOrWhiteSpace(imdbIdText) || !long.TryParse(imdbIdText.TrimStart('t'), NumberStyles.Any, _usCulture, out imdbId))
{
_logger.Debug("Imdb id missing");
return new List<RemoteSubtitleInfo>();
}
break;
2014-05-05 04:36:45 +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
}
2014-05-07 18:38:50 +00:00
await Login(cancellationToken).ConfigureAwait(false);
2014-05-05 04:36:45 +00:00
var subLanguageId = NormalizeLanguage(request.Language);
2014-05-05 04:36:45 +00:00
var hash = Utilities.ComputeHash(request.MediaPath);
var fileInfo = new FileInfo(request.MediaPath);
var movieByteSize = fileInfo.Length;
2014-05-11 22:38:10 +00:00
var searchImdbId = request.ContentType == VideoContentType.Movie ? imdbId.ToString(_usCulture) : "";
var subtitleSearchParameters = request.ContentType == VideoContentType.Episode
2014-05-09 03:24:58 +00: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-05 04:36:45 +00:00
var parms = new List<SubtitleSearchParameters> {
2014-05-09 03:24:58 +00:00
new SubtitleSearchParameters( subLanguageId,
movieHash: hash,
movieByteSize: movieByteSize,
imdbid: searchImdbId ),
2014-05-05 04:36:45 +00:00
};
2014-05-09 03:24:58 +00:00
parms.AddRange(subtitleSearchParameters);
2014-05-09 04:39:43 +00:00
var result = await OpenSubtitles.SearchSubtitlesAsync(parms.ToArray(), cancellationToken).ConfigureAwait(false);
2014-05-05 04:36:45 +00:00
if (!(result is MethodResponseSubtitleSearch))
{
2015-06-11 21:22:44 +00:00
_logger.Error("Invalid response type");
2014-05-07 02:28:19 +00:00
return new List<RemoteSubtitleInfo>();
2014-05-05 04:36:45 +00:00
}
Predicate<SubtitleSearchResult> mediaFilter =
x =>
2014-05-11 22:38:10 +00:00
request.ContentType == VideoContentType.Episode
2014-05-10 18:06:29 +00:00
? !string.IsNullOrEmpty(x.SeriesSeason) && !string.IsNullOrEmpty(x.SeriesEpisode) &&
int.Parse(x.SeriesSeason, _usCulture) == request.ParentIndexNumber &&
int.Parse(x.SeriesEpisode, _usCulture) == request.IndexNumber
: !string.IsNullOrEmpty(x.IDMovieImdb) && long.Parse(x.IDMovieImdb, _usCulture) == imdbId;
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))
2014-07-15 19:57:27 +00:00
.OrderBy(x => (x.MovieHash == hash ? 0 : 1))
2014-05-07 02:28:19 +00:00
.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,
2014-05-11 23:02:28 +00:00
ThreeLetterISOLanguageName = i.SubLanguageID,
2014-05-07 02:28:19 +00:00
2014-05-08 05:04:39 +00:00
Id = i.SubFormat + "-" + i.SubLanguageID + "-" + i.IDSubtitleFile,
2014-05-07 02:28:19 +00:00
Name = i.SubFileName,
DateCreated = DateTime.Parse(i.SubAddDate, _usCulture),
IsHashMatch = i.MovieHash == hasCopy
2015-11-25 17:06:50 +00:00
}).Where(i => !string.Equals(i.Format, "sub", StringComparison.OrdinalIgnoreCase) && !string.Equals(i.Format, "idx", StringComparison.OrdinalIgnoreCase));
2014-05-05 04:36:45 +00:00
}
2014-05-07 18:38:50 +00:00
public void Dispose()
{
2014-08-10 22:13:17 +00:00
_config.NamedConfigurationUpdating -= _config_NamedConfigurationUpdating;
2014-07-11 04:27:46 +00:00
if (_dailyTimer != null)
{
_dailyTimer.Dispose();
_dailyTimer = null;
}
2014-05-07 18:38:50 +00:00
}
2014-05-05 04:36:45 +00:00
}
}