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>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="DiscogsApiClient" Version="4.0.0" />
|
||||
<PackageReference Include="Jellyfin.Controller" Version="10.8.13" />
|
||||
<PackageReference Include="Jellyfin.Model" Version="10.8.13" />
|
||||
</ItemGroup>
|
||||
|
@ -1,5 +1,4 @@
|
||||
using DiscogsApiClient;
|
||||
using MediaBrowser.Common.Plugins;
|
||||
using MediaBrowser.Common.Plugins;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Jellyfin.Plugin.Discogs;
|
||||
@ -10,10 +9,6 @@ public class PluginServiceRegistrator : IPluginServiceRegistrator
|
||||
/// <inheritdoc />
|
||||
public void RegisterServices(IServiceCollection serviceCollection)
|
||||
{
|
||||
serviceCollection.AddDiscogsApiClient(options =>
|
||||
{
|
||||
// TODO: Add jellyfin & plugin version
|
||||
options.UserAgent = "Jellyfin/1.0.0";
|
||||
});
|
||||
serviceCollection.AddSingleton<DiscogsApi>();
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
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 MediaBrowser.Controller.Entities.Audio;
|
||||
using MediaBrowser.Controller.Providers;
|
||||
@ -23,21 +16,15 @@ namespace Jellyfin.Plugin.Discogs.Providers;
|
||||
/// </summary>
|
||||
public class DiscogsArtistProvider : IRemoteMetadataProvider<MusicArtist, ArtistInfo>
|
||||
{
|
||||
private readonly IDiscogsApiClient _discogsApiClient;
|
||||
private readonly IDiscogsAuthenticationService _discogsAuthenticationService;
|
||||
private readonly PluginConfiguration _configuration;
|
||||
private readonly DiscogsApi _api;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DiscogsArtistProvider"/> class.
|
||||
/// </summary>
|
||||
/// <param name="discogsApiClient">The discogsApiClient.</param>
|
||||
/// <param name="discogsAuthenticationService">The discogsAuthenticationService.</param>
|
||||
/// <param name="configuration">The configuration.</param>
|
||||
public DiscogsArtistProvider(IDiscogsApiClient discogsApiClient, IDiscogsAuthenticationService discogsAuthenticationService, PluginConfiguration configuration)
|
||||
/// <param name="api">The Discogs API.</param>
|
||||
public DiscogsArtistProvider(DiscogsApi api)
|
||||
{
|
||||
_discogsApiClient = discogsApiClient;
|
||||
_discogsAuthenticationService = discogsAuthenticationService;
|
||||
_configuration = configuration;
|
||||
_api = api;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@ -46,18 +33,16 @@ public class DiscogsArtistProvider : IRemoteMetadataProvider<MusicArtist, Artist
|
||||
/// <inheritdoc />
|
||||
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ArtistInfo searchInfo, CancellationToken cancellationToken)
|
||||
{
|
||||
_discogsAuthenticationService.AuthenticateWithPersonalAccessToken(_configuration.ApiToken);
|
||||
|
||||
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);
|
||||
return new[] { new RemoteSearchResult { ProviderIds = new Dictionary<string, string> { { DiscogsArtistExternalId.ProviderKey, result.Id.ToString(CultureInfo.InvariantCulture) }, }, Name = result.Name, ImageUrl = result.Images.FirstOrDefault()?.ImageUri150 } };
|
||||
var result = await _api.GetArtist(artistId, cancellationToken).ConfigureAwait(false);
|
||||
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
|
||||
{
|
||||
var response = await _discogsApiClient.SearchDatabase(new SearchQueryParameters { Query = searchInfo.Name, Type = "artist", }, cancellationToken: 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, });
|
||||
var response = await _api.Search(searchInfo.Name, "artist", cancellationToken).ConfigureAwait(false);
|
||||
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)
|
||||
{
|
||||
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 _discogsApiClient.GetArtist(artistIdInt, cancellationToken).ConfigureAwait(false);
|
||||
var result = await _api.GetArtist(artistId, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
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, },
|
||||
RemoteImages = result.Images
|
||||
.Where(image => image.Type == DiscogsApiClient.Contract.ImageType.Primary)
|
||||
.Select(image => (image.ImageUri, ImageType.Primary))
|
||||
Item = new MusicArtist { ProviderIds = new Dictionary<string, string> { { DiscogsArtistExternalId.ProviderKey, result!["id"]!.ToString() } }, Name = result!["name"]!.ToString(), Overview = result!["profile"]!.ToString(), },
|
||||
RemoteImages = result!["images"]!.AsArray()
|
||||
.Where(image => image!["type"]!.ToString() == "primary")
|
||||
.Select(image => (image!["uri"]!.ToString(), ImageType.Primary))
|
||||
.ToList(),
|
||||
QueriedById = true,
|
||||
HasMetadata = true,
|
||||
@ -86,8 +70,5 @@ public class DiscogsArtistProvider : IRemoteMetadataProvider<MusicArtist, Artist
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken) => _api.GetImage(url, cancellationToken);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user