updated opds to use pagination

pull/3344/head
adechant 5 months ago
parent 30bfc49b86
commit 97f8b9fd26

@ -40,54 +40,134 @@ namespace Readarr.Api.V1.OPDS
[HttpGet]
public OPDSCatalogResource GetOPDSCatalog()
{
var metadataTask = Task.Run(() => _authorService.GetAllAuthors());
/*var metadataTask = Task.Run(() => _authorService.GetAllAuthors());
var books = _bookService.GetAllBooks();
var authors = metadataTask.GetAwaiter().GetResult().ToDictionary(x => x.AuthorMetadataId);
foreach (var book in books)
{
book.Author = authors[book.AuthorMetadataId];
}
}*/
var catalog = OPDSResourceMapper.ToOPDSCatalogResource();
//catalog.Publications = MapToResource(books, wanted);
//catalog.Publications = MapToResource(books, wanted);
return catalog;
}
// /opds/publications
[HttpGet("publications")]
public OPDSPublicationsResource GetOPDSPublications()
public OPDSPublicationsResource GetOPDSPublications([FromQuery] int? page,
[FromQuery] int? itemsPerPage)
{
var metadataTask = Task.Run(() => _authorService.GetAllAuthors());
var books = _bookService.GetAllBooks();
var authors = metadataTask.GetAwaiter().GetResult().ToDictionary(x => x.AuthorMetadataId);
if (itemsPerPage == null)
{
itemsPerPage = 10;
}
else if (itemsPerPage < 10)
{
itemsPerPage = 10;
}
if (page == null)
{
page = 1;
itemsPerPage = books.Count;
}
var pageCount = ((books.Count - 1) / itemsPerPage) + 1;
if (page > pageCount)
{
throw new BadRequestException(string.Format("requested page is greater than the total page count: {0}", page));
}
foreach (var book in books)
{
book.Author = authors[book.AuthorMetadataId];
}
var publications = OPDSResourceMapper.ToOPDSPublicationsResource();
publications.Publications = MapToResource(books, false);
var pubList = MapToResource(books, false);
var publications = OPDSResourceMapper.ToOPDSPublicationsResource((int)page, (int)itemsPerPage, pubList.Count);
if (itemsPerPage == pubList.Count)
{
publications.Publications = pubList;
}
else
{
if (page == pageCount)
{
publications.Publications = pubList.GetRange(((int)page - 1) * (int)itemsPerPage, pubList.Count % (int)itemsPerPage);
}
else
{
publications.Publications = pubList.GetRange(((int)page - 1) * (int)itemsPerPage, (int)itemsPerPage);
}
}
return publications;
}
// /opds/wanted
[HttpGet("wanted")]
public OPDSPublicationsResource GetOPDSWantedPublications()
public OPDSPublicationsResource GetOPDSWantedPublications([FromQuery] int? page,
[FromQuery] int? itemsPerPage)
{
var metadataTask = Task.Run(() => _authorService.GetAllAuthors());
var books = _bookService.GetAllBooks();
var authors = metadataTask.GetAwaiter().GetResult().ToDictionary(x => x.AuthorMetadataId);
if (itemsPerPage == null)
{
itemsPerPage = 10;
}
else if (itemsPerPage < 10)
{
itemsPerPage = 10;
}
if (page == null)
{
page = 1;
itemsPerPage = books.Count;
}
var pageCount = ((books.Count - 1) / itemsPerPage) + 1;
if (page > pageCount)
{
throw new BadRequestException(string.Format("requested page is greater than the total page count: {0}", page));
}
foreach (var book in books)
{
book.Author = authors[book.AuthorMetadataId];
}
var publications = OPDSResourceMapper.ToOPDSPublicationsResource();
publications.Publications = MapToResource(books, true);
var pubList = MapToResource(books, true);
var publications = OPDSResourceMapper.ToOPDSPublicationsResource((int)page, (int)itemsPerPage, pubList.Count);
if (itemsPerPage == pubList.Count)
{
publications.Publications = pubList;
}
else
{
if (page == pageCount)
{
publications.Publications = pubList.GetRange(((int)page - 1) * (int)itemsPerPage, pubList.Count % (int)itemsPerPage);
}
else
{
publications.Publications = pubList.GetRange(((int)page - 1) * (int)itemsPerPage, (int)itemsPerPage);
}
}
return publications;
}

@ -13,6 +13,11 @@ namespace Readarr.Api.V1.OPDS
public class OPDSCatalogMetadataResource : IEmbeddedDocument
{
public string Title { get; set; }
public int NumberOfItems { get; set; }
public int ItemsPerPage { get; set; }
public int CurrentPage { get; set; }
}
public class OPDSLinkResource : IEmbeddedDocument
@ -115,7 +120,7 @@ namespace Readarr.Api.V1.OPDS
};
}
public static OPDSPublicationsResource ToOPDSPublicationsResource()
public static OPDSPublicationsResource ToOPDSPublicationsResource(int page, int itemsPerPage, int totalItems)
{
var self = new OPDSLinkResource
{
@ -130,7 +135,10 @@ namespace Readarr.Api.V1.OPDS
var meta = new OPDSCatalogMetadataResource
{
Title = self.Title
Title = self.Title,
ItemsPerPage = itemsPerPage,
CurrentPage = page,
NumberOfItems = totalItems
};
return new OPDSPublicationsResource

Loading…
Cancel
Save