You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Sonarr/src/Sonarr.Api.V3/Parse/ParseController.cs

79 lines
2.7 KiB

using Microsoft.AspNetCore.Mvc;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.CustomFormats;
using NzbDrone.Core.Download.Aggregation;
using NzbDrone.Core.Parser;
using Sonarr.Api.V3.CustomFormats;
using Sonarr.Api.V3.Episodes;
using Sonarr.Api.V3.Series;
using Sonarr.Http;
namespace Sonarr.Api.V3.Parse
{
[V3ApiController]
public class ParseController : Controller
{
private readonly IParsingService _parsingService;
private readonly IRemoteEpisodeAggregationService _aggregationService;
private readonly ICustomFormatCalculationService _formatCalculator;
public ParseController(IParsingService parsingService,
IRemoteEpisodeAggregationService aggregationService,
ICustomFormatCalculationService formatCalculator)
{
_parsingService = parsingService;
_aggregationService = aggregationService;
_formatCalculator = formatCalculator;
}
[HttpGet]
[Produces("application/json")]
public ParseResource Parse(string title, string path)
{
if (title.IsNullOrWhiteSpace())
{
return null;
}
var parsedEpisodeInfo = path.IsNotNullOrWhiteSpace() ? Parser.ParsePath(path) : Parser.ParseTitle(title);
if (parsedEpisodeInfo == null)
{
return new ParseResource
{
Title = title
};
}
var remoteEpisode = _parsingService.Map(parsedEpisodeInfo, 0, 0);
if (remoteEpisode != null)
{
_aggregationService.Augment(remoteEpisode);
remoteEpisode.CustomFormats = _formatCalculator.ParseCustomFormat(remoteEpisode, 0);
remoteEpisode.CustomFormatScore = remoteEpisode?.Series?.QualityProfile?.Value.CalculateCustomFormatScore(remoteEpisode.CustomFormats) ?? 0;
return new ParseResource
{
Title = title,
ParsedEpisodeInfo = remoteEpisode.ParsedEpisodeInfo,
Series = remoteEpisode.Series.ToResource(),
Episodes = remoteEpisode.Episodes.ToResource(),
Languages = remoteEpisode.Languages,
CustomFormats = remoteEpisode.CustomFormats?.ToResource(false),
CustomFormatScore = remoteEpisode.CustomFormatScore
};
}
else
{
return new ParseResource
{
Title = title,
ParsedEpisodeInfo = parsedEpisodeInfo
};
}
}
}
}