From c5dcf380570031ad20bc400f6db85c49e1d04abd Mon Sep 17 00:00:00 2001 From: TheNetsky <56271887+TheNetsky@users.noreply.github.com> Date: Sun, 22 Sep 2024 12:04:04 +0200 Subject: [PATCH] Fix Asura stuff --- package.json | 4 +- src/AsuraScans/AsuraScans.ts | 79 +++++++++++++++++------------- src/AsuraScans/AsuraScansParser.ts | 7 ++- 3 files changed, 51 insertions(+), 39 deletions(-) diff --git a/package.json b/package.json index 1219e90..8795ecc 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,6 @@ "@typescript-eslint/parser": "^5.57.1", "eslint": "^7.27.0", "eslint-plugin-modules-newline": "^0.0.6", - "simple-url": "^1.1.8", "typescript": "^4.9.5" }, "dependencies": { @@ -28,6 +27,7 @@ "cheerio": "^1.0.0-rc.12", "entities": "^4.4.0", "nodemon": "^2.0.22", - "ts-node": "^10.2.1" + "ts-node": "^10.2.1", + "url": "^0.11.4" } } diff --git a/src/AsuraScans/AsuraScans.ts b/src/AsuraScans/AsuraScans.ts index d04a6bc..c159ba5 100644 --- a/src/AsuraScans/AsuraScans.ts +++ b/src/AsuraScans/AsuraScans.ts @@ -21,6 +21,8 @@ import { TagSection } from '@paperback/types' +import { parse } from 'url' + import { AsuraScansParser } from './AsuraScansParser' import { URLBuilder } from './UrlBuilder' import { @@ -42,13 +44,11 @@ import { SourceStateManager } from '@paperback/types/lib' -import simpleUrl from 'simple-url' - const ASURASCANS_DOMAIN = 'https://asuracomic.net' const ASURASCANS_API_DOMAIN = 'https://gg.asuracomic.net' export const AsuraScansInfo: SourceInfo = { - version: '4.1.8', + version: '4.2.0', name: 'AsuraScans', description: 'Extension that pulls manga from AsuraScans', author: 'Seyden', @@ -117,26 +117,32 @@ export class AsuraScans implements ChapterProviding, HomePageSectionsProviding, } } - const path: any = simpleUrl.parse(request.url, true) - if (!path.protocol || path.protocol == 'http') { - path.protocol = 'https' - request.url = simpleUrl.create(path) + const urlObject = parse(request.url) + + if (!urlObject.protocol || urlObject.protocol === 'http:') { + urlObject.protocol = 'https:' + request.url = urlObject.toString() } - if (path.host.includes('localhost')) { - const url: string = await this.getBaseUrl() - path.host = simpleUrl.parse(url, true).host - request.url = simpleUrl.create(path) + + if (urlObject.hostname?.includes('localhost')) { + const baseUrl = await this.getBaseUrl() + const baseHost = parse(baseUrl).host ?? '' + urlObject.host = baseHost + request.url = urlObject.toString() } + if (isImgLink(request.url)) { const overrideUrl: string = await this.stateManager.retrieve('Domain') - if (overrideUrl && overrideUrl != this.baseUrl) { - const basePath: any = simpleUrl.parse(this.baseUrl, true) - const overridePath: any = simpleUrl.parse(overrideUrl, true) - if (path.host.includes(basePath.host) || path.host.includes(overridePath.host)) { - path.host = overridePath.host - request.url = simpleUrl.create(path) + + if (overrideUrl && overrideUrl !== this.baseUrl) { + const basePath = parse(this.baseUrl) + const overridePath = parse(overrideUrl) + + if (urlObject.host?.includes(basePath.host ?? '') || urlObject.host?.includes(overridePath.host ?? '')) { + urlObject.host = overridePath.host + request.url = urlObject.toString() } } } @@ -260,25 +266,22 @@ export class AsuraScans implements ChapterProviding, HomePageSectionsProviding, for (const key in this.mangaDataRequests) { const tempRequest = this.mangaDataRequests[key] - if (tempRequest!.expires < Date.now()) { + if (tempRequest?.expires && tempRequest.expires < Date.now()) { delete this.mangaDataRequests[key] } } this.mangaDataRequests[mangaId] = { expires: Date.now() + 5000, - data: new Promise(async (resolve, reject) => { - try { - const result = await this.getMangaData(mangaId) - resolve(result) - } - catch (e) { - reject(e) - } + data: new Promise((resolve, reject) => { + this.getMangaData(mangaId) + .then((result) => resolve(result)) + .catch((e) => reject(e)) }) } - return this.mangaDataRequests[mangaId]!.data + + return this.mangaDataRequests[mangaId]?.data } async getMangaSlug(mangaId: string): Promise { @@ -289,7 +292,7 @@ export class AsuraScans implements ChapterProviding, HomePageSectionsProviding, await this.stateManager.store(`${mangaId}:slug`, link) } - // @ts-ignore + //@ts-expect-error Force async function async getMangaShareUrl(mangaId: string): Promise { const slug = await this.getMangaSlug(mangaId) if (!slug) { @@ -419,7 +422,7 @@ export class AsuraScans implements ChapterProviding, HomePageSectionsProviding, .addQueryParameter('page', page.toString()) if (query?.title) { - urlBuilder = urlBuilder.addQueryParameter('name', encodeURIComponent(query?.title.replace(/[’‘´`'\-][a-z]*/g, '%') ?? '')) + urlBuilder = urlBuilder.addQueryParameter('name', encodeURIComponent(query?.title.replace(/[’‘´`'-][a-z]*/g, '%') ?? '')) } urlBuilder = urlBuilder @@ -460,10 +463,20 @@ export class AsuraScans implements ChapterProviding, HomePageSectionsProviding, continue } - promises.push(new Promise(async () => { - section.section.items = await this.parser.parseHomeSection($, section, this) - sectionCallback(section.section) - })) + promises.push( + new Promise((resolve) => { + this.parser.parseHomeSection($, section, this) + .then((items) => { + section.section.items = items + sectionCallback(section.section) + resolve() // Resolve once the work is done + }) + .catch((error) => { + throw new Error(error) + }) + }) + ) + } // Make sure the function completes diff --git a/src/AsuraScans/AsuraScansParser.ts b/src/AsuraScans/AsuraScansParser.ts index c249fce..df1b0f0 100644 --- a/src/AsuraScans/AsuraScansParser.ts +++ b/src/AsuraScans/AsuraScansParser.ts @@ -14,7 +14,6 @@ import { import entities = require('entities') import { - FilterItem, Filters } from './AsuraScansInterfaces' @@ -44,7 +43,7 @@ export class AsuraScansParser { await source.setMangaSlug(mangaId, `series/${slug}`) } - const rawStatus = comicObj.comic.status.name.trim() + const rawStatus = comicObj.comic?.status?.name?.trim() ?? '' let status switch (rawStatus.toLowerCase()) { case source.manga_StatusTypes.DROPPED.toLowerCase(): @@ -190,8 +189,8 @@ export class AsuraScansParser { { id: 'chapters:250', label: '+250' } ] - const createTags = (filterItems: FilterItem[], prefix: string): Tag[] => { - return filterItems.map(item => ({ + const createTags = (filterItems: any, prefix: string): Tag[] => { + return filterItems.map((item: { id: any; value: any; name: any }) => ({ id: `${prefix}:${item.id ?? item.value}`, // Use `id` or `value` for `order` items label: item.name }))