Skip to content

Commit

Permalink
Merge pull request #1794 from openzim/downloading-articles-thumbnails
Browse files Browse the repository at this point in the history
Fixes to download articles thumbnails
  • Loading branch information
kelson42 authored Mar 6, 2023
2 parents 10dbdaf + f70e9b1 commit 5470f6a
Showing 1 changed file with 3 additions and 33 deletions.
36 changes: 3 additions & 33 deletions src/Downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ class Downloader {
let finalProcessedResp: QueryMwRet

while (true) {
const queryOpts = {
const queryOpts: KVS<any> = {
...this.getArticleQueryOpts(shouldGetThumbnail, true),
titles: articleIds.join('|'),
...(this.mwCapabilities.coordinatesAvailable ? { colimit: 'max' } : {}),
Expand All @@ -285,9 +285,7 @@ class Downloader {
let processedResponse = resp.query ? normalizeMwResponse(resp.query) : {}
if (resp.continue) {
continuation = resp.continue
const relevantDetails = this.stripNonContinuedProps(processedResponse)

finalProcessedResp = finalProcessedResp === undefined ? relevantDetails : deepmerge(finalProcessedResp, relevantDetails)
finalProcessedResp = finalProcessedResp === undefined ? processedResponse : deepmerge(finalProcessedResp, processedResponse)
} else {
if (this.mw.getCategories) {
processedResponse = await this.setArticleSubCategories(processedResponse)
Expand Down Expand Up @@ -344,9 +342,7 @@ class Downloader {
if (!queryComplete) {
queryContinuation = resp['query-continue']

const relevantDetails = this.stripNonContinuedProps(processedResponse)

finalProcessedResp = finalProcessedResp === undefined ? relevantDetails : deepmerge(finalProcessedResp, relevantDetails)
finalProcessedResp = finalProcessedResp === undefined ? processedResponse : deepmerge(finalProcessedResp, processedResponse)
} else {
if (this.mw.getCategories) {
processedResponse = await this.setArticleSubCategories(processedResponse)
Expand Down Expand Up @@ -438,32 +434,6 @@ class Downloader {
return `${isMainPage ? this.baseUrlForMainPage : this.baseUrl}${encodeURIComponent(articleId)}`
}

private stripNonContinuedProps(articleDetails: QueryMwRet, cont: QueryContinueOpts | ContinueOpts = {}): QueryMwRet {
const propsMap: KVS<string[]> = {
pageimages: ['thumbnail', 'pageimage'],
coordinates: ['coordinates'],
categories: ['categories'],
}
const keysToKeep: string[] = ['subCategories', 'revisions', 'redirects'].concat(Object.keys(cont).reduce((acc, key) => acc.concat(propsMap[key] || []), []))
const items = Object.entries(articleDetails).map(([aId, detail]) => {
const newDetail = keysToKeep.reduce((acc, key) => {
const val = (detail as any)[key]
if (!val) {
return acc
} else {
return {
...acc,
[key]: val,
}
}
}, {})
return [aId, newDetail]
})
return items.reduce((acc, [key, detail]: any[]) => {
return { ...acc, [key]: detail }
}, {})
}

private static handleMWWarningsAndErrors(resp: MwApiResponse): void {
if (resp.warnings) logger.warn(`Got warning from MW Query ${JSON.stringify(resp.warnings, null, '\t')}`)
if (resp.error?.code === DB_ERROR) throw new Error(`Got error from MW Query ${JSON.stringify(resp.error, null, '\t')}`)
Expand Down

0 comments on commit 5470f6a

Please sign in to comment.