mirror of
https://github.com/jellyfin/jellyfin-plugin-opensubtitles.git
synced 2024-11-23 06:09:51 +00:00
Merge pull request #76 from MBR-0001/api-key
This commit is contained in:
commit
4be71b5f53
@ -38,7 +38,8 @@ namespace Jellyfin.Plugin.OpenSubtitles.API
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<ActionResult> ValidateLoginInfo([FromBody] LoginInfoInput body)
|
||||
{
|
||||
var response = await OpenSubtitlesHandler.OpenSubtitles.LogInAsync(body.Username, body.Password, body.ApiKey, CancellationToken.None).ConfigureAwait(false);
|
||||
var key = !string.IsNullOrWhiteSpace(body.CustomApiKey) ? body.CustomApiKey : OpenSubtitlesPlugin.ApiKey;
|
||||
var response = await OpenSubtitlesHandler.OpenSubtitles.LogInAsync(body.Username, body.Password, key, CancellationToken.None).ConfigureAwait(false);
|
||||
|
||||
if (!response.Ok)
|
||||
{
|
||||
@ -58,7 +59,7 @@ namespace Jellyfin.Plugin.OpenSubtitles.API
|
||||
|
||||
if (response.Data != null)
|
||||
{
|
||||
await OpenSubtitlesHandler.OpenSubtitles.LogOutAsync(response.Data, body.ApiKey, CancellationToken.None).ConfigureAwait(false);
|
||||
await OpenSubtitlesHandler.OpenSubtitles.LogOutAsync(response.Data, key, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return Ok(new { Downloads = response.Data?.User?.AllowedDownloads ?? 0 });
|
||||
|
@ -18,8 +18,8 @@ namespace Jellyfin.Plugin.OpenSubtitles.Configuration
|
||||
public string Password { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the API Key.
|
||||
/// Gets or sets the custom API Key.
|
||||
/// </summary>
|
||||
public string ApiKey { get; set; } = string.Empty;
|
||||
public string CustomApiKey { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ namespace Jellyfin.Plugin.OpenSubtitles
|
||||
private LoginInfo? _login;
|
||||
private DateTime? _limitReset;
|
||||
private IReadOnlyList<string>? _languages;
|
||||
private string _apiKey;
|
||||
private string _customApiKey;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="OpenSubtitleDownloader"/> class.
|
||||
@ -47,12 +47,23 @@ namespace Jellyfin.Plugin.OpenSubtitles
|
||||
|
||||
OpenSubtitlesPlugin.Instance!.ConfigurationChanged += (_, _) =>
|
||||
{
|
||||
_apiKey = GetOptions().ApiKey;
|
||||
_customApiKey = GetOptions().CustomApiKey;
|
||||
// force a login next time a request is made
|
||||
_login = null;
|
||||
};
|
||||
|
||||
_apiKey = GetOptions().ApiKey;
|
||||
_customApiKey = GetOptions().CustomApiKey;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the API key that will be used for requests.
|
||||
/// </summary>
|
||||
public string ApiKey
|
||||
{
|
||||
get
|
||||
{
|
||||
return !string.IsNullOrWhiteSpace(_customApiKey) ? _customApiKey : OpenSubtitlesPlugin.ApiKey;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@ -75,11 +86,6 @@ namespace Jellyfin.Plugin.OpenSubtitles
|
||||
throw new ArgumentNullException(nameof(request));
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(_apiKey))
|
||||
{
|
||||
throw new AuthenticationException("API key not set up");
|
||||
}
|
||||
|
||||
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)))
|
||||
@ -149,7 +155,7 @@ namespace Jellyfin.Plugin.OpenSubtitles
|
||||
|
||||
_logger.LogDebug("Search query: {Query}", options);
|
||||
|
||||
var searchResponse = await OpenSubtitlesHandler.OpenSubtitles.SearchSubtitlesAsync(options, _apiKey, cancellationToken).ConfigureAwait(false);
|
||||
var searchResponse = await OpenSubtitlesHandler.OpenSubtitles.SearchSubtitlesAsync(options, ApiKey, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (!searchResponse.Ok)
|
||||
{
|
||||
@ -201,11 +207,6 @@ namespace Jellyfin.Plugin.OpenSubtitles
|
||||
throw new ArgumentException("Missing param", nameof(id));
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(_apiKey))
|
||||
{
|
||||
throw new AuthenticationException("API key not set up");
|
||||
}
|
||||
|
||||
if (_login?.User?.RemainingDownloads <= 0)
|
||||
{
|
||||
if (_limitReset < DateTime.UtcNow)
|
||||
@ -241,7 +242,7 @@ namespace Jellyfin.Plugin.OpenSubtitles
|
||||
|
||||
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?.ResetTime != null)
|
||||
{
|
||||
@ -322,11 +323,6 @@ namespace Jellyfin.Plugin.OpenSubtitles
|
||||
return;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(_apiKey))
|
||||
{
|
||||
throw new AuthenticationException("API key is not set up");
|
||||
}
|
||||
|
||||
var options = GetOptions();
|
||||
if (string.IsNullOrWhiteSpace(options.Username) || string.IsNullOrWhiteSpace(options.Password))
|
||||
{
|
||||
@ -336,7 +332,7 @@ namespace Jellyfin.Plugin.OpenSubtitles
|
||||
var loginResponse = await OpenSubtitlesHandler.OpenSubtitles.LogInAsync(
|
||||
options.Username,
|
||||
options.Password,
|
||||
_apiKey,
|
||||
ApiKey,
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (!loginResponse.Ok)
|
||||
@ -359,7 +355,7 @@ namespace Jellyfin.Plugin.OpenSubtitles
|
||||
return;
|
||||
}
|
||||
|
||||
var infoResponse = await OpenSubtitlesHandler.OpenSubtitles.GetUserInfo(_login, _apiKey, cancellationToken).ConfigureAwait(false);
|
||||
var infoResponse = await OpenSubtitlesHandler.OpenSubtitles.GetUserInfo(_login, ApiKey, cancellationToken).ConfigureAwait(false);
|
||||
if (infoResponse.Ok)
|
||||
{
|
||||
_login.User = infoResponse.Data?.Data;
|
||||
@ -380,7 +376,7 @@ namespace Jellyfin.Plugin.OpenSubtitles
|
||||
|
||||
if (_languages == null || _languages.Count == 0)
|
||||
{
|
||||
var res = await OpenSubtitlesHandler.OpenSubtitles.GetLanguageList(_apiKey, cancellationToken).ConfigureAwait(false);
|
||||
var res = await OpenSubtitlesHandler.OpenSubtitles.GetLanguageList(ApiKey, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (!res.Ok || res.Data?.Data == null)
|
||||
{
|
||||
|
@ -13,6 +13,11 @@ namespace Jellyfin.Plugin.OpenSubtitles
|
||||
/// </summary>
|
||||
public class OpenSubtitlesPlugin : BasePlugin<PluginConfiguration>, IHasWebPages
|
||||
{
|
||||
/// <summary>
|
||||
/// Default API key to use when performing an API call.
|
||||
/// </summary>
|
||||
public const string ApiKey = "gUCLWGoAg2PmyseoTM0INFFVPcDCeDlT";
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="OpenSubtitlesPlugin"/> class.
|
||||
/// </summary>
|
||||
|
@ -16,9 +16,9 @@
|
||||
<div class="fieldDescription">You can utilize this plugin by editing a library and modifying the options for subtitle downloads.</div>
|
||||
</div>
|
||||
<div class="inputContainer">
|
||||
<input is="emby-input" type="password" id="apikey" label="${HeaderApiKey}:" />
|
||||
<input is="emby-input" type="password" id="apikey" label="(Optional) ${HeaderApiKey}:" />
|
||||
<div class="fieldDescription">
|
||||
<a is="emby-linkbutton" class="button-link" target="_blank" href="https://www.opensubtitles.com/en/consumers/new">Create API Key</a>
|
||||
You can provide an <a is="emby-linkbutton" class="button-link" target="_blank" href="https://www.opensubtitles.com/en/consumers">API Key</a> for doing API requests, if left empty the default API key will be used.
|
||||
</div>
|
||||
<div class="fieldDescription">
|
||||
<a is="emby-linkbutton" class="button-link" target="_blank" href="https://92500a62-df9e-42ed-82a4-e6b3eeb89365.site.hbuptime.com/">OpenSubtitles API Status</a>
|
||||
|
@ -9,7 +9,7 @@ export default function (view, params) {
|
||||
ApiClient.getPluginConfiguration(OpenSubtitlesConfig.pluginUniqueId).then(function (config) {
|
||||
page.querySelector('#username').value = config.Username || '';
|
||||
page.querySelector('#password').value = config.Password || '';
|
||||
page.querySelector('#apikey').value = config.ApiKey || '';
|
||||
page.querySelector('#apikey').value = config.CustomApiKey || '';
|
||||
Dashboard.hideLoadingMsg();
|
||||
});
|
||||
});
|
||||
@ -19,18 +19,18 @@ export default function (view, params) {
|
||||
Dashboard.showLoadingMsg();
|
||||
const form = this;
|
||||
ApiClient.getPluginConfiguration(OpenSubtitlesConfig.pluginUniqueId).then(function (config) {
|
||||
const username = form.querySelector('#username').value;
|
||||
const password = form.querySelector('#password').value;
|
||||
const apiKey = form.querySelector('#apikey').value;
|
||||
const username = form.querySelector('#username').value.trim();
|
||||
const password = form.querySelector('#password').value.trim();
|
||||
const apiKey = form.querySelector('#apikey').value.trim();
|
||||
|
||||
if (!username || !password || !apiKey) {
|
||||
if (!username || !password) {
|
||||
Dashboard.processErrorResponse({statusText: "Account info is incomplete"});
|
||||
return;
|
||||
}
|
||||
|
||||
const el = form.querySelector('#ossresponse');
|
||||
|
||||
const data = JSON.stringify({ Username: username, Password: password, ApiKey: apiKey });
|
||||
const data = JSON.stringify({ Username: username, Password: password, CustomApiKey: apiKey });
|
||||
const url = ApiClient.getUrl('Jellyfin.Plugin.OpenSubtitles/ValidateLoginInfo');
|
||||
|
||||
const handler = response => response.json().then(res => {
|
||||
@ -39,7 +39,7 @@ export default function (view, params) {
|
||||
|
||||
config.Username = username;
|
||||
config.Password = password;
|
||||
config.ApiKey = apiKey;
|
||||
config.CustomApiKey = apiKey;
|
||||
|
||||
ApiClient.updatePluginConfiguration(OpenSubtitlesConfig.pluginUniqueId, config).then(function (result) {
|
||||
Dashboard.processPluginConfigurationUpdateResult(result);
|
||||
|
@ -20,9 +20,8 @@ namespace OpenSubtitlesHandler.Models
|
||||
public string Password { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the api key.
|
||||
/// Gets or sets the custom api key.
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string ApiKey { get; set; } = null!;
|
||||
public string CustomApiKey { get; set; } = null!;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user