using System;
using System.Collections.Generic;
using System.Net.Mime;
using ConfusedPolarBear.Plugin.IntroSkipper.Configuration;
using MediaBrowser.Controller.Entities.TV;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace ConfusedPolarBear.Plugin.IntroSkipper.Controllers;
///
/// Skip intro controller.
///
[Authorize]
[ApiController]
[Produces(MediaTypeNames.Application.Json)]
public class SkipIntroController : ControllerBase
{
///
/// Initializes a new instance of the class.
///
public SkipIntroController()
{
}
///
/// Returns the timestamps of the introduction in a television episode. Responses are in API version 1 format.
///
/// ID of the episode. Required.
/// Timestamps to return. Optional. Defaults to Introduction for backwards compatibility.
/// Episode contains an intro.
/// Failed to find an intro in the provided episode.
/// Detected intro.
[HttpGet("Episode/{id}/IntroTimestamps")]
[HttpGet("Episode/{id}/IntroTimestamps/v1")]
public ActionResult GetIntroTimestamps(
[FromRoute] Guid id,
[FromQuery] AnalysisMode mode = AnalysisMode.Introduction)
{
var intro = GetIntro(id, mode);
if (intro is null || !intro.Valid)
{
return NotFound();
}
return intro;
}
///
/// Gets a dictionary of all skippable segments.
///
/// Media ID.
/// Skippable segments dictionary.
/// Dictionary of skippable segments.
[HttpGet("Episode/{id}/IntroSkipperSegments")]
public ActionResult> GetSkippableSegments([FromRoute] Guid id)
{
var segments = new Dictionary();
if (GetIntro(id, AnalysisMode.Introduction) is Intro intro)
{
segments[AnalysisMode.Introduction] = intro;
}
if (GetIntro(id, AnalysisMode.Credits) is Intro credits)
{
segments[AnalysisMode.Credits] = credits;
}
return segments;
}
/// Lookup and return the skippable timestamps for the provided item.
/// Unique identifier of this episode.
/// Mode.
/// Intro object if the provided item has an intro, null otherwise.
private Intro? GetIntro(Guid id, AnalysisMode mode)
{
try
{
var timestamp = mode == AnalysisMode.Introduction ?
Plugin.Instance!.Intros[id] :
Plugin.Instance!.Credits[id];
// Operate on a copy to avoid mutating the original Intro object stored in the dictionary.
var segment = new Intro(timestamp);
var config = Plugin.Instance!.Configuration;
segment.IntroEnd -= config.SecondsOfIntroToPlay;
if (config.PersistSkipButton)
{
segment.ShowSkipPromptAt = segment.IntroStart;
segment.HideSkipPromptAt = segment.IntroEnd;
}
else
{
segment.ShowSkipPromptAt = Math.Max(0, segment.IntroStart - config.ShowPromptAdjustment);
segment.HideSkipPromptAt = Math.Min(
segment.IntroStart + config.HidePromptAdjustment,
segment.IntroEnd);
}
return segment;
}
catch (KeyNotFoundException)
{
return null;
}
}
///
/// Erases all previously discovered introduction timestamps.
///
/// Mode.
/// Operation successful.
/// No content.
[Authorize(Policy = "RequiresElevation")]
[HttpPost("Intros/EraseTimestamps")]
public ActionResult ResetIntroTimestamps([FromQuery] AnalysisMode mode)
{
if (mode == AnalysisMode.Introduction)
{
Plugin.Instance!.Intros.Clear();
}
else if (mode == AnalysisMode.Credits)
{
Plugin.Instance!.Credits.Clear();
}
Plugin.Instance!.SaveTimestamps();
return NoContent();
}
///
/// Get all introductions or credits. Only used by the end to end testing script.
///
/// Mode.
/// All timestamps have been returned.
/// List of IntroWithMetadata objects.
[Authorize(Policy = "RequiresElevation")]
[HttpGet("Intros/All")]
public ActionResult> GetAllTimestamps(
[FromQuery] AnalysisMode mode = AnalysisMode.Introduction)
{
List intros = new();
var timestamps = mode == AnalysisMode.Introduction ?
Plugin.Instance!.Intros :
Plugin.Instance!.Credits;
// Get metadata for all intros
foreach (var intro in timestamps)
{
// Get the details of the item from Jellyfin
var rawItem = Plugin.Instance.GetItem(intro.Key);
if (rawItem == null || rawItem is not Episode episode)
{
throw new InvalidCastException("Unable to cast item id " + intro.Key + " to an Episode");
}
// Associate the metadata with the intro
intros.Add(
new IntroWithMetadata(
episode.SeriesName,
episode.AiredSeasonNumber ?? 0,
episode.Name,
intro.Value));
}
return intros;
}
///
/// Gets the user interface configuration.
///
/// UserInterfaceConfiguration returned.
/// UserInterfaceConfiguration.
[HttpGet]
[Route("Intros/UserInterfaceConfiguration")]
public ActionResult GetUserInterfaceConfiguration()
{
var config = Plugin.Instance!.Configuration;
return new UserInterfaceConfiguration(
config.SkipButtonVisible,
config.SkipButtonIntroText,
config.SkipButtonEndCreditsText);
}
}