First POC with podcasts library
This commit is contained in:
@@ -0,0 +1,365 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Mime;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Plugin.Jellypod.Api.Models;
|
||||
using Jellyfin.Plugin.Jellypod.Models;
|
||||
using Jellyfin.Plugin.Jellypod.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Plugin.Jellypod.Api;
|
||||
|
||||
/// <summary>
|
||||
/// Controller for Jellypod podcast management.
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
[Route("Jellypod")]
|
||||
[Produces(MediaTypeNames.Application.Json)]
|
||||
public class JellypodController : ControllerBase
|
||||
{
|
||||
private readonly ILogger<JellypodController> _logger;
|
||||
private readonly IRssFeedService _rssFeedService;
|
||||
private readonly IPodcastStorageService _storageService;
|
||||
private readonly IPodcastDownloadService _downloadService;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JellypodController"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">Logger instance.</param>
|
||||
/// <param name="rssFeedService">RSS feed service.</param>
|
||||
/// <param name="storageService">Storage service.</param>
|
||||
/// <param name="downloadService">Download service.</param>
|
||||
public JellypodController(
|
||||
ILogger<JellypodController> logger,
|
||||
IRssFeedService rssFeedService,
|
||||
IPodcastStorageService storageService,
|
||||
IPodcastDownloadService downloadService)
|
||||
{
|
||||
_logger = logger;
|
||||
_rssFeedService = rssFeedService;
|
||||
_storageService = storageService;
|
||||
_downloadService = downloadService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all subscribed podcasts.
|
||||
/// </summary>
|
||||
/// <returns>List of podcasts.</returns>
|
||||
[HttpGet("podcasts")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult<IEnumerable<Podcast>>> GetPodcasts()
|
||||
{
|
||||
var podcasts = await _storageService.GetAllPodcastsAsync().ConfigureAwait(false);
|
||||
return Ok(podcasts);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a specific podcast by ID.
|
||||
/// </summary>
|
||||
/// <param name="id">Podcast ID.</param>
|
||||
/// <returns>The podcast.</returns>
|
||||
[HttpGet("podcasts/{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult<Podcast>> GetPodcast([FromRoute] Guid id)
|
||||
{
|
||||
var podcast = await _storageService.GetPodcastAsync(id).ConfigureAwait(false);
|
||||
return podcast != null ? Ok(podcast) : NotFound();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a new podcast subscription.
|
||||
/// </summary>
|
||||
/// <param name="request">Add podcast request.</param>
|
||||
/// <returns>The created podcast.</returns>
|
||||
[HttpPost("podcasts")]
|
||||
[ProducesResponseType(StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status409Conflict)]
|
||||
public async Task<ActionResult<Podcast>> AddPodcast([FromBody] AddPodcastRequest request)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(request.FeedUrl))
|
||||
{
|
||||
return BadRequest("Feed URL is required");
|
||||
}
|
||||
|
||||
// Check if already subscribed
|
||||
var existingPodcasts = await _storageService.GetAllPodcastsAsync().ConfigureAwait(false);
|
||||
var existing = existingPodcasts.FirstOrDefault(p =>
|
||||
string.Equals(p.FeedUrl, request.FeedUrl, StringComparison.OrdinalIgnoreCase));
|
||||
if (existing != null)
|
||||
{
|
||||
return Conflict($"Already subscribed to this podcast: {existing.Title}");
|
||||
}
|
||||
|
||||
// Fetch and validate the feed
|
||||
var podcast = await _rssFeedService.FetchPodcastAsync(request.FeedUrl).ConfigureAwait(false);
|
||||
if (podcast == null)
|
||||
{
|
||||
return BadRequest("Invalid or inaccessible RSS feed");
|
||||
}
|
||||
|
||||
podcast.AutoDownloadEnabled = request.AutoDownload ?? true;
|
||||
await _storageService.AddPodcastAsync(podcast).ConfigureAwait(false);
|
||||
|
||||
// Download podcast artwork
|
||||
await _downloadService.DownloadPodcastArtworkAsync(podcast).ConfigureAwait(false);
|
||||
|
||||
_logger.LogInformation("Added podcast: {Title} ({Url})", podcast.Title, podcast.FeedUrl);
|
||||
|
||||
return CreatedAtAction(nameof(GetPodcast), new { id = podcast.Id }, podcast);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a podcast subscription.
|
||||
/// </summary>
|
||||
/// <param name="id">Podcast ID.</param>
|
||||
/// <param name="deleteFiles">Whether to delete downloaded files.</param>
|
||||
/// <returns>No content.</returns>
|
||||
[HttpDelete("podcasts/{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> DeletePodcast([FromRoute] Guid id, [FromQuery] bool deleteFiles = false)
|
||||
{
|
||||
var podcast = await _storageService.GetPodcastAsync(id).ConfigureAwait(false);
|
||||
if (podcast == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
if (deleteFiles)
|
||||
{
|
||||
foreach (var episode in podcast.Episodes.Where(e => e.Status == EpisodeStatus.Downloaded))
|
||||
{
|
||||
await _downloadService.DeleteEpisodeFileAsync(episode).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
await _storageService.DeletePodcastAsync(id).ConfigureAwait(false);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates a podcast's settings.
|
||||
/// </summary>
|
||||
/// <param name="id">Podcast ID.</param>
|
||||
/// <param name="request">Update request.</param>
|
||||
/// <returns>The updated podcast.</returns>
|
||||
[HttpPut("podcasts/{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult<Podcast>> UpdatePodcast([FromRoute] Guid id, [FromBody] UpdatePodcastRequest request)
|
||||
{
|
||||
var podcast = await _storageService.GetPodcastAsync(id).ConfigureAwait(false);
|
||||
if (podcast == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
if (request.AutoDownload.HasValue)
|
||||
{
|
||||
podcast.AutoDownloadEnabled = request.AutoDownload.Value;
|
||||
}
|
||||
|
||||
if (request.MaxEpisodesToKeep.HasValue)
|
||||
{
|
||||
podcast.MaxEpisodesToKeep = request.MaxEpisodesToKeep.Value;
|
||||
}
|
||||
|
||||
await _storageService.UpdatePodcastAsync(podcast).ConfigureAwait(false);
|
||||
return Ok(podcast);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Refreshes a podcast feed.
|
||||
/// </summary>
|
||||
/// <param name="id">Podcast ID.</param>
|
||||
/// <returns>The updated podcast.</returns>
|
||||
[HttpPost("podcasts/{id}/refresh")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult<Podcast>> RefreshPodcast([FromRoute] Guid id)
|
||||
{
|
||||
var podcast = await _storageService.GetPodcastAsync(id).ConfigureAwait(false);
|
||||
if (podcast == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var updatedPodcast = await _rssFeedService.FetchPodcastAsync(podcast.FeedUrl).ConfigureAwait(false);
|
||||
if (updatedPodcast == null)
|
||||
{
|
||||
return BadRequest("Failed to fetch podcast feed");
|
||||
}
|
||||
|
||||
// Find new episodes
|
||||
var existingGuids = podcast.Episodes
|
||||
.Select(e => e.EpisodeGuid)
|
||||
.Where(g => !string.IsNullOrEmpty(g))
|
||||
.ToHashSet(StringComparer.Ordinal);
|
||||
|
||||
var newEpisodes = updatedPodcast.Episodes
|
||||
.Where(e => !string.IsNullOrEmpty(e.EpisodeGuid) && !existingGuids.Contains(e.EpisodeGuid))
|
||||
.ToList();
|
||||
|
||||
foreach (var episode in newEpisodes)
|
||||
{
|
||||
episode.PodcastId = podcast.Id;
|
||||
podcast.Episodes.Insert(0, episode);
|
||||
}
|
||||
|
||||
podcast.LastUpdated = DateTime.UtcNow;
|
||||
await _storageService.UpdatePodcastAsync(podcast).ConfigureAwait(false);
|
||||
|
||||
return Ok(podcast);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Downloads multiple episodes from a podcast.
|
||||
/// </summary>
|
||||
/// <param name="id">Podcast ID.</param>
|
||||
/// <param name="request">Download request with count.</param>
|
||||
/// <returns>Number of episodes queued.</returns>
|
||||
[HttpPost("podcasts/{id}/download")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult<DownloadResult>> DownloadEpisodes([FromRoute] Guid id, [FromBody] DownloadEpisodesRequest request)
|
||||
{
|
||||
var podcast = await _storageService.GetPodcastAsync(id).ConfigureAwait(false);
|
||||
if (podcast == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var count = request.Count > 0 ? request.Count : 5;
|
||||
var episodesToDownload = podcast.Episodes
|
||||
.Where(e => e.Status == EpisodeStatus.Available)
|
||||
.OrderByDescending(e => e.PublishedDate)
|
||||
.Take(count)
|
||||
.ToList();
|
||||
|
||||
foreach (var episode in episodesToDownload)
|
||||
{
|
||||
await _downloadService.QueueDownloadAsync(podcast, episode).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
_logger.LogInformation("Queued {Count} episodes for download from {Podcast}", episodesToDownload.Count, podcast.Title);
|
||||
|
||||
return Ok(new DownloadResult { QueuedCount = episodesToDownload.Count });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Downloads a specific episode.
|
||||
/// </summary>
|
||||
/// <param name="podcastId">Podcast ID.</param>
|
||||
/// <param name="episodeId">Episode ID.</param>
|
||||
/// <returns>Accepted status.</returns>
|
||||
[HttpPost("podcasts/{podcastId}/episodes/{episodeId}/download")]
|
||||
[ProducesResponseType(StatusCodes.Status202Accepted)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> DownloadEpisode([FromRoute] Guid podcastId, [FromRoute] Guid episodeId)
|
||||
{
|
||||
var podcast = await _storageService.GetPodcastAsync(podcastId).ConfigureAwait(false);
|
||||
var episode = podcast?.Episodes.FirstOrDefault(e => e.Id == episodeId);
|
||||
|
||||
if (podcast == null || episode == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
await _downloadService.QueueDownloadAsync(podcast, episode).ConfigureAwait(false);
|
||||
return Accepted();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes an episode.
|
||||
/// </summary>
|
||||
/// <param name="podcastId">Podcast ID.</param>
|
||||
/// <param name="episodeId">Episode ID.</param>
|
||||
/// <param name="deleteFile">Whether to delete the downloaded file.</param>
|
||||
/// <returns>No content.</returns>
|
||||
[HttpDelete("podcasts/{podcastId}/episodes/{episodeId}")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> DeleteEpisode(
|
||||
[FromRoute] Guid podcastId,
|
||||
[FromRoute] Guid episodeId,
|
||||
[FromQuery] bool deleteFile = true)
|
||||
{
|
||||
var podcast = await _storageService.GetPodcastAsync(podcastId).ConfigureAwait(false);
|
||||
var episode = podcast?.Episodes.FirstOrDefault(e => e.Id == episodeId);
|
||||
|
||||
if (podcast == null || episode == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
if (deleteFile && episode.Status == EpisodeStatus.Downloaded)
|
||||
{
|
||||
await _downloadService.DeleteEpisodeFileAsync(episode).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
podcast.Episodes.Remove(episode);
|
||||
await _storageService.UpdatePodcastAsync(podcast).ConfigureAwait(false);
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Previews a podcast feed without subscribing.
|
||||
/// </summary>
|
||||
/// <param name="request">Preview request.</param>
|
||||
/// <returns>The podcast info.</returns>
|
||||
[HttpPost("preview")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<ActionResult<Podcast>> PreviewFeed([FromBody] PreviewFeedRequest request)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(request.FeedUrl))
|
||||
{
|
||||
return BadRequest("Feed URL is required");
|
||||
}
|
||||
|
||||
var podcast = await _rssFeedService.FetchPodcastAsync(request.FeedUrl).ConfigureAwait(false);
|
||||
return podcast != null ? Ok(podcast) : BadRequest("Invalid or inaccessible RSS feed");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes duplicate podcast subscriptions, keeping the first one.
|
||||
/// </summary>
|
||||
/// <returns>Number of duplicates removed.</returns>
|
||||
[HttpPost("cleanup")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult<object>> CleanupDuplicates()
|
||||
{
|
||||
var podcasts = await _storageService.GetAllPodcastsAsync().ConfigureAwait(false);
|
||||
var seenUrls = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
var duplicatesToRemove = new List<Guid>();
|
||||
|
||||
foreach (var podcast in podcasts)
|
||||
{
|
||||
if (seenUrls.Contains(podcast.FeedUrl))
|
||||
{
|
||||
duplicatesToRemove.Add(podcast.Id);
|
||||
}
|
||||
else
|
||||
{
|
||||
seenUrls.Add(podcast.FeedUrl);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var id in duplicatesToRemove)
|
||||
{
|
||||
await _storageService.DeletePodcastAsync(id).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
_logger.LogInformation("Removed {Count} duplicate podcast subscriptions", duplicatesToRemove.Count);
|
||||
|
||||
return Ok(new { RemovedCount = duplicatesToRemove.Count });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Jellyfin.Plugin.Jellypod.Api.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Request to add a new podcast.
|
||||
/// </summary>
|
||||
public class AddPodcastRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the RSS feed URL.
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string FeedUrl { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether to enable auto-download.
|
||||
/// </summary>
|
||||
public bool? AutoDownload { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace Jellyfin.Plugin.Jellypod.Api.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Request to download episodes from a podcast.
|
||||
/// </summary>
|
||||
public class DownloadEpisodesRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the number of episodes to download.
|
||||
/// </summary>
|
||||
public int Count { get; set; } = 5;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace Jellyfin.Plugin.Jellypod.Api.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Result of a download request.
|
||||
/// </summary>
|
||||
public class DownloadResult
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the number of episodes queued for download.
|
||||
/// </summary>
|
||||
public int QueuedCount { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Jellyfin.Plugin.Jellypod.Api.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Request to preview a feed.
|
||||
/// </summary>
|
||||
public class PreviewFeedRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the RSS feed URL.
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string FeedUrl { get; set; } = string.Empty;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace Jellyfin.Plugin.Jellypod.Api.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Request to update a podcast.
|
||||
/// </summary>
|
||||
public class UpdatePodcastRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets whether to enable auto-download.
|
||||
/// </summary>
|
||||
public bool? AutoDownload { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the maximum episodes to keep.
|
||||
/// </summary>
|
||||
public int? MaxEpisodesToKeep { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Plugin.Jellypod.Api;
|
||||
|
||||
/// <summary>
|
||||
/// Controller for proxying podcast audio streams.
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("Jellypod/Stream")]
|
||||
public class StreamProxyController : ControllerBase
|
||||
{
|
||||
private readonly ILogger<StreamProxyController> _logger;
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="StreamProxyController"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">Logger instance.</param>
|
||||
/// <param name="httpClientFactory">HTTP client factory.</param>
|
||||
public StreamProxyController(
|
||||
ILogger<StreamProxyController> logger,
|
||||
IHttpClientFactory httpClientFactory)
|
||||
{
|
||||
_logger = logger;
|
||||
_httpClientFactory = httpClientFactory;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Proxies an audio stream from a remote URL.
|
||||
/// </summary>
|
||||
/// <param name="url">Base64-encoded URL to stream.</param>
|
||||
/// <param name="cancellationToken">Cancellation token.</param>
|
||||
/// <returns>The audio stream.</returns>
|
||||
[HttpGet("{url}")]
|
||||
[AllowAnonymous]
|
||||
public async Task<IActionResult> GetStream([FromRoute] string url, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Decode the URL from base64
|
||||
var decodedUrl = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(url));
|
||||
_logger.LogDebug("Proxying audio stream from: {Url}", decodedUrl);
|
||||
|
||||
var client = _httpClientFactory.CreateClient("Jellypod");
|
||||
|
||||
// Make a streaming request
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, decodedUrl);
|
||||
var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
_logger.LogWarning("Failed to fetch audio stream: {StatusCode}", response.StatusCode);
|
||||
return StatusCode((int)response.StatusCode);
|
||||
}
|
||||
|
||||
// Get content type
|
||||
var contentType = response.Content.Headers.ContentType?.MediaType ?? "audio/mpeg";
|
||||
var contentLength = response.Content.Headers.ContentLength;
|
||||
|
||||
// Stream the response
|
||||
var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (contentLength.HasValue)
|
||||
{
|
||||
Response.Headers["Content-Length"] = contentLength.Value.ToString(CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
Response.Headers["Accept-Ranges"] = "bytes";
|
||||
|
||||
return File(stream, contentType, enableRangeProcessing: true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error proxying audio stream");
|
||||
return StatusCode(500, "Failed to proxy audio stream");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user