Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
Fixed #4 - incorrect image suffix loading for ksk
Browse files Browse the repository at this point in the history
  • Loading branch information
WaltersAsh committed Aug 13, 2023
1 parent 066b98f commit 44ef963
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Koushoku/Koushoku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
} from './KoushokuParser';

export const KoushokuInfo: SourceInfo = {
version: '1.0.3',
version: '1.0.4',
name: 'Koushoku',
icon: 'icon.png',
author: 'WaltersAsh',
Expand Down
34 changes: 18 additions & 16 deletions src/Koushoku/KoushokuParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import entities = require('entities');

export const DOMAIN = 'https://ksk.moe';
const REGEX_PAGE = /[0-9]+(.(jpg|png))/g;

export async function getTags(requestManager: RequestManager, cheerio: CheerioAPI): Promise<Tag[]> {
const request = App.createRequest({
Expand Down Expand Up @@ -125,26 +126,27 @@ export async function getPages(id: string, requestManager: RequestManager, cheer
const length = parseInt($('span:contains("Pages")').text().split(' ')[0] ?? '');
const urlPieces = $('img').first().attr('src')?.split('/') ?? '';
const suffix = urlPieces[urlPieces?.length - 1] ?? '';

const pagesRequest = App.createRequest({
url: `${DOMAIN}/read${id.slice(7)}/1`,
method: 'GET'
});
const pagesData = await requestManager.schedule(pagesRequest, 1);
const $$ = cheerio.load(pagesData.data as string, {xmlMode: true});

// Image format is not guranteed
// const imageFormat = suffix.slice(suffix.length, 3) ? 'jpg' : 'png';
const pageNumFormat = suffix.split('.')[0]?.length;

if (pageNumFormat === 2) {
for (let i = 1; i < length + 1; i++) {
// Pages start from 01, 02, 03..09, 10, 11...
const imageLink = i < 10 ? `${DOMAIN}/resampled/${imageId}/0${i}.png`: `${DOMAIN}/resampled/${imageId}/${i}.png`;
const testedImageLink = await testImageLink(imageLink, requestManager);
pages.push(testedImageLink);
}
} else if (pageNumFormat === 3) {
for (let i = 1; i < length + 1; i++) {
// Pages start from 001, 002..010, 011..100, 101...
let imageLink = i < 10 ? `${DOMAIN}/resampled/${imageId}/00${i}.png`: `${DOMAIN}/resampled/${imageId}/0${i}.png`;
if (i > 99) {
imageLink = `${DOMAIN}/resampled/${imageId}/${i}.png`;
}
const testedImageLink = await testImageLink(imageLink, requestManager);
pages.push(testedImageLink);
// Extract page nums from script
const pageNums = $$('script[type$=javascript]').last().toString();
const unsortedPageNumsList = pageNums.matchAll(REGEX_PAGE);
const pageNumsList = [...new Set(Array.from(unsortedPageNumsList, x => x[0]))];

if (pageNumFormat === 2 || pageNumFormat === 3) {
for (let i = 0; i < length; i++) {
const imageLink = `${DOMAIN}/resampled/${imageId}/${pageNumsList[i]}`;
pages.push(imageLink);
}
} else {
const urlFormat = urlPieces[urlPieces.length - 1];
Expand Down

0 comments on commit 44ef963

Please sign in to comment.