From b2937d3c9462ac1adacee2ba6aa80e80acf9650e Mon Sep 17 00:00:00 2001 From: neobooru <50623835+neobooru@users.noreply.github.com> Date: Wed, 5 Jul 2023 13:57:59 +0200 Subject: [PATCH] Find PNG version of posts on konachan + test fixes --- package.json | 2 +- src/engines/Moebooru.test.ts | 18 ++++++++++++++++++ src/engines/Moebooru.ts | 12 ++++++++++++ src/engines/Shimmie2.test.ts | 2 +- src/test/jest.d.ts | 1 - 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index f0e9ad6..f0d627a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "neo-scraper", - "version": "0.7.0", + "version": "0.7.1", "description": "Scrape a booru-like post object from the DOM.", "type": "module", "main": "dist/index.js", diff --git a/src/engines/Moebooru.test.ts b/src/engines/Moebooru.test.ts index 1f2bf65..33d9ad3 100644 --- a/src/engines/Moebooru.test.ts +++ b/src/engines/Moebooru.test.ts @@ -76,3 +76,21 @@ test("get konachan.com post 285782 with notes", async () => { ); expect(res.posts[0]).toHaveSource("https://www.pixiv.net/member_illust.php?mode=medium&illust_id=75747354"); }); + +test("get konachan.com post 358247 jpeg and png", async () => { + const pageUrl = "https://konachan.com/post/show/358247/3d-animal_ears-arknights-black_hair-blonde_hair-br"; + const res = await scrapeUrl(pageUrl); + expect(res.posts.length).toBe(2); + expect(res.posts[0]).toHaveTag("texas_(arknights)", "character"); + expect(res.posts[0]).toHaveResolution([6144, 3456]); + expect(res.posts[0].pageUrl).toBe(pageUrl); + expect(res.posts[0].contentUrl).toBe( + "https://konachan.com/image/56983a2166a13f4af963774ccd424f0e/Konachan.com%20-%20358247%203d%20arknights%20computer%20couch%20dress%20drink%20gloves%20gun%20halo%20hoodie%20necklace%20pantyhose%20red_eyes%20red_hair%20shirt%20shorts%20skirt%20tie%20twintails%20weapon%20wolfgirl.png" + ); + expect(res.posts[0]).toHaveSource("https://www.pixiv.net/en/artworks/108925945"); + + // JPEG variant + expect(res.posts[1].contentUrl).toBe( + "https://konachan.com/jpeg/56983a2166a13f4af963774ccd424f0e/Konachan.com%20-%20358247%203d%20arknights%20computer%20couch%20dress%20drink%20gloves%20gun%20halo%20hoodie%20necklace%20pantyhose%20red_eyes%20red_hair%20shirt%20shorts%20skirt%20tie%20twintails%20weapon%20wolfgirl.jpg" + ); +}); diff --git a/src/engines/Moebooru.ts b/src/engines/Moebooru.ts index e52c2a7..d337b34 100644 --- a/src/engines/Moebooru.ts +++ b/src/engines/Moebooru.ts @@ -110,6 +110,18 @@ export default class Moebooru extends ScrapeEngineBase { post.notes = createNotesFromMoebooruBoxes(document, boxSize); } + // Sometimes there is both a JPEG and a PNG version of a post. + // If this is the case, copy all the information from the JPEG + // version and only replace the contentUrl with the link to the PNG version. + // We want to add this PNG version before the other version, so that it appears on top. + const unchangedImgEl = document.querySelector("a.original-file-unchanged"); + if (unchangedImgEl && unchangedImgEl.href != post.contentUrl) { + const copy = Object.assign(new ScrapedPost(), post); + copy.contentUrl = unchangedImgEl.href; + result.tryAddPost(copy); + } + + // Add normal/non-png post. result.tryAddPost(post); return result; diff --git a/src/engines/Shimmie2.test.ts b/src/engines/Shimmie2.test.ts index 29b514e..5450f33 100644 --- a/src/engines/Shimmie2.test.ts +++ b/src/engines/Shimmie2.test.ts @@ -10,6 +10,6 @@ test("get rule34.paheal.net post 2505982", async () => { "http://law-zilla.tumblr.com/post/171249285285/finished-subdraw-37-kaisa-from-league-of-legends" ); expect(res.posts[0].contentUrl).toContain( - ".paheal.net/_images/15427ab5ed2cddbeef947e30f5c9cab6/2505982%20-%20Kai%27Sa%20League_of_Legends%20lawzilla.png" + ".paheal.net/_images/15427ab5ed2cddbeef947e30f5c9cab6/2505982%20-%20Kai%27Sa%20lawzilla%20League_of_Legends.png" ); }); diff --git a/src/test/jest.d.ts b/src/test/jest.d.ts index 22ff885..96be272 100644 --- a/src/test/jest.d.ts +++ b/src/test/jest.d.ts @@ -4,7 +4,6 @@ import { TagCategory } from "../BooruTypes.js"; declare global { namespace jest { interface Matchers { - // The `= undefined` part is incorrect according to VSCode, but it is actually required. toHaveTag(tagName: string, tagCategory?: TagCategory): CustomMatcherResult; toHaveNote(noteText: string): CustomMatcherResult; toHaveResolution(resolution: [number, number]): CustomMatcherResult;