Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
malik1004x authored and kelson42 committed Jul 19, 2024
1 parent 10db05e commit 5ee8a15
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 33 deletions.
42 changes: 37 additions & 5 deletions src/Downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ imageminOptions.set('default', new Map())
imageminOptions.set('webp', new Map())

imageminOptions.get('default').set('image/png', {
plugins: [(imageminPngquant as any)({ speed: 3, strip: true, dithering: 0 }), imageminAdvPng({ optimizationLevel: 4, iterations: 5 })],
plugins: [
(imageminPngquant as any)({ speed: 3, strip: true, dithering: 0 }),
imageminAdvPng({
optimizationLevel: 4,
iterations: 5,
}),
],
})
imageminOptions.get('default').set('image/jpeg', {
plugins: [imageminJpegoptim({ max: 60, stripAll: true })],
Expand Down Expand Up @@ -81,6 +87,7 @@ export const defaultStreamRequestOptions: AxiosRequestConfig = {
}

type URLDirector = WikimediaDesktopURLDirector | WikimediaMobileURLDirector | VisualEditorURLDirector | RestApiURLDirector

/**
* Downloader is a class providing content retrieval functionalities for both Mediawiki and S3 remote instances.
*/
Expand Down Expand Up @@ -258,7 +265,13 @@ class Downloader {
return finalProcessedResp
}

public async getArticleDetailsNS(ns: number, gapcontinue = ''): Promise<{ gapContinue: string; articleDetails: QueryMwRet }> {
public async getArticleDetailsNS(
ns: number,
gapcontinue = '',
): Promise<{
gapContinue: string
articleDetails: QueryMwRet
}> {
let queryContinuation: QueryContinueOpts
let finalProcessedResp: QueryMwRet
let gCont: string = null
Expand Down Expand Up @@ -366,7 +379,13 @@ class Downloader {
})
}

public async downloadContent(_url: string, retry = true): Promise<{ content: Buffer | string; responseHeaders: any }> {
public async downloadContent(
_url: string,
retry = true,
): Promise<{
content: Buffer | string
responseHeaders: any
}> {
if (!_url) {
throw new Error(`Parameter [${_url}] is not a valid url`)
}
Expand Down Expand Up @@ -612,7 +631,16 @@ class Downloader {
handler(err)
}

private async getSubCategories(articleId: string, continueStr = ''): Promise<Array<{ pageid: number; ns: number; title: string }>> {
private async getSubCategories(
articleId: string,
continueStr = '',

Check warning on line 636 in src/Downloader.ts

View check run for this annotation

Codecov / codecov/patch

src/Downloader.ts#L636

Added line #L636 was not covered by tests
): Promise<
Array<{
pageid: number
ns: number
title: string
}>
> {
const apiUrlDirector = new ApiURLDirector(MediaWiki.actionApiUrl.href)

const { query, continue: cont } = await this.getJSON<any>(apiUrlDirector.buildSubCategoriesURL(articleId, continueStr))
Expand Down Expand Up @@ -690,7 +718,11 @@ class Downloader {
jsConfigVars = jsConfigVars.replace('nosuchaction', 'view') // to replace the wgAction config that is set to 'nosuchaction' from api but should be 'view'

// Download mobile page dependencies only once
if ((await MediaWiki.hasWikimediaMobileApi(this.loginCookie)) && this.wikimediaMobileJsDependenciesList.length === 0 && this.wikimediaMobileStyleDependenciesList.length === 0) {
if (
(await MediaWiki.hasWikimediaMobileApi(this.loginCookie)) &&
this.wikimediaMobileJsDependenciesList.length === 0 &&
this.wikimediaMobileStyleDependenciesList.length === 0
) {
try {
// TODO: An arbitrary title can be placed since all Wikimedia wikis have the same mobile offline resources
const mobileModulesData = await this.getJSON<any>(`${MediaWiki.mobileModulePath}Test`)
Expand Down
30 changes: 14 additions & 16 deletions src/util/saveArticles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,17 +247,23 @@ export async function saveArticles(zimCreator: ZimCreator, downloader: Downloade
let articlesRenderer
if (forceRender) {
// All articles and main page will use the same renderer if 'forceRender' is specified
const renderer = await rendererBuilder.createRenderer({
renderType: 'specific',
renderName: forceRender,
}, downloader.loginCookie)
const renderer = await rendererBuilder.createRenderer(
{
renderType: 'specific',
renderName: forceRender,
},
downloader.loginCookie,
)
mainPageRenderer = renderer
articlesRenderer = renderer
} else {
mainPageRenderer = await rendererBuilder.createRenderer({ renderType: 'desktop' }, downloader.loginCookie)
articlesRenderer = await rendererBuilder.createRenderer({
renderType: hasWikimediaMobileApi ? 'mobile' : 'auto',
}, downloader.loginCookie)
articlesRenderer = await rendererBuilder.createRenderer(

Check warning on line 261 in src/util/saveArticles.ts

View check run for this annotation

Codecov / codecov/patch

src/util/saveArticles.ts#L260-L261

Added lines #L260 - L261 were not covered by tests
{
renderType: hasWikimediaMobileApi ? 'mobile' : 'auto',

Check warning on line 263 in src/util/saveArticles.ts

View check run for this annotation

Codecov / codecov/patch

src/util/saveArticles.ts#L263

Added line #L263 was not covered by tests
},
downloader.loginCookie,
)
}
downloader.setUrlsDirectors(mainPageRenderer, articlesRenderer)

Expand Down Expand Up @@ -301,15 +307,7 @@ export async function saveArticles(zimCreator: ZimCreator, downloader: Downloade

rets = await downloader.getArticle(downloader.webp, _moduleDependencies, articleId, articleDetailXId, renderer, articleUrl, dump, articleDetail, isMainPage)

for (const {
articleId,
displayTitle: articleTitle,
html: finalHTML,
mediaDependencies,
moduleDependencies,
staticFiles,
subtitles
} of rets) {
for (const { articleId, displayTitle: articleTitle, html: finalHTML, mediaDependencies, moduleDependencies, staticFiles, subtitles } of rets) {
if (!finalHTML) {
logger.warn(`No HTML returned for article [${articleId}], skipping`)
continue
Expand Down
33 changes: 21 additions & 12 deletions test/unit/renderers/renderer.builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,40 @@ describe('RendererBuilder', () => {
it('should create a WikimediaDesktopRenderer for desktop mode', async () => {
const { MediaWiki } = await setupScrapeClasses() // en wikipedia

const renderer = await rendererBuilder.createRenderer({
MediaWiki,
renderType: 'desktop',
} as RendererBuilderOptions, '')
const renderer = await rendererBuilder.createRenderer(
{
MediaWiki,
renderType: 'desktop',
} as RendererBuilderOptions,
'',
)
expect(renderer).toBeInstanceOf(WikimediaDesktopRenderer)
})

it('should create a WikimediaDesktopRenderer for auto mode for en wikipedia', async () => {
const { MediaWiki } = await setupScrapeClasses() // en wikipedia

const renderer = await rendererBuilder.createRenderer({
MediaWiki,
renderType: 'auto',
} as RendererBuilderOptions, '')
const renderer = await rendererBuilder.createRenderer(
{
MediaWiki,
renderType: 'auto',
} as RendererBuilderOptions,
'',
)
expect(renderer).toBeInstanceOf(WikimediaDesktopRenderer)
})

it('should throw error for unknown render mode', async () => {
const { MediaWiki } = await setupScrapeClasses() // en wikipedia

expect(async () => {
await rendererBuilder.createRenderer({
MediaWiki,
renderType: 'unknownMode' as any,
} as RendererBuilderOptions, '')
await rendererBuilder.createRenderer(
{
MediaWiki,
renderType: 'unknownMode' as any,
} as RendererBuilderOptions,
'',
)
}).rejects.toThrow('Unknown render: unknownMode')
})

Expand Down

0 comments on commit 5ee8a15

Please sign in to comment.