New: Plex Watchlist RSS support

This commit is contained in:
Mark McDowall 2023-03-12 23:41:35 -07:00
parent 4f5a183152
commit 6d88a98282
7 changed files with 345 additions and 0 deletions

View File

@ -0,0 +1,29 @@
using System;
using NLog;
using NzbDrone.Common.Http;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Parser;
namespace NzbDrone.Core.ImportLists.Rss.Plex
{
public class PlexRssImport : RssImportBase<PlexRssImportSettings>
{
public PlexRssImport(IHttpClient httpClient,
IImportListStatusService importListStatusService,
IConfigService configService,
IParsingService parsingService,
Logger logger)
: base(httpClient, importListStatusService, configService, parsingService, logger)
{
}
public override ImportListType ListType => ImportListType.Plex;
public override TimeSpan MinRefreshInterval => TimeSpan.FromHours(6);
public override string Name => "Plex Watchlist RSS";
public override IParseImportListResponse GetParser()
{
return new PlexRssImportParser(_logger);
}
}
}

View File

@ -0,0 +1,45 @@
using System.Xml.Linq;
using NLog;
using NzbDrone.Core.Indexers;
using NzbDrone.Core.Indexers.Exceptions;
using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.ImportLists.Rss.Plex
{
public class PlexRssImportParser : RssImportBaseParser
{
private readonly Logger _logger;
public PlexRssImportParser(Logger logger)
: base(logger)
{
_logger = logger;
}
protected override ImportListItemInfo ProcessItem(XElement item)
{
var info = new ImportListItemInfo();
var guid = item.TryGetValue("guid", string.Empty);
var category = item.TryGetValue("category");
if (category != "show")
{
return null;
}
info.Title = item.TryGetValue("title", "Unknown");
if (int.TryParse(guid.Replace("tvdb://", ""), out var tvdbId))
{
info.TvdbId = tvdbId;
}
if (info.TvdbId == 0)
{
throw new UnsupportedFeedException("Each item in the RSS feed must have a guid element with a TVDB ID");
}
return info;
}
}
}

View File

@ -0,0 +1,27 @@
using FluentValidation;
using NzbDrone.Core.Annotations;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.ImportLists.Rss.Plex
{
public class PlexRssImportSettingsValidator : AbstractValidator<PlexRssImportSettings>
{
public PlexRssImportSettingsValidator()
{
RuleFor(c => c.Url).NotEmpty();
}
}
public class PlexRssImportSettings : RssImportBaseSettings, IImportListSettings
{
private PlexRssImportSettingsValidator Validator => new PlexRssImportSettingsValidator();
[FieldDefinition(0, Label = "Url", Type = FieldType.Textbox, HelpLink = "https://app.plex.tv/desktop/#!/settings/watchlist")]
public override string Url { get; set; }
public override NzbDroneValidationResult Validate()
{
return new NzbDroneValidationResult(Validator.Validate(this));
}
}
}

View File

@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using NLog;
using NzbDrone.Common.Http;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Parser;
using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.ImportLists.Rss
{
public class RssImportBase<TSettings> : HttpImportListBase<TSettings>
where TSettings : RssImportBaseSettings, new()
{
public RssImportBase(IHttpClient httpClient,
IImportListStatusService importListStatusService,
IConfigService configService,
IParsingService parsingService,
Logger logger)
: base(httpClient, importListStatusService, configService, parsingService, logger)
{
}
public override ImportListType ListType => ImportListType.Advanced;
public override TimeSpan MinRefreshInterval => TimeSpan.FromHours(6);
public override string Name => "RSS List Base";
public override IList<ImportListItemInfo> Fetch()
{
return FetchItems(g => g.GetListItems());
}
public override IParseImportListResponse GetParser()
{
return new RssImportBaseParser(_logger);
}
public override IImportListRequestGenerator GetRequestGenerator()
{
return new RssImportRequestGenerator()
{
Settings = Settings
};
}
}
}

View File

@ -0,0 +1,144 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Xml;
using System.Xml.Linq;
using NLog;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.ImportLists.Exceptions;
using NzbDrone.Core.Indexers;
using NzbDrone.Core.Indexers.Exceptions;
using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.ImportLists.Rss
{
public class RssImportBaseParser : IParseImportListResponse
{
private readonly Logger _logger;
public RssImportBaseParser(Logger logger)
{
_logger = logger;
}
public virtual IList<ImportListItemInfo> ParseResponse(ImportListResponse importResponse)
{
var series = new List<ImportListItemInfo>();
if (!PreProcess(importResponse))
{
return series;
}
var document = LoadXmlDocument(importResponse);
var items = GetItems(document).ToList();
foreach (var item in items)
{
try
{
var itemInfo = ProcessItem(item);
series.AddIfNotNull(itemInfo);
}
catch (UnsupportedFeedException itemEx)
{
itemEx.WithData("FeedUrl", importResponse.Request.Url);
itemEx.WithData("ItemTitle", item.Title());
throw;
}
catch (Exception itemEx)
{
itemEx.WithData("FeedUrl", importResponse.Request.Url);
itemEx.WithData("ItemTitle", item.Title());
_logger.Error(itemEx, "An error occurred while processing feed item from {0}", importResponse.Request.Url);
}
}
return series;
}
protected virtual bool PreProcess(ImportListResponse importListResponse)
{
if (importListResponse.HttpResponse.StatusCode != HttpStatusCode.OK)
{
throw new ImportListException(importListResponse, "Request resulted in an unexpected StatusCode [{0}]", importListResponse.HttpResponse.StatusCode);
}
if (importListResponse.HttpResponse.Headers.ContentType != null && importListResponse.HttpResponse.Headers.ContentType.Contains("text/xml") &&
importListResponse.HttpRequest.Headers.Accept != null && !importListResponse.HttpRequest.Headers.Accept.Contains("text/xml"))
{
throw new ImportListException(importListResponse, "Request responded with html content. Site is likely blocked or unavailable.");
}
return true;
}
protected virtual XDocument LoadXmlDocument(ImportListResponse importListResponse)
{
try
{
var content = XmlCleaner.ReplaceEntities(importListResponse.Content);
content = XmlCleaner.ReplaceUnicode(content);
using (var xmlTextReader = XmlReader.Create(new StringReader(content), new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore, IgnoreComments = true }))
{
return XDocument.Load(xmlTextReader);
}
}
catch (XmlException ex)
{
var contentSample = importListResponse.Content.Substring(0, Math.Min(importListResponse.Content.Length, 512));
_logger.Debug("Truncated response content (originally {0} characters): {1}", importListResponse.Content.Length, contentSample);
ex.WithData(importListResponse.HttpResponse);
throw;
}
}
protected IEnumerable<XElement> GetItems(XDocument document)
{
var root = document.Root;
if (root == null)
{
return Enumerable.Empty<XElement>();
}
var channel = root.Element("channel");
if (channel == null)
{
return Enumerable.Empty<XElement>();
}
return channel.Elements("item");
}
protected virtual ImportListItemInfo ProcessItem(XElement item)
{
var info = new ImportListItemInfo();
var guid = item.TryGetValue("guid");
if (guid != null)
{
if (int.TryParse(guid, out var tvdbId))
{
info.TvdbId = tvdbId;
}
}
info.Title = item.TryGetValue("title", "Unknown");
if (info.TvdbId == 0)
{
throw new UnsupportedFeedException("Each item in the RSS feed must have a guid element with a TVDB ID");
}
return info;
}
}
}

View File

@ -0,0 +1,29 @@
using FluentValidation;
using NzbDrone.Core.Annotations;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.ImportLists.Rss
{
public class RssImportSettingsValidator : AbstractValidator<RssImportBaseSettings>
{
public RssImportSettingsValidator()
{
RuleFor(c => c.Url).NotEmpty();
}
}
public class RssImportBaseSettings : IImportListSettings
{
private RssImportSettingsValidator Validator => new RssImportSettingsValidator();
public string BaseUrl { get; set; }
[FieldDefinition(0, Label = "Url", Type = FieldType.Textbox)]
public virtual string Url { get; set; }
public virtual NzbDroneValidationResult Validate()
{
return new NzbDroneValidationResult(Validator.Validate(this));
}
}
}

View File

@ -0,0 +1,26 @@
using System.Collections.Generic;
using NzbDrone.Common.Http;
namespace NzbDrone.Core.ImportLists.Rss
{
public class RssImportRequestGenerator : IImportListRequestGenerator
{
public RssImportBaseSettings Settings { get; set; }
public virtual ImportListPageableRequestChain GetListItems()
{
var pageableRequests = new ImportListPageableRequestChain();
pageableRequests.Add(GetSeriesRequest());
return pageableRequests;
}
private IEnumerable<ImportListRequest> GetSeriesRequest()
{
var request = new ImportListRequest(Settings.Url, HttpAccept.Rss);
yield return request;
}
}
}