forked from MediaBrowser/Emby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b27706
commit a291b20
Showing
74 changed files
with
6,372 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace MediaBrowser.Controller.Providers | ||
{ | ||
public interface ISubtitleProvider | ||
{ | ||
/// <summary> | ||
/// Gets the name. | ||
/// </summary> | ||
/// <value>The name.</value> | ||
string Name { get; } | ||
|
||
/// <summary> | ||
/// Gets the supported media types. | ||
/// </summary> | ||
/// <value>The supported media types.</value> | ||
IEnumerable<SubtitleMediaType> SupportedMediaTypes { get; } | ||
|
||
/// <summary> | ||
/// Gets the subtitles. | ||
/// </summary> | ||
/// <param name="request">The request.</param> | ||
/// <param name="cancellationToken">The cancellation token.</param> | ||
/// <returns>Task{SubtitleResponse}.</returns> | ||
Task<SubtitleResponse> GetSubtitles(SubtitleRequest request, CancellationToken cancellationToken); | ||
} | ||
|
||
public enum SubtitleMediaType | ||
{ | ||
Episode = 0, | ||
Movie = 1 | ||
} | ||
|
||
public class SubtitleResponse | ||
{ | ||
public string Format { get; set; } | ||
public bool HasContent { get; set; } | ||
public Stream Stream { get; set; } | ||
} | ||
|
||
public class SubtitleRequest | ||
{ | ||
public string Language { get; set; } | ||
|
||
public SubtitleMediaType ContentType { get; set; } | ||
|
||
public string MediaPath { get; set; } | ||
public string SeriesName { get; set; } | ||
public int? IndexNumber { get; set; } | ||
public int? ParentIndexNumber { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
MediaBrowser.Providers/Subtitles/OpenSubtitleDownloader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
using MediaBrowser.Common.Net; | ||
using MediaBrowser.Controller.Providers; | ||
using MediaBrowser.Model.Logging; | ||
using MediaBrowser.Model.MediaInfo; | ||
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"); | ||
|
||
public OpenSubtitleDownloader(ILogger logger, IHttpClient httpClient) | ||
{ | ||
_logger = logger; | ||
_httpClient = httpClient; | ||
} | ||
|
||
public string Name | ||
{ | ||
get { return "Open Subtitles"; } | ||
} | ||
|
||
public IEnumerable<SubtitleMediaType> SupportedMediaTypes | ||
{ | ||
get { return new[] { SubtitleMediaType.Episode, SubtitleMediaType.Movie }; } | ||
} | ||
|
||
public Task<SubtitleResponse> GetSubtitles(SubtitleRequest request, CancellationToken cancellationToken) | ||
{ | ||
return request.ContentType == SubtitleMediaType.Episode | ||
? GetEpisodeSubtitles(request, cancellationToken) | ||
: GetMovieSubtitles(request, cancellationToken); | ||
} | ||
|
||
public async Task<SubtitleResponse> GetMovieSubtitles(SubtitleRequest request, CancellationToken cancellationToken) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public async Task<SubtitleResponse> GetEpisodeSubtitles(SubtitleRequest request, CancellationToken cancellationToken) | ||
{ | ||
var response = new SubtitleResponse(); | ||
|
||
if (!request.IndexNumber.HasValue || !request.ParentIndexNumber.HasValue) | ||
{ | ||
_logger.Debug("Information Missing"); | ||
return response; | ||
} | ||
if (string.IsNullOrEmpty(request.MediaPath)) | ||
{ | ||
_logger.Debug("Path Missing"); | ||
return response; | ||
} | ||
|
||
Utilities.HttpClient = _httpClient; | ||
OpenSubtitles.SetUserAgent("OS Test User Agent"); | ||
var loginResponse = OpenSubtitles.LogIn("", "", "en"); | ||
if (!(loginResponse is MethodResponseLogIn)) | ||
{ | ||
_logger.Debug("Login error"); | ||
return response; | ||
} | ||
|
||
var subLanguageId = request.Language; | ||
var hash = Utilities.ComputeHash(request.MediaPath); | ||
var fileInfo = new FileInfo(request.MediaPath); | ||
var movieByteSize = fileInfo.Length; | ||
|
||
var parms = new List<SubtitleSearchParameters> { | ||
new SubtitleSearchParameters(subLanguageId, hash, movieByteSize), | ||
new SubtitleSearchParameters(subLanguageId, request.SeriesName, request.ParentIndexNumber.Value.ToString(_usCulture), request.IndexNumber.Value.ToString(_usCulture)), | ||
|
||
}; | ||
|
||
var result = OpenSubtitles.SearchSubtitles(parms.ToArray()); | ||
if (!(result is MethodResponseSubtitleSearch)) | ||
{ | ||
_logger.Debug("invalid response type"); | ||
return null; | ||
} | ||
|
||
var results = ((MethodResponseSubtitleSearch)result).Results; | ||
var bestResult = results.Where(x => x.SubBad == "0" && int.Parse(x.SeriesSeason) == request.ParentIndexNumber && int.Parse(x.SeriesEpisode) == request.IndexNumber) | ||
.OrderBy(x => x.MovieHash == hash) | ||
.ThenBy(x => Math.Abs(long.Parse(x.MovieByteSize) - movieByteSize)) | ||
.ThenByDescending(x => int.Parse(x.SubDownloadsCnt)) | ||
.ThenByDescending(x => double.Parse(x.SubRating)) | ||
.ToList(); | ||
|
||
if (!bestResult.Any()) | ||
{ | ||
_logger.Debug("No Subtitles"); | ||
return response; | ||
} | ||
|
||
_logger.Debug("Found " + bestResult.Count + " subtitles."); | ||
|
||
var subtitle = bestResult.First(); | ||
var downloadsList = new[] { int.Parse(subtitle.IDSubtitleFile) }; | ||
|
||
var resultDownLoad = OpenSubtitles.DownloadSubtitles(downloadsList); | ||
if (!(resultDownLoad is MethodResponseSubtitleDownload)) | ||
{ | ||
_logger.Debug("invalid response type"); | ||
return response; | ||
} | ||
if (!((MethodResponseSubtitleDownload)resultDownLoad).Results.Any()) | ||
{ | ||
_logger.Debug("No Subtitle Downloads"); | ||
return response; | ||
} | ||
|
||
var res = ((MethodResponseSubtitleDownload)resultDownLoad).Results.First(); | ||
var data = Convert.FromBase64String(res.Data); | ||
|
||
response.HasContent = true; | ||
response.Format = SubtitleFormat.SRT; | ||
response.Stream = new MemoryStream(Utilities.Decompress(new MemoryStream(data))); | ||
return response; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.