Skip to content

Commit

Permalink
feat(schema): ✨ add copyright and isbn fields
Browse files Browse the repository at this point in the history
not all items have these, but most major works do.

2 new model fields are added. rollbacks not recommended, but probably supported
  • Loading branch information
djdembeck committed Aug 19, 2023
1 parent feec8d8 commit d9bf840
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/config/models/Book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const bookSchema = schema(
}),
{ required: true }
),
copyright: types.number(),
description: types.string({ required: true }),
formatType: types.string({ required: true }),
genres: types.array(
Expand All @@ -24,6 +25,7 @@ const bookSchema = schema(
),
image: types.string(),
isAdult: types.boolean({ required: true }),
isbn: types.string(),
language: types.string({ required: true }),
narrators: types.array(
types.object({
Expand Down Expand Up @@ -57,6 +59,7 @@ const bookSchema = schema(
{
defaults: {
isAdult: false,
isbn: '',
region: 'us'
},
timestamps: true
Expand Down
12 changes: 12 additions & 0 deletions src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,13 @@ export type ApiNarratorOnBook = z.infer<typeof ApiNarratorOnBookSchema>
export const ApiBookSchema = z.object({
asin: AsinSchema,
authors: z.array(ApiAuthorOnBookSchema),
copyright: z.number().int().optional(),
description: z.string(),
formatType: z.string(),
genres: z.array(ApiGenreSchema).optional(),
image: z.string().url().optional(),
isAdult: z.boolean().default(false),
isbn: z.string().optional(),
language: z.string(),
narrators: z.array(ApiNarratorOnBookSchema).optional(),
publisherName: z.string(),
Expand Down Expand Up @@ -164,20 +166,30 @@ const baseShape = z.object({
available_codecs: z.array(AudibleCodecSchema).optional(),
category_ladders: z.array(AudibleCategoriesSchema),
content_type: z.string(),
copyright: z.string(),
date_first_available: z.string().optional(),
editorial_reviews: z.array(z.string()).optional(),
extended_product_description: z.string().optional(),
format_type: z.string(),
has_children: z.boolean(),
is_adult_product: z.boolean(),
is_listenable: z.boolean(),
is_pdf_url_available: z.boolean().optional(),
is_purchasability_suppressed: z.boolean(),
isbn: z.string().optional(),
issue_date: z.string(),
language: z.string(),
merchandising_description: z.string().optional(),
merchandising_summary: z.string(),
narrators: z.array(ApiNarratorOnBookSchema).optional(),
product_images: z.record(z.string(), z.string().url()).optional(),
platinum_keywords: z.array(z.string()).optional(),
product_site_launch_date: z.string().datetime().optional(),
publisher_name: z.string(),
publisher_summary: z.string(),
rating: AudibleRatingSchema,
// Audible passes this as a string, but it's a boolean in the string
read_along_support: z.coerce.boolean().optional(),
release_date: z.string(),
runtime_length_min: z.number().or(z.literal(0)).optional(),
sku: z.string().optional(),
Expand Down
25 changes: 23 additions & 2 deletions src/helpers/books/audible/ApiHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ class ApiHelper {
const baseDomain = 'https://api.audible'
const regionTLD = regions[region].tld
const baseUrl = '1.0/catalog/products'
// Last item is intentionally image_sizes
const paramArr = [
'category_ladders',
'contributors',
'media',
'product_attrs',
'product_desc',
'product_details',
'product_extended_attrs',
'product_attrs',
'media',
'rating',
'series',
'image_sizes=500,1024'
Expand Down Expand Up @@ -85,6 +87,23 @@ class ApiHelper {
return ApiGenreSchema.safeParse(genre).success
}

getCopyrightYear() {
if (!this.audibleResponse) throw new Error(ErrorMessageNoData(this.asin, 'ApiHelper'))
const regex = /(?:©|\(c\)|copyright\b)\s*(\d{4})/iu
const copyright = this.audibleResponse.copyright
const match = regex.exec(copyright)

// find the lowest year in the string
if (match) {
try {
const years = match[1].split('-').map(Number)
return Math.min(...years)
} catch (error) {
return undefined
}
}
}

/**
* Find the parent categories (genres) of the given category array
* @param {AudibleCategory[]} categories array of categories to check
Expand Down Expand Up @@ -270,6 +289,7 @@ class ApiHelper {
}
return authorJson
}),
copyright: this.getCopyrightYear(),
description: cleanupDescription(
htmlToText(this.audibleResponse['merchandising_summary'], {
wordwrap: false
Expand All @@ -281,6 +301,7 @@ class ApiHelper {
}),
image: this.getHighResImage(),
isAdult: this.audibleResponse.is_adult_product,
isbn: this.audibleResponse.isbn ?? '',
language: this.audibleResponse.language,
narrators:
this.audibleResponse.narrators?.map((person: ApiNarratorOnBook) => {
Expand Down

0 comments on commit d9bf840

Please sign in to comment.