From f25aa10c78c8356e37c4dcf189300df6071b61a3 Mon Sep 17 00:00:00 2001 From: Justin Joe Date: Sun, 15 Oct 2023 15:40:43 +1300 Subject: [PATCH] Fix ksk --- src/Koushoku/Koushoku.ts | 21 +++++++++-- src/Koushoku/KoushokuParser.ts | 64 +++++++++++++++------------------- 2 files changed, 47 insertions(+), 38 deletions(-) diff --git a/src/Koushoku/Koushoku.ts b/src/Koushoku/Koushoku.ts index 1a909cd..252cfc3 100644 --- a/src/Koushoku/Koushoku.ts +++ b/src/Koushoku/Koushoku.ts @@ -26,6 +26,7 @@ import { getPages, getTags, isLastPage, + CloudFlareError } from './KoushokuParser'; export const KoushokuInfo: SourceInfo = { @@ -43,7 +44,7 @@ export const KoushokuInfo: SourceInfo = { type: BadgeColor.RED } ], - intents: SourceIntents.MANGA_CHAPTERS | SourceIntents.HOMEPAGE_SECTIONS + intents: SourceIntents.MANGA_CHAPTERS | SourceIntents.HOMEPAGE_SECTIONS | SourceIntents.CLOUDFLARE_BYPASS_REQUIRED }; export class Koushoku implements SearchResultsProviding, MangaProviding, ChapterProviding, HomePageSectionsProviding { @@ -91,6 +92,7 @@ export class Koushoku implements SearchResultsProviding, MangaProviding, Chapter method: 'GET' }); const responseForRecentlyAdded = await this.requestManager.schedule(requestForRecentlyAdded, 1); + CloudFlareError(responseForRecentlyAdded.status); const $recentlyAdded = this.cheerio.load(responseForRecentlyAdded.data as string); const recentlyAddedAlbumsSection = App.createHomeSection({id: 'recent', title: 'Recent Updates', containsMoreItems: true, type: HomeSectionType.singleRowNormal}); @@ -118,6 +120,7 @@ export class Koushoku implements SearchResultsProviding, MangaProviding, Chapter param }); const response = await this.requestManager.schedule(request, 1); + CloudFlareError(response.status); const $ = this.cheerio.load(response.data as string); const albums = getAlbums($); @@ -141,7 +144,7 @@ export class Koushoku implements SearchResultsProviding, MangaProviding, Chapter tags: data.tags, author: data.artist, artist: data.artist, - desc: '' + desc: data.desc }) }); } @@ -173,7 +176,7 @@ export class Koushoku implements SearchResultsProviding, MangaProviding, Chapter let request; if (query.title) { request = App.createRequest({ - url: `${DOMAIN}/search?page/${searchPage}&q=${encodeURIComponent(query.title)}`, + url: `${DOMAIN}/search?page=${searchPage}&q=${encodeURIComponent(query.title)}`, method: 'GET' }); } else { @@ -183,6 +186,7 @@ export class Koushoku implements SearchResultsProviding, MangaProviding, Chapter }); } const response = await this.requestManager.schedule(request, 1); + CloudFlareError(response.status); const $ = this.cheerio.load(response.data as string); const albums = getAlbums($); @@ -193,4 +197,15 @@ export class Koushoku implements SearchResultsProviding, MangaProviding, Chapter metadata }); } + + async getCloudflareBypassRequestAsync(): Promise { + return App.createRequest({ + url: DOMAIN, + method: 'GET', + headers: { + 'referer': `${DOMAIN}/`, + 'user-agent': await this.requestManager.getDefaultUserAgent() + } + }); + } } \ No newline at end of file diff --git a/src/Koushoku/KoushokuParser.ts b/src/Koushoku/KoushokuParser.ts index 4867ec5..1b2892f 100644 --- a/src/Koushoku/KoushokuParser.ts +++ b/src/Koushoku/KoushokuParser.ts @@ -15,14 +15,16 @@ export async function getTags(requestManager: RequestManager, cheerio: CheerioAP method: 'GET' }); const data = await requestManager.schedule(request, 1); + CloudFlareError(data.status); const $ = cheerio.load(data.data as string); - const tagElements = $('main', 'main').children().toArray(); - + const tagElements = $('div.entry').toArray(); const tags: Tag[] = []; for (const element of tagElements) { - const id = $('a', element).attr('href') ?? ''; - const label = $('span', element).first().text() ?? ''; + const id = $('a', element).attr('href')?.slice(6) ?? ''; + const label = $('strong', element).first().text() ?? ''; + console.log(id); + console.log(label); tags.push(App.createTag({ id, label })); } @@ -37,12 +39,7 @@ export function getAlbums ($: CheerioStatic): PartialSourceManga[] { const image = $('img', album).attr('src') ?? ''; const id = $('a', album).attr('href') ?? ''; const title = $('a', album).attr('title') ?? ''; - const artist = $('span', album).first().text() ?? ''; - - console.log('ID ' + id); - console.log('Image ' + image); - console.log('Title ' + title); - console.log('Artist ' + artist); + const artist = $('span', album).last().text() ?? ''; if (!id || !title) { continue; @@ -66,39 +63,28 @@ export async function getGalleryData(id: string, requestManager: RequestManager, method: 'GET' }); const data = await requestManager.schedule(request, 1); + CloudFlareError(data.status); const $ = cheerio.load(data.data as string); - const title = $('h2', 'section#metadata').first().text(); + const title = $('h1.title').first().text(); const image = $('img').first().attr('src') ?? 'https://i.imgur.com/GYUxEX8.png'; - const artistSection = $('strong:contains("Artist")').parent(); - const artist = $('span', artistSection).first().text(); - - const tagsElement1 = $('strong:contains("Tag")').first().parent(); - const tagsElement2 = $('span', tagsElement1).toArray(); - - const tagsToRender: Tag[] = []; - for (const tag of tagsElement2) { - const label = $(tag).text(); - if (label.match(/^\d/)) continue; - - if (!label) { - continue; - } - tagsToRender.push({ id: `/tags/${encodeURIComponent(label)}`, label: label }); - } + const artist = $('a', 'tr.artists').text(); + const magazine = $('a', 'tr.magazines').text(); + const pages = $('td', 'tr.pages').last().text(); + const created = $('td', 'tr.created').last().text(); + const published = $('td', 'tr.published').last().text(); + const desc = 'Magazine: ' + magazine + '\n' + + 'Pages: ' + pages + '\n' + + 'Created: ' + created + '\n' + + 'Published: ' + published + '\n'; - const tagSections: TagSection[] = [App.createTagSection({ - id: '0', - label: 'Tags', - tags: tagsToRender.map(x => App.createTag(x)) - })]; return { id: id, titles: [entities.decodeHTML(title as string)], image: image, artist: artist, - tags: tagSections + desc: desc }; } @@ -110,18 +96,20 @@ export async function getPages(id: string, requestManager: RequestManager, cheer method: 'GET' }); let data = await requestManager.schedule(request, 1); + CloudFlareError(data.status); let $ = cheerio.load(data.data as string); const lengthText = $('span.total').text(); const length = parseInt(lengthText.substring(0, lengthText.length / 2)); let imageLink = $('img', 'main.page').attr('src') ?? ''; pages.push(imageLink); - for (let i = 1; i < length + 1; i++) { + for (let i = 2; i < length + 1; i++) { request = App.createRequest({ url: `${DOMAIN}/${id}/${i}`, method: 'GET' }); data = await requestManager.schedule(request, 1); + CloudFlareError(data.status); $ = cheerio.load(data.data as string); imageLink = $('img', 'main.page').attr('src') ?? ''; pages.push(imageLink); @@ -132,4 +120,10 @@ export async function getPages(id: string, requestManager: RequestManager, cheer export const isLastPage = (albums: PartialSourceManga[]): boolean => { return albums.length != 25; -}; \ No newline at end of file +}; + +export function CloudFlareError(status: number): void { + if (status == 503 || status == 403) { + throw new Error(`CLOUDFLARE BYPASS ERROR:\nPlease go to the homepage of <${DOMAIN}> and press the cloud icon.`); + } +} \ No newline at end of file