using System; using System.Data; using System.Net; using System.Net.Http; using System.Threading.Tasks; using Jellyfin.Plugin.TubeArchivistMetadata.Utilities; using Microsoft.Extensions.Logging; using Newtonsoft.Json; namespace Jellyfin.Plugin.TubeArchivistMetadata.TubeArchivist { /// /// Class to interact with TubeArchivist API. /// public class TubeArchivistApi { private ILogger _logger; private HttpClient client; private static TubeArchivistApi _taApiInstance = null!; /// /// Initializes a new instance of the class. /// /// HTTP client to make requests to TubeArchivist API. private TubeArchivistApi(HttpClient httpClient) { if (Plugin.Instance == null) { throw new DataException("Uninitialized plugin!"); } else { _logger = Plugin.Instance.Logger; } client = httpClient; } /// /// Gets the instance of the class. /// /// The TubeArchivistApi instance. public static TubeArchivistApi GetInstance() { if (_taApiInstance == null) { if (Plugin.Instance != null) { _taApiInstance = new TubeArchivistApi(Plugin.Instance.HttpClient); } else { throw new DataException("Uninitialized plugin!"); } } return _taApiInstance; } /// /// Retrieves the given channel information from TubeArchivist. /// /// YouTube channel id. /// A task. public async Task GetChannel(string channelId) { ResponseContainer? channel = null; var channelsEndpoint = "/api/channel/"; var url = new Uri(Utils.SanitizeUrl(Plugin.Instance?.Configuration.TubeArchivistUrl + channelsEndpoint + channelId)); var response = await client.GetAsync(url).ConfigureAwait(true); while (response.StatusCode == HttpStatusCode.Moved) { url = response.Headers.Location; _logger.LogInformation("{Message}", "Received redirect to: " + url); response = await client.GetAsync(url).ConfigureAwait(true); } _logger.LogInformation("{Message}", url + ": " + response.StatusCode); if (response.IsSuccessStatusCode) { string rawData = await response.Content.ReadAsStringAsync().ConfigureAwait(true); channel = JsonConvert.DeserializeObject>(rawData); } return channel?.Data; } /// /// Retrieves the given video information from TubeArchivist. /// /// YouTube video id. /// A task. public async Task GetVideo(string videoId) { ResponseContainer