Skip to content

Commit

Permalink
api(giantbomb): support show more
Browse files Browse the repository at this point in the history
  • Loading branch information
gqgs committed Aug 4, 2023
1 parent 08447a5 commit 2014fc4
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ export abstract class API<APIResult> {
return results.filter(result => valid_results.has(result))
}

// eslint-disable-next-line
public async showMore(tab: string, selected: SearchResult): Promise<SearchResult[]> {
public async showMore({} : { tab: string, selected: SearchResult }): Promise<SearchResult[]> {
return []
}
}
31 changes: 27 additions & 4 deletions src/api/giantbomb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,51 @@ interface APIResult {

interface Result {
id: number
guid: string
name: string
image: {
original_url: string
}
image: Image
}

interface Image {
original_url: string
}

export default class GiantBomb extends API<APIResult> {
readonly has_show_more = true
readonly name = "giantbomb"
readonly tabs = ["game", "character"]

fetchURL(tab: string, query: string): { url: string } {
return {
url: proxyImage(`https://www.giantbomb.com/api/search/?api_key=${api_key}&resources=${tab}&query=${encodeURI(query)}&format=json&field_list=id,name,image`)
url: proxyImage(`https://www.giantbomb.com/api/search/?api_key=${api_key}&resources=${tab}&query=${encodeURI(query)}&format=json&field_list=id,name,image,guid`)
}
}
processResult(result: APIResult): SearchResult[] {
return (result?.results ?? []).map((result: Result) => {
return {
guid: result.guid,
mal_id: result.id,
title: result.name,
image_url: proxyImage(result.image.original_url)
}
})
}
async showMore({ selected } : { selected: SearchResult}): Promise<SearchResult[]> {
const id = ++this.last_id
const resp = await fetch(proxyImage(`https://www.giantbomb.com/api/images/${selected.guid}/?api_key=${api_key}&format=json&limit=30`))
const result = await resp.json()
if (this.last_id > id) return []
const image_set = new Set()
const fetch_result: SearchResult[] = (result.results ?? []).map((image: Image) => {
return {
title: selected.title,
image_url: proxyImage(image.original_url)
}
}).filter((result: SearchResult) => {
const is_duplicated = image_set.has(result.image_url)
image_set.add(result.image_url)
return !is_duplicated
})
return fetch_result
}
}
2 changes: 1 addition & 1 deletion src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const changeApi = async (newApi: string): Promise<void> => {

const showMore = async (tab: string): Promise<void> => {
if (selected.value == null) return
results.value = await api.showMore(tab, selected.value)
results.value = await api.showMore({ tab, selected: selected.value })
showing_more.value = true
}

Expand Down
2 changes: 1 addition & 1 deletion src/api/jikan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default class Jikan extends API<APIResult> {
}
})
}
async showMore(tab: string, selected: SearchResult): Promise<SearchResult[]> {
async showMore({ tab, selected } : { tab: string, selected: SearchResult }): Promise<SearchResult[]> {
tab = this.denormalizeTab(tab)
const id = ++this.last_id
const resp = await fetch(`https://api.jikan.moe/v4/${tab}/${selected.mal_id}/pictures`)
Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface Update {
}

export interface SearchResult {
guid?: string
mal_id: number
title: string
image_url: string
Expand Down

0 comments on commit 2014fc4

Please sign in to comment.