2014-05-06 19:28:19 -07:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
2014-05-04 21:36:45 -07:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2014-05-06 19:28:19 -07:00
|
|
|
|
using MediaBrowser.Controller.Subtitles;
|
2014-05-05 07:45:45 -07:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
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
|
|
|
|
|
{
|
|
|
|
|
public class OpenSubtitleDownloader : ISubtitleProvider
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
|
|
|
|
|
2014-05-06 19:28:19 -07:00
|
|
|
|
public OpenSubtitleDownloader(ILogManager logManager, IHttpClient httpClient)
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return "Open Subtitles"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<SubtitleMediaType> SupportedMediaTypes
|
|
|
|
|
{
|
|
|
|
|
get { return new[] { SubtitleMediaType.Episode, SubtitleMediaType.Movie }; }
|
|
|
|
|
}
|
|
|
|
|
|
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) };
|
|
|
|
|
|
|
|
|
|
var resultDownLoad = OpenSubtitles.DownloadSubtitles(downloadsList);
|
|
|
|
|
if (!(resultDownLoad is MethodResponseSubtitleDownload))
|
|
|
|
|
{
|
|
|
|
|
throw new ApplicationException("Invalid response type");
|
|
|
|
|
}
|
2014-05-04 21:36:45 -07:00
|
|
|
|
|
2014-05-06 19:28:19 -07: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)))
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<RemoteSubtitleInfo>> SearchSubtitles(SubtitleSearchRequest request, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-05-05 07:45:45 -07:00
|
|
|
|
var imdbIdText = request.GetProviderId(MetadataProviders.Imdb);
|
|
|
|
|
long imdbId;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(imdbIdText) ||
|
2014-05-06 19:28:19 -07:00
|
|
|
|
!long.TryParse(imdbIdText.TrimStart('t'), NumberStyles.Any, _usCulture, out imdbId))
|
2014-05-05 07:45:45 -07:00
|
|
|
|
{
|
2014-05-06 19:28:19 -07:00
|
|
|
|
_logger.Debug("Imdb id missing");
|
|
|
|
|
return new List<RemoteSubtitleInfo>();
|
2014-05-05 07:45:45 -07:00
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
var loginResponse = await OpenSubtitles.LogInAsync("", "", "en", cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
2014-05-04 21:36:45 -07:00
|
|
|
|
if (!(loginResponse is MethodResponseLogIn))
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Login error");
|
2014-05-06 19:28:19 -07:00
|
|
|
|
return new List<RemoteSubtitleInfo>();
|
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-05 00:36:26 -07: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-04 21:36:45 -07:00
|
|
|
|
var parms = new List<SubtitleSearchParameters> {
|
|
|
|
|
new SubtitleSearchParameters(subLanguageId, hash, movieByteSize),
|
2014-05-05 00:36:26 -07:00
|
|
|
|
subtitleSearchParameters
|
2014-05-04 21:36:45 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = OpenSubtitles.SearchSubtitles(parms.ToArray());
|
|
|
|
|
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,
|
|
|
|
|
|
|
|
|
|
Id = i.SubFormat + "-" + i.SubLanguageID + "-" + i.IDSubtitle,
|
|
|
|
|
|
|
|
|
|
Name = i.SubFileName,
|
|
|
|
|
DateCreated = DateTime.Parse(i.SubAddDate, _usCulture),
|
|
|
|
|
IsHashMatch = i.MovieHash == hasCopy
|
|
|
|
|
});
|
2014-05-04 21:36:45 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|