Merge pull request #75 from MBR-0001/master

This commit is contained in:
Cody Robibero 2021-11-01 17:30:00 -06:00 committed by GitHub
commit a3afa7d2dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 23 deletions

View File

@ -26,7 +26,6 @@ namespace Jellyfin.Plugin.OpenSubtitles
/// </summary> /// </summary>
public class OpenSubtitleDownloader : ISubtitleProvider public class OpenSubtitleDownloader : ISubtitleProvider
{ {
private static readonly CultureInfo _usCulture = CultureInfo.ReadOnly(new CultureInfo("en-US"));
private readonly ILogger<OpenSubtitleDownloader> _logger; private readonly ILogger<OpenSubtitleDownloader> _logger;
private LoginInfo? _login; private LoginInfo? _login;
private DateTime? _limitReset; private DateTime? _limitReset;
@ -81,7 +80,7 @@ namespace Jellyfin.Plugin.OpenSubtitles
throw new AuthenticationException("API key not set up"); throw new AuthenticationException("API key not set up");
} }
long.TryParse(request.GetProviderId(MetadataProvider.Imdb)?.TrimStart('t') ?? string.Empty, NumberStyles.Any, _usCulture, out var imdbId); long.TryParse(request.GetProviderId(MetadataProvider.Imdb)?.TrimStart('t') ?? string.Empty, NumberStyles.Any, CultureInfo.InvariantCulture, out var imdbId);
if (request.ContentType == VideoContentType.Episode && (!request.IndexNumber.HasValue || !request.ParentIndexNumber.HasValue || string.IsNullOrEmpty(request.SeriesName))) if (request.ContentType == VideoContentType.Episode && (!request.IndexNumber.HasValue || !request.ParentIndexNumber.HasValue || string.IsNullOrEmpty(request.SeriesName)))
{ {
@ -116,21 +115,29 @@ namespace Jellyfin.Plugin.OpenSubtitles
{ {
{ "languages", language }, { "languages", language },
{ "moviehash", hash }, { "moviehash", hash },
{ "type", request.ContentType == VideoContentType.Episode ? "episode" : "movie" }, { "type", request.ContentType == VideoContentType.Episode ? "episode" : "movie" }
{ "query", request.ContentType == VideoContentType.Episode ? request.SeriesName : Path.GetFileName(request.MediaPath) }
}; };
// If we have the IMDb ID we use that, otherwise query with the details // If we have the IMDb ID we use that, otherwise query with the details
if (imdbId != 0) if (imdbId != 0)
{ {
options.Add("imdb_id", imdbId.ToString(_usCulture)); options.Add("imdb_id", imdbId.ToString(CultureInfo.InvariantCulture));
} }
else else
{ {
options.Add("query", Path.GetFileName(request.MediaPath));
if (request.ContentType == VideoContentType.Episode) if (request.ContentType == VideoContentType.Episode)
{ {
options.Add("season_number", request.ParentIndexNumber?.ToString(_usCulture) ?? string.Empty); if (request.ParentIndexNumber.HasValue)
options.Add("episode_number", request.IndexNumber?.ToString(_usCulture) ?? string.Empty); {
options.Add("season_number", request.ParentIndexNumber.Value.ToString(CultureInfo.InvariantCulture));
}
if (request.IndexNumber.HasValue)
{
options.Add("episode_number", request.IndexNumber.Value.ToString(CultureInfo.InvariantCulture));
}
} }
} }
@ -231,16 +238,13 @@ namespace Jellyfin.Plugin.OpenSubtitles
var language = idParts[1]; var language = idParts[1];
var ossId = idParts[2]; var ossId = idParts[2];
var fid = int.Parse(ossId, _usCulture); var fid = int.Parse(ossId, CultureInfo.InvariantCulture);
var info = await OpenSubtitlesHandler.OpenSubtitles.GetSubtitleLinkAsync(fid, _login, _apiKey, cancellationToken).ConfigureAwait(false); var info = await OpenSubtitlesHandler.OpenSubtitles.GetSubtitleLinkAsync(fid, _login, _apiKey, cancellationToken).ConfigureAwait(false);
if (info.Data?.Message != null && info.Data.Message.Contains("UTC", StringComparison.Ordinal)) if (info.Data?.ResetTime != null)
{ {
// "Your quota will be renewed in 20 hours and 52 minutes (2021-08-24 12:02:10 UTC) " _limitReset = info.Data.ResetTime;
var str = info.Data.Message.Split('(')[1].Trim().Replace(" UTC)", "Z", StringComparison.Ordinal);
_limitReset = DateTime.Parse(str, _usCulture, DateTimeStyles.AdjustToUniversal);
_logger.LogDebug("Updated expiration time to {ResetTime}", _limitReset); _logger.LogDebug("Updated expiration time to {ResetTime}", _limitReset);
} }
@ -287,7 +291,7 @@ namespace Jellyfin.Plugin.OpenSubtitles
{ {
var msg = string.Format( var msg = string.Format(
CultureInfo.InvariantCulture, CultureInfo.InvariantCulture,
"Failed to obtain download link for file {0}: {1}", "Failed to obtain download link for file {0}: {1} (empty response)",
fid, fid,
info.Code); info.Code);

View File

@ -82,6 +82,6 @@ namespace OpenSubtitlesHandler.Models
/// <summary> /// <summary>
/// Gets a value indicating whether the request was successful. /// Gets a value indicating whether the request was successful.
/// </summary> /// </summary>
public bool Ok => (int)Code < 400; public bool Ok => (int)Code >= 200 && (int)Code <= 299;
} }
} }

View File

@ -1,4 +1,5 @@
using System.Text.Json.Serialization; using System;
using System.Text.Json.Serialization;
namespace OpenSubtitlesHandler.Models.Responses namespace OpenSubtitlesHandler.Models.Responses
{ {
@ -24,5 +25,11 @@ namespace OpenSubtitlesHandler.Models.Responses
/// </summary> /// </summary>
[JsonPropertyName("message")] [JsonPropertyName("message")]
public string? Message { get; set; } public string? Message { get; set; }
/// <summary>
/// Gets or sets the reset time.
/// </summary>
[JsonPropertyName("reset_time_utc")]
public DateTime? ResetTime { get; set; }
} }
} }

View File

@ -120,11 +120,6 @@ namespace OpenSubtitlesHandler
{ {
var opts = System.Web.HttpUtility.ParseQueryString(string.Empty); var opts = System.Web.HttpUtility.ParseQueryString(string.Empty);
foreach (var (key, value) in options.OrderBy(x => x.Key))
{
opts.Add(key, value);
}
var max = -1; var max = -1;
var current = 1; var current = 1;
@ -134,11 +129,21 @@ namespace OpenSubtitlesHandler
do do
{ {
opts.Set("page", current.ToString(CultureInfo.InvariantCulture)); opts.Clear();
if (current > 1)
{
options["page"] = current.ToString(CultureInfo.InvariantCulture);
}
foreach (var (key, value) in options.OrderBy(x => x.Key))
{
opts.Add(key.ToLower(CultureInfo.InvariantCulture), value.ToLower(CultureInfo.InvariantCulture));
}
response = await RequestHandler.SendRequestAsync($"/subtitles?{opts}", HttpMethod.Get, null, null, apiKey, cancellationToken).ConfigureAwait(false); response = await RequestHandler.SendRequestAsync($"/subtitles?{opts}", HttpMethod.Get, null, null, apiKey, cancellationToken).ConfigureAwait(false);
last = new ApiResponse<SearchResult>(response, $"options: {options}", $"page: {current}"); last = new ApiResponse<SearchResult>(response, $"query: {opts}", $"page: {current}");
if (!last.Ok || last.Data == null) if (!last.Ok || last.Data == null)
{ {