mirror of
https://github.com/jellyfin/jellyfin-plugin-discogs.git
synced 2024-11-23 06:09:43 +00:00
Somewhat working now
This commit is contained in:
parent
eccb8442e9
commit
615edc8092
61
Jellyfin.Plugin.Discogs/DiscogsApi.cs
Normal file
61
Jellyfin.Plugin.Discogs/DiscogsApi.cs
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Net.Http.Json;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Web;
|
||||||
|
using Jellyfin.Plugin.Discogs.Configuration;
|
||||||
|
using MediaBrowser.Common.Net;
|
||||||
|
using Microsoft.AspNetCore.WebUtilities;
|
||||||
|
|
||||||
|
namespace Jellyfin.Plugin.Discogs;
|
||||||
|
|
||||||
|
#pragma warning disable CS1591
|
||||||
|
public class DiscogsApi
|
||||||
|
{
|
||||||
|
private const string Server = "https://api.discogs.com/";
|
||||||
|
private readonly HttpClient _client;
|
||||||
|
|
||||||
|
public DiscogsApi(IHttpClientFactory clientFactory) : this(clientFactory, Plugin.Instance!.Configuration)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public DiscogsApi(IHttpClientFactory clientFactory, PluginConfiguration configuration)
|
||||||
|
{
|
||||||
|
_client = clientFactory.CreateClient(NamedClient.Default);
|
||||||
|
|
||||||
|
// TODO: This doesn't update the token when configuration changes
|
||||||
|
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Discogs", $"token={configuration.ApiToken}");
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<JsonNode?> GetArtist(string id, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var uri = new Uri($"{Server}artists/{HttpUtility.UrlEncode(id)}");
|
||||||
|
var response = await _client.GetAsync(uri, cancellationToken).ConfigureAwait(false);
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
return await response.Content.ReadFromJsonAsync<JsonNode>(cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<JsonNode?> Search(string query, string? type, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var uri = new Uri(QueryHelpers.AddQueryString($"{Server}database/search", new Dictionary<string, string?> { { "q", query }, { "type", type } }));
|
||||||
|
var response = await _client.GetAsync(uri, cancellationToken).ConfigureAwait(false);
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
return await response.Content.ReadFromJsonAsync<JsonNode>(cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<HttpResponseMessage> GetImage(string url, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
if (!url.StartsWith(Server, StringComparison.Ordinal))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"URL does not start with {Server}", nameof(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
var response = await _client.GetAsync(url, cancellationToken).ConfigureAwait(false);
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
@ -11,7 +11,6 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="DiscogsApiClient" Version="4.0.0" />
|
|
||||||
<PackageReference Include="Jellyfin.Controller" Version="10.8.13" />
|
<PackageReference Include="Jellyfin.Controller" Version="10.8.13" />
|
||||||
<PackageReference Include="Jellyfin.Model" Version="10.8.13" />
|
<PackageReference Include="Jellyfin.Model" Version="10.8.13" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
using DiscogsApiClient;
|
using MediaBrowser.Common.Plugins;
|
||||||
using MediaBrowser.Common.Plugins;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace Jellyfin.Plugin.Discogs;
|
namespace Jellyfin.Plugin.Discogs;
|
||||||
@ -10,10 +9,6 @@ public class PluginServiceRegistrator : IPluginServiceRegistrator
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void RegisterServices(IServiceCollection serviceCollection)
|
public void RegisterServices(IServiceCollection serviceCollection)
|
||||||
{
|
{
|
||||||
serviceCollection.AddDiscogsApiClient(options =>
|
serviceCollection.AddSingleton<DiscogsApi>();
|
||||||
{
|
|
||||||
// TODO: Add jellyfin & plugin version
|
|
||||||
options.UserAgent = "Jellyfin/1.0.0";
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,8 @@
|
|||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Globalization;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using DiscogsApiClient;
|
|
||||||
using DiscogsApiClient.Authentication;
|
|
||||||
using DiscogsApiClient.QueryParameters;
|
|
||||||
using Jellyfin.Extensions;
|
|
||||||
using Jellyfin.Plugin.Discogs.Configuration;
|
|
||||||
using Jellyfin.Plugin.Discogs.ExternalIds;
|
using Jellyfin.Plugin.Discogs.ExternalIds;
|
||||||
using MediaBrowser.Controller.Entities.Audio;
|
using MediaBrowser.Controller.Entities.Audio;
|
||||||
using MediaBrowser.Controller.Providers;
|
using MediaBrowser.Controller.Providers;
|
||||||
@ -23,21 +16,15 @@ namespace Jellyfin.Plugin.Discogs.Providers;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class DiscogsArtistProvider : IRemoteMetadataProvider<MusicArtist, ArtistInfo>
|
public class DiscogsArtistProvider : IRemoteMetadataProvider<MusicArtist, ArtistInfo>
|
||||||
{
|
{
|
||||||
private readonly IDiscogsApiClient _discogsApiClient;
|
private readonly DiscogsApi _api;
|
||||||
private readonly IDiscogsAuthenticationService _discogsAuthenticationService;
|
|
||||||
private readonly PluginConfiguration _configuration;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="DiscogsArtistProvider"/> class.
|
/// Initializes a new instance of the <see cref="DiscogsArtistProvider"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="discogsApiClient">The discogsApiClient.</param>
|
/// <param name="api">The Discogs API.</param>
|
||||||
/// <param name="discogsAuthenticationService">The discogsAuthenticationService.</param>
|
public DiscogsArtistProvider(DiscogsApi api)
|
||||||
/// <param name="configuration">The configuration.</param>
|
|
||||||
public DiscogsArtistProvider(IDiscogsApiClient discogsApiClient, IDiscogsAuthenticationService discogsAuthenticationService, PluginConfiguration configuration)
|
|
||||||
{
|
{
|
||||||
_discogsApiClient = discogsApiClient;
|
_api = api;
|
||||||
_discogsAuthenticationService = discogsAuthenticationService;
|
|
||||||
_configuration = configuration;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
@ -46,18 +33,16 @@ public class DiscogsArtistProvider : IRemoteMetadataProvider<MusicArtist, Artist
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ArtistInfo searchInfo, CancellationToken cancellationToken)
|
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ArtistInfo searchInfo, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
_discogsAuthenticationService.AuthenticateWithPersonalAccessToken(_configuration.ApiToken);
|
|
||||||
|
|
||||||
var artistId = searchInfo.GetProviderId(DiscogsArtistExternalId.ProviderKey);
|
var artistId = searchInfo.GetProviderId(DiscogsArtistExternalId.ProviderKey);
|
||||||
if (artistId != null && int.TryParse(artistId, out var artistIdInt))
|
if (artistId != null)
|
||||||
{
|
{
|
||||||
var result = await _discogsApiClient.GetArtist(artistIdInt, cancellationToken).ConfigureAwait(false);
|
var result = await _api.GetArtist(artistId, cancellationToken).ConfigureAwait(false);
|
||||||
return new[] { new RemoteSearchResult { ProviderIds = new Dictionary<string, string> { { DiscogsArtistExternalId.ProviderKey, result.Id.ToString(CultureInfo.InvariantCulture) }, }, Name = result.Name, ImageUrl = result.Images.FirstOrDefault()?.ImageUri150 } };
|
return new[] { new RemoteSearchResult { ProviderIds = new Dictionary<string, string> { { DiscogsArtistExternalId.ProviderKey, result!["id"]!.ToString() }, }, Name = result!["name"]!.ToString(), ImageUrl = result!["images"]!.AsArray().FirstOrDefault()?["uri150"]?.ToString() } };
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var response = await _discogsApiClient.SearchDatabase(new SearchQueryParameters { Query = searchInfo.Name, Type = "artist", }, cancellationToken: cancellationToken).ConfigureAwait(false);
|
var response = await _api.Search(searchInfo.Name, "artist", cancellationToken).ConfigureAwait(false);
|
||||||
return response.Results.Select(result => new RemoteSearchResult { ProviderIds = new Dictionary<string, string> { { DiscogsArtistExternalId.ProviderKey, result.Id.ToString(CultureInfo.InvariantCulture) }, }, Name = result.Title, ImageUrl = result.CoverImageUrl, });
|
return response!["results"]!.AsArray().Select(result => new RemoteSearchResult { ProviderIds = new Dictionary<string, string> { { DiscogsArtistExternalId.ProviderKey, result!["id"]!.ToString() }, }, Name = result["title"]!.ToString(), ImageUrl = result!["cover_image_url"]?.ToString(), });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,17 +50,16 @@ public class DiscogsArtistProvider : IRemoteMetadataProvider<MusicArtist, Artist
|
|||||||
public async Task<MetadataResult<MusicArtist>> GetMetadata(ArtistInfo info, CancellationToken cancellationToken)
|
public async Task<MetadataResult<MusicArtist>> GetMetadata(ArtistInfo info, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var artistId = info.GetProviderId(DiscogsArtistExternalId.ProviderKey);
|
var artistId = info.GetProviderId(DiscogsArtistExternalId.ProviderKey);
|
||||||
if (artistId != null && int.TryParse(artistId, out var artistIdInt))
|
if (artistId != null)
|
||||||
{
|
{
|
||||||
_discogsAuthenticationService.AuthenticateWithPersonalAccessToken(_configuration.ApiToken);
|
var result = await _api.GetArtist(artistId, cancellationToken).ConfigureAwait(false);
|
||||||
var result = await _discogsApiClient.GetArtist(artistIdInt, cancellationToken).ConfigureAwait(false);
|
|
||||||
|
|
||||||
return new MetadataResult<MusicArtist>
|
return new MetadataResult<MusicArtist>
|
||||||
{
|
{
|
||||||
Item = new MusicArtist { ProviderIds = new Dictionary<string, string>() { { DiscogsArtistExternalId.ProviderKey, result.Id.ToString(CultureInfo.InvariantCulture) }, }, Name = result.Name, Overview = result.Profile, },
|
Item = new MusicArtist { ProviderIds = new Dictionary<string, string> { { DiscogsArtistExternalId.ProviderKey, result!["id"]!.ToString() } }, Name = result!["name"]!.ToString(), Overview = result!["profile"]!.ToString(), },
|
||||||
RemoteImages = result.Images
|
RemoteImages = result!["images"]!.AsArray()
|
||||||
.Where(image => image.Type == DiscogsApiClient.Contract.ImageType.Primary)
|
.Where(image => image!["type"]!.ToString() == "primary")
|
||||||
.Select(image => (image.ImageUri, ImageType.Primary))
|
.Select(image => (image!["uri"]!.ToString(), ImageType.Primary))
|
||||||
.ToList(),
|
.ToList(),
|
||||||
QueriedById = true,
|
QueriedById = true,
|
||||||
HasMetadata = true,
|
HasMetadata = true,
|
||||||
@ -86,8 +70,5 @@ public class DiscogsArtistProvider : IRemoteMetadataProvider<MusicArtist, Artist
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
|
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken) => _api.GetImage(url, cancellationToken);
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user