Skip to content

Commit

Permalink
Add kindle to server
Browse files Browse the repository at this point in the history
  • Loading branch information
ahobsonsayers committed Apr 25, 2024
1 parent a8db659 commit 829bfac
Show file tree
Hide file tree
Showing 4 changed files with 437 additions and 116 deletions.
150 changes: 101 additions & 49 deletions schema/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,64 +16,57 @@ security:
- api_key: []

paths:
/search:
/goodreads/search:
get:
description: Search for books
operationId: search
summary: Search for books
security:
- api_key: []
operationId: searchGoodreads
summary: Search for books using goodreads
description: Search for books using goodreads
parameters:
- name: query
in: query
required: true
schema:
type: string
- name: author
in: query
required: false
- $ref: "#/components/parameters/query"
- $ref: "#/components/parameters/author"
responses:
"200":
$ref: "#/components/responses/200"
"400":
$ref: "#/components/responses/400"
"401":
$ref: "#/components/responses/401"
"500":
$ref: "#/components/responses/500"

/kindle/{region}/search:
get:
operationId: searchKindle
summary: Search for books using kindle
description: Search for books using kindle
parameters:
- provider:
name: region
in: path
schema:
type: string

enum:
- "au"
- "ca"
- "de"
- "es"
- "fr"
- "in"
- "it"
- "jp"
- "uk"
- "us"
- $ref: "#/components/parameters/query"
- $ref: "#/components/parameters/author"
responses:
"200":
description: OK
content:
application/json:
schema:
type: object
properties:
matches:
type: array
items:
$ref: "#/components/schemas/BookMetadata"
$ref: "#/components/responses/200"
"400":
description: Bad Request
content:
application/json:
schema:
type: object
properties:
error:
type: string
$ref: "#/components/responses/400"
"401":
description: Unauthorized
content:
application/json:
schema:
type: object
properties:
error:
type: string
$ref: "#/components/responses/401"
"500":
description: Internal Server Error
content:
application/json:
schema:
type: object
properties:
error:
type: string
$ref: "#/components/responses/500"

components:
securitySchemes:
Expand Down Expand Up @@ -139,3 +132,62 @@ components:
type: string
sequence:
type: string

parameters:
query:
name: query
in: query
required: true
schema:
type: string

author:
name: author
in: query
required: false
schema:
type: string

responses:
200:
description: OK
content:
application/json:
schema:
type: object
properties:
matches:
type: array
items:
$ref: "#/components/schemas/BookMetadata"

400:
description: Bad Request
content:
application/json:
schema:
type: object
properties:
error:
type: string

401:
description: Unauthorized
content:
application/json:
schema:
type: object
properties:
error:
type: string

500:
type: object
description: Internal Server Error
content:
application/json:
schema:
type: object
properties:
error:
type: string
66 changes: 64 additions & 2 deletions server/book.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,60 @@
package server

import (
"context"
"strconv"

"github.com/ahobsonsayers/abs-goodreads/goodreads"
"github.com/ahobsonsayers/abs-goodreads/kindle"
"github.com/samber/lo"
)

func GoodreadsBookToAudioBookShelfBook(goodreadsBook goodreads.Book) BookMetadata {
func searchGoodreadsBooks(ctx context.Context, bookTitle string, bookAuthor *string) ([]BookMetadata, error) {
goodreadsBooks, err := goodreads.DefaultClient.SearchBooks(ctx, bookTitle, bookAuthor)
if err != nil {
return nil, err
}

books := make([]BookMetadata, 0, len(goodreadsBooks))
for _, goodreadsBook := range goodreadsBooks {
book := goodreadsBookToBookMetadata(goodreadsBook)
books = append(books, book)
}

return books, nil
}

func searchKindleBooks(
ctx context.Context,
countryCode SearchKindleParamsRegion,
bookTitle string,
bookAuthor *string,
) ([]BookMetadata, error) {
kindleClient, err := kindle.NewClient(nil, lo.ToPtr(string(countryCode)))
if err != nil {
return nil, err
}

kindleBooks, err := kindleClient.Search(ctx, bookTitle, bookAuthor)
if err != nil {
return nil, err
}

books := make([]BookMetadata, 0, len(kindleBooks))
for _, kindleBook := range kindleBooks {
book := kindleBookToBookMetadata(kindleBook)
books = append(books, book)
}

return books, nil
}

func goodreadsBookToBookMetadata(goodreadsBook goodreads.Book) BookMetadata {
title := goodreadsBook.Work.Title
if title == "" {
title = goodreadsBook.BestEdition.Title
}

var authorName *string
if len(goodreadsBook.Authors) != 0 {
authorName = &goodreadsBook.Authors[0].Name
Expand All @@ -29,7 +76,7 @@ func GoodreadsBookToAudioBookShelfBook(goodreadsBook goodreads.Book) BookMetadat

return BookMetadata{
// Work Fields
Title: goodreadsBook.Work.Title,
Title: title,
Author: authorName,
PublishedYear: lo.ToPtr(strconv.Itoa(goodreadsBook.Work.PublicationYear)),
// Edition Fields
Expand All @@ -43,3 +90,18 @@ func GoodreadsBookToAudioBookShelfBook(goodreadsBook goodreads.Book) BookMetadat
Genres: lo.ToPtr([]string(goodreadsBook.Genres)),
}
}

func kindleBookToBookMetadata(kindleBook kindle.Book) BookMetadata {
var publishedYear *string
if kindleBook.PublishDate != nil {
publishedYear = lo.ToPtr(strconv.Itoa(kindleBook.PublishDate.Year()))
}

return BookMetadata{
Asin: &kindleBook.ASIN,
Title: kindleBook.Title,
Author: &kindleBook.Author,
Cover: &kindleBook.Cover,
PublishedYear: publishedYear,
}
}
Loading

0 comments on commit 829bfac

Please sign in to comment.