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.
recyclarr/src/Trash/Sonarr/QualityDefinition/SonarrQualityDefinitionGuid...

74 lines
2.7 KiB

using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Flurl.Http;
using Trash.Extensions;
namespace Trash.Sonarr.QualityDefinition
{
public class SonarrQualityDefinitionGuideParser : ISonarrQualityDefinitionGuideParser
{
private readonly Regex _regexHeader = new(@"^#+", RegexOptions.Compiled);
private readonly Regex _regexTableRow =
new(@"\| *(.*?) *\| *([\d.]+) *\| *([\d.]+) *\|", RegexOptions.Compiled);
public async Task<string> GetMarkdownData()
{
return await
"https://raw.githubusercontent.com/TRaSH-/Guides/master/docs/Sonarr/V3/Sonarr-Quality-Settings-File-Size.md"
.GetStringAsync();
}
public IDictionary<SonarrQualityDefinitionType, List<SonarrQualityData>> ParseMarkdown(string markdown)
{
var results = new Dictionary<SonarrQualityDefinitionType, List<SonarrQualityData>>();
List<SonarrQualityData>? table = null;
var reader = new StringReader(markdown);
for (var line = reader.ReadLine(); line != null; line = reader.ReadLine())
{
if (string.IsNullOrEmpty(line))
{
continue;
}
var match = _regexHeader.Match(line);
if (match.Success)
{
var type = line.ContainsIgnoreCase("anime")
? SonarrQualityDefinitionType.Anime
: SonarrQualityDefinitionType.Series;
table = results.GetOrCreate(type);
// If we grab a table that isn't empty, that means for whatever reason *another* table
// in the markdown is trying to modify a previous table's data. For example, maybe there
// are two "Series" quality tables. That would be a weird edge case, but handle that
// here just in case.
if (table.Count > 0)
{
table = null;
}
}
else if (table != null)
{
match = _regexTableRow.Match(line);
if (match.Success)
{
table.Add(new SonarrQualityData
{
Name = match.Groups[1].Value,
Min = match.Groups[2].Value.ToDecimal(),
Max = match.Groups[3].Value.ToDecimal()
});
}
}
}
return results;
}
}
}