Skip to content

Commit 57fbbb9

Browse files
committed
Improve author description for non native languages
1 parent f753950 commit 57fbbb9

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

app/helper/author.ts

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Infer } from '@vinejs/vine/types'
22
import { authorBookValidator, getBasicValidator, searchAuthorValidator } from '#validators/common'
33
import Author from '#models/author'
44
import axios from 'axios'
5-
import { audibleHeaders, getAudibleExtraHeaders, regionMap } from '#config/app'
5+
import { audibleHeaders, getAudibleExtraHeaders, localRegionMap, regionMap } from '#config/app'
66
import { AudibleHelper } from './audible.js'
77
import { HttpContext } from '@adonisjs/core/http'
88
import NotFoundException from '#exceptions/not_found_exception'
@@ -75,14 +75,31 @@ export class AuthorHelper {
7575
)
7676
}
7777

78+
private static async getAuthorDetails(payload: Infer<typeof getBasicValidator>) {
79+
// Credits for this endpoint to https://github.com/sunbrolynk
80+
// The discovered repository did not contain any license, but simple URLs/API endpoints
81+
// are not copyrightable. On that note, said repository infringes this repos copyright (Thus not liking repo).
82+
// Since the implementation is not taken from said repository, there's no copyright infringement.
83+
84+
return await axios.get(
85+
`https://api.audible${regionMap[payload.region]}/1.0/catalog/contributors/` + payload.asin,
86+
{
87+
headers: { ...getAudibleExtraHeaders(payload.region), ...audibleHeaders },
88+
params: {
89+
locale: localRegionMap[payload.region ?? 'us'],
90+
},
91+
}
92+
)
93+
}
94+
7895
private static async fetchFromAudible(
7996
payload: Infer<typeof getBasicValidator>,
8097
author?: Author | null
8198
) {
8299
const startTime = new Date()
83100
const ctx = HttpContext.get()
84101

85-
const response = await AuthorHelper.getAuthorPage(payload)
102+
const response = await AuthorHelper.getAuthorDetails(payload)
86103

87104
if (ctx)
88105
void ctx.logger.info({
@@ -94,11 +111,11 @@ export class AuthorHelper {
94111

95112
if (response.status === 200) {
96113
const json: any = response.data
97-
if (!json || Object.keys(response.data.page_details?.model || {}).length === 0) {
114+
if (!json || response.data.contributor?.name == null) {
98115
throw new NotFoundException()
99116
}
100117

101-
return await AuthorHelper.saveResponse(json, payload, author)
118+
return await AuthorHelper.saveResponse(json.contributor, payload, author)
102119
}
103120

104121
return null
@@ -109,26 +126,25 @@ export class AuthorHelper {
109126
payload: Infer<typeof getBasicValidator>,
110127
author: Author
111128
) {
112-
const sections = json.sections
113-
for (const section of sections) {
114-
if (section?.model?.person_image_url) {
115-
author.image = section.model.person_image_url.replace(/\._.*_/, '')
116-
}
117-
for (const item of section.model.items || []) {
118-
if (item.view.template === 'ExpandableText' && item.model.expandable_content) {
119-
author.description = item.model.expandable_content?.value?.replace('\t', '').trim() || ''
120-
}
121-
}
122-
author.fetchedDescription = true
129+
if (json.bio) {
130+
author.description = json.bio.replace('\t', '').trim() || ''
123131
}
124-
if (json.page_details?.model?.title) {
125-
author.name = json.page_details?.model?.title?.replace('\t', '').trim() || ''
132+
author.fetchedDescription = true
133+
if (json.profile_image_url) {
134+
author.image = json.profile_image_url
135+
}
136+
if (json.name) {
137+
author.name = json.name?.replace('\t', '').trim() || ''
126138
}
127139
if (!author.region) {
128140
author.region = payload.region
129141
}
130142
author.asin = payload.asin?.replace('\t', '').trim() || ''
131143

144+
if (!json.name) {
145+
throw new NotFoundException()
146+
}
147+
132148
return await retryOnUniqueViolation(async () => {
133149
const serializedAuthor = author.serialize()
134150
const { id, asin, region, name, ...rest } = serializedAuthor

0 commit comments

Comments
 (0)