2022-05-01 00:33:22 -05:00
|
|
|
using System;
|
2022-06-13 01:52:41 -05:00
|
|
|
using System.Collections.Generic;
|
2022-05-01 00:33:22 -05:00
|
|
|
using System.Net.Mime;
|
2022-07-29 03:34:55 -05:00
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2022-05-01 00:33:22 -05:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace ConfusedPolarBear.Plugin.IntroSkipper.Controllers;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Skip intro controller.
|
|
|
|
/// </summary>
|
|
|
|
[Authorize]
|
|
|
|
[ApiController]
|
|
|
|
[Produces(MediaTypeNames.Application.Json)]
|
|
|
|
public class SkipIntroController : ControllerBase
|
|
|
|
{
|
|
|
|
/// <summary>
|
2022-05-09 22:50:41 -05:00
|
|
|
/// Initializes a new instance of the <see cref="SkipIntroController"/> class.
|
2022-05-01 00:33:22 -05:00
|
|
|
/// </summary>
|
|
|
|
public SkipIntroController()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2022-06-12 21:28:24 -05:00
|
|
|
/// Returns the timestamps of the introduction in a television episode. Responses are in API version 1 format.
|
2022-05-01 00:33:22 -05:00
|
|
|
/// </summary>
|
2022-05-01 01:24:57 -05:00
|
|
|
/// <param name="id">ID of the episode. Required.</param>
|
2022-05-01 00:33:22 -05:00
|
|
|
/// <response code="200">Episode contains an intro.</response>
|
|
|
|
/// <response code="404">Failed to find an intro in the provided episode.</response>
|
2022-05-09 22:50:41 -05:00
|
|
|
/// <returns>Detected intro.</returns>
|
2022-05-01 00:33:22 -05:00
|
|
|
[HttpGet("Episode/{id}/IntroTimestamps")]
|
2022-06-12 21:28:24 -05:00
|
|
|
[HttpGet("Episode/{id}/IntroTimestamps/v1")]
|
2022-05-01 01:24:57 -05:00
|
|
|
public ActionResult<Intro> GetIntroTimestamps([FromRoute] Guid id)
|
2022-05-01 00:33:22 -05:00
|
|
|
{
|
2022-06-12 21:28:24 -05:00
|
|
|
var intro = GetIntro(id);
|
2022-05-01 00:33:22 -05:00
|
|
|
|
2022-06-12 21:28:24 -05:00
|
|
|
if (intro is null || !intro.Valid)
|
2022-05-01 00:33:22 -05:00
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
}
|
|
|
|
|
2022-05-05 18:26:02 -05:00
|
|
|
// Populate the prompt show/hide times.
|
|
|
|
var config = Plugin.Instance!.Configuration;
|
|
|
|
intro.ShowSkipPromptAt = Math.Max(0, intro.IntroStart - config.ShowPromptAdjustment);
|
|
|
|
intro.HideSkipPromptAt = intro.IntroStart + config.HidePromptAdjustment;
|
2022-09-01 22:37:03 -05:00
|
|
|
intro.IntroEnd -= config.SecondsOfIntroToPlay;
|
2022-05-05 18:26:02 -05:00
|
|
|
|
2022-05-01 00:33:22 -05:00
|
|
|
return intro;
|
|
|
|
}
|
2022-06-07 12:18:03 -05:00
|
|
|
|
2022-06-12 21:28:24 -05:00
|
|
|
/// <summary>Lookup and return the intro timestamps for the provided item.</summary>
|
|
|
|
/// <param name="id">Unique identifier of this episode.</param>
|
|
|
|
/// <returns>Intro object if the provided item has an intro, null otherwise.</returns>
|
|
|
|
private Intro? GetIntro(Guid id)
|
|
|
|
{
|
2022-09-04 03:16:33 -05:00
|
|
|
// Returns a copy to avoid mutating the original Intro object stored in the dictionary.
|
|
|
|
return Plugin.Instance!.Intros.TryGetValue(id, out var intro) ? new Intro(intro) : null;
|
2022-06-12 21:28:24 -05:00
|
|
|
}
|
|
|
|
|
2022-06-07 12:18:03 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Erases all previously discovered introduction timestamps.
|
|
|
|
/// </summary>
|
|
|
|
/// <response code="204">Operation successful.</response>
|
|
|
|
/// <returns>No content.</returns>
|
|
|
|
[Authorize(Policy = "RequiresElevation")]
|
|
|
|
[HttpPost("Intros/EraseTimestamps")]
|
|
|
|
public ActionResult ResetIntroTimestamps()
|
|
|
|
{
|
|
|
|
Plugin.Instance!.Intros.Clear();
|
|
|
|
Plugin.Instance!.SaveTimestamps();
|
|
|
|
return NoContent();
|
|
|
|
}
|
2022-06-13 01:52:41 -05:00
|
|
|
|
|
|
|
/// <summary>
|
2022-11-24 00:43:23 -06:00
|
|
|
/// Get all introductions or credits. Only used by the end to end testing script.
|
2022-06-13 01:52:41 -05:00
|
|
|
/// </summary>
|
2022-11-24 00:43:23 -06:00
|
|
|
/// <param name="mode">Mode.</param>
|
|
|
|
/// <response code="200">All timestamps have been returned.</response>
|
2022-07-29 03:34:55 -05:00
|
|
|
/// <returns>List of IntroWithMetadata objects.</returns>
|
2022-06-13 01:52:41 -05:00
|
|
|
[Authorize(Policy = "RequiresElevation")]
|
|
|
|
[HttpGet("Intros/All")]
|
2022-11-24 00:43:23 -06:00
|
|
|
public ActionResult<List<IntroWithMetadata>> GetAllTimestamps(
|
|
|
|
[FromQuery] AnalysisMode mode = AnalysisMode.Introduction)
|
2022-06-13 01:52:41 -05:00
|
|
|
{
|
2022-07-29 03:34:55 -05:00
|
|
|
List<IntroWithMetadata> intros = new();
|
|
|
|
|
2022-11-24 00:43:23 -06:00
|
|
|
var timestamps = mode == AnalysisMode.Introduction ?
|
|
|
|
Plugin.Instance!.Intros :
|
|
|
|
Plugin.Instance!.Credits;
|
|
|
|
|
2022-07-29 03:34:55 -05:00
|
|
|
// Get metadata for all intros
|
2022-11-24 00:43:23 -06:00
|
|
|
foreach (var intro in timestamps)
|
2022-07-29 03:34:55 -05:00
|
|
|
{
|
|
|
|
// Get the details of the item from Jellyfin
|
|
|
|
var rawItem = Plugin.Instance!.GetItem(intro.Key);
|
|
|
|
if (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;
|
2022-06-13 01:52:41 -05:00
|
|
|
}
|
2022-05-01 00:33:22 -05:00
|
|
|
}
|