parent
fb7ec5c61e
commit
ec0e930a54
@ -0,0 +1,122 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using FluentValidation.Results;
|
||||||
|
using NLog;
|
||||||
|
using NzbDrone.Common.Extensions;
|
||||||
|
using NzbDrone.Common.Http;
|
||||||
|
using NzbDrone.Core.Configuration;
|
||||||
|
using NzbDrone.Core.MetadataSource;
|
||||||
|
using NzbDrone.Core.Parser;
|
||||||
|
using NzbDrone.Core.Parser.Model;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.ImportLists.Goodreads
|
||||||
|
{
|
||||||
|
public class GoodreadsListImportList : ImportListBase<GoodreadsListImportListSettings>
|
||||||
|
{
|
||||||
|
private readonly IProvideListInfo _listInfo;
|
||||||
|
|
||||||
|
public override string Name => "Goodreads List";
|
||||||
|
public override ImportListType ListType => ImportListType.Goodreads;
|
||||||
|
|
||||||
|
public GoodreadsListImportList(IProvideListInfo listInfo,
|
||||||
|
IImportListStatusService importListStatusService,
|
||||||
|
IConfigService configService,
|
||||||
|
IParsingService parsingService,
|
||||||
|
Logger logger)
|
||||||
|
: base(importListStatusService, configService, parsingService, logger)
|
||||||
|
{
|
||||||
|
_listInfo = listInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IList<ImportListItemInfo> Fetch()
|
||||||
|
{
|
||||||
|
var result = new List<ImportListItemInfo>();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var pageNum = 1;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (pageNum > 100)
|
||||||
|
{
|
||||||
|
// you always seem to get back page 100 for bigger pages...
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
var page = FetchPage(pageNum++);
|
||||||
|
|
||||||
|
if (page.Any())
|
||||||
|
{
|
||||||
|
result.AddRange(page);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_importListStatusService.RecordSuccess(Definition.Id);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
_importListStatusService.RecordFailure(Definition.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return CleanupListItems(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<ImportListItemInfo> FetchPage(int page)
|
||||||
|
{
|
||||||
|
var list = _listInfo.GetListInfo(Settings.ListId, page);
|
||||||
|
var result = new List<ImportListItemInfo>();
|
||||||
|
|
||||||
|
foreach (var book in list.Books)
|
||||||
|
{
|
||||||
|
var author = book.Authors.FirstOrDefault();
|
||||||
|
|
||||||
|
result.Add(new ImportListItemInfo
|
||||||
|
{
|
||||||
|
BookGoodreadsId = book.Work.Id.ToString(),
|
||||||
|
Book = book.Work.OriginalTitle,
|
||||||
|
EditionGoodreadsId = book.Id.ToString(),
|
||||||
|
Author = author?.Name,
|
||||||
|
AuthorGoodreadsId = author?.Id.ToString()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Test(List<ValidationFailure> failures)
|
||||||
|
{
|
||||||
|
failures.AddIfNotNull(TestConnection());
|
||||||
|
}
|
||||||
|
|
||||||
|
private ValidationFailure TestConnection()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_listInfo.GetListInfo(Settings.ListId, 1);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
catch (HttpException e)
|
||||||
|
{
|
||||||
|
_logger.Warn(e, "Goodreads API Error");
|
||||||
|
if (e.Response.StatusCode == HttpStatusCode.NotFound)
|
||||||
|
{
|
||||||
|
return new ValidationFailure(nameof(Settings.ListId), $"List {Settings.ListId} not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ValidationFailure(nameof(Settings.ListId), $"Could not get list data");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.Warn(ex, "Unable to connect to Goodreads");
|
||||||
|
|
||||||
|
return new ValidationFailure(string.Empty, "Unable to connect to import list, check the log for more details");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
using FluentValidation;
|
||||||
|
using NzbDrone.Core.Annotations;
|
||||||
|
using NzbDrone.Core.Validation;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.ImportLists.Goodreads
|
||||||
|
{
|
||||||
|
public class GoodreadsListImportListValidator : AbstractValidator<GoodreadsListImportListSettings>
|
||||||
|
{
|
||||||
|
public GoodreadsListImportListValidator()
|
||||||
|
{
|
||||||
|
RuleFor(c => c.ListId).GreaterThan(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class GoodreadsListImportListSettings : IImportListSettings
|
||||||
|
{
|
||||||
|
private static readonly GoodreadsListImportListValidator Validator = new ();
|
||||||
|
|
||||||
|
public GoodreadsListImportListSettings()
|
||||||
|
{
|
||||||
|
BaseUrl = "www.goodreads.com";
|
||||||
|
}
|
||||||
|
|
||||||
|
public string BaseUrl { get; set; }
|
||||||
|
|
||||||
|
[FieldDefinition(0, Label = "List ID", HelpText = "Goodreads list ID")]
|
||||||
|
public int ListId { get; set; }
|
||||||
|
|
||||||
|
public NzbDroneValidationResult Validate()
|
||||||
|
{
|
||||||
|
return new NzbDroneValidationResult(Validator.Validate(this));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
using NzbDrone.Core.Books;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.MetadataSource.Goodreads
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Represents information about a book series as defined by the Goodreads API.
|
||||||
|
/// </summary>
|
||||||
|
[DebuggerDisplay("{DebuggerDisplay,nq}")]
|
||||||
|
public sealed class ListResource : GoodreadsResource
|
||||||
|
{
|
||||||
|
public ListResource()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ElementName => "list";
|
||||||
|
|
||||||
|
public int Page { get; private set; }
|
||||||
|
|
||||||
|
public int PerPage { get; private set; }
|
||||||
|
|
||||||
|
public int ListBooksCount { get; private set; }
|
||||||
|
|
||||||
|
public List<BookResource> Books { get; set; }
|
||||||
|
|
||||||
|
public override void Parse(XElement element)
|
||||||
|
{
|
||||||
|
Page = element.ElementAsInt("page");
|
||||||
|
PerPage = element.ElementAsInt("per_page");
|
||||||
|
ListBooksCount = element.ElementAsInt("total_books");
|
||||||
|
|
||||||
|
Books = element.ParseChildren<BookResource>("books", "book") ?? new List<BookResource>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
using NzbDrone.Core.MetadataSource.Goodreads;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.MetadataSource
|
||||||
|
{
|
||||||
|
public interface IProvideListInfo
|
||||||
|
{
|
||||||
|
ListResource GetListInfo(int id, int page, bool useCache = true);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue