using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Net.Mime;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace ConfusedPolarBear.Plugin.IntroSkipper.Controllers;
///
/// Audio fingerprint visualization controller. Allows browsing fingerprints on a per episode basis.
///
[Authorize(Policy = "RequiresElevation")]
[ApiController]
[Produces(MediaTypeNames.Application.Json)]
[Route("Intros")]
public class VisualizationController : ControllerBase
{
///
/// Initializes a new instance of the class.
///
public VisualizationController()
{
}
///
/// Returns all show names and seasons.
///
/// Dictionary of show names to a list of season names.
[HttpGet("Shows")]
public ActionResult>> GetShowSeasons()
{
var showSeasons = new Dictionary>();
// Loop through all episodes in the analysis queue
foreach (var episodes in Plugin.Instance!.AnalysisQueue)
{
foreach (var episode in episodes.Value)
{
// Add each season's name to the series hashset
var series = episode.SeriesName;
if (!showSeasons.ContainsKey(series))
{
showSeasons[series] = new HashSet();
}
showSeasons[series].Add(GetSeasonName(episode));
}
}
return showSeasons;
}
///
/// Returns the names and unique identifiers of all episodes in the provided season.
///
/// Show name.
/// Season name.
/// List of episode titles.
[HttpGet("Show/{Series}/{Season}")]
public ActionResult> GetSeasonEpisodes(
[FromRoute] string series,
[FromRoute] string season)
{
var episodes = new List();
foreach (var queuedEpisodes in Plugin.Instance!.AnalysisQueue)
{
var first = queuedEpisodes.Value[0];
var firstSeasonName = GetSeasonName(first);
// Assert that the queued episode series and season are equal to what was requested
if (
!string.Equals(first.SeriesName, series, StringComparison.OrdinalIgnoreCase) ||
!string.Equals(firstSeasonName, season, StringComparison.OrdinalIgnoreCase))
{
continue;
}
foreach (var queuedEpisode in queuedEpisodes.Value)
{
episodes.Add(new EpisodeVisualization(queuedEpisode.EpisodeId, queuedEpisode.Name));
}
}
return episodes;
}
///
/// Fingerprint the provided episode and returns the uncompressed fingerprint data points.
///
/// Episode id.
/// Read only collection of fingerprint points.
[HttpGet("Fingerprint/{Id}")]
public ActionResult> GetEpisodeFingerprint([FromRoute] Guid id)
{
var queue = Plugin.Instance!.AnalysisQueue;
// Search through all queued episodes to find the requested id
foreach (var season in queue)
{
foreach (var needle in season.Value)
{
if (needle.EpisodeId == id)
{
return Chromaprint.Fingerprint(needle);
}
}
}
return NotFound();
}
private string GetSeasonName(QueuedEpisode episode)
{
return "Season " + episode.SeasonNumber.ToString(CultureInfo.InvariantCulture);
}
}