From f9e647113003078c995aa284f4f6568f107adbda Mon Sep 17 00:00:00 2001 From: Nithin <10778861+darthnithin@users.noreply.github.com> Date: Sun, 29 Dec 2024 16:13:44 -0800 Subject: [PATCH 1/6] Quick GetChild Implementation (git Fixed) --- src/tags/index.ts | 2 ++ src/tags/page-getters.ts | 12 ++++++++++++ types/entities.ts | 1 + 3 files changed, 15 insertions(+) diff --git a/src/tags/index.ts b/src/tags/index.ts index a959fa3..3304616 100644 --- a/src/tags/index.ts +++ b/src/tags/index.ts @@ -4,6 +4,7 @@ import { isCanonical, isCommon, getParentTags, + getChildTags, } from "./page-getters"; import { getTagId, getTagNameFromFeed } from "./works-feed-getters"; import { @@ -30,6 +31,7 @@ export const getTag = async ({ common: isCommon(tagPage), canonicalName: getCanonical(tagPage), parentTags: getParentTags(tagPage), + childTags: getChildTags(tagPage), }; }; diff --git a/src/tags/page-getters.ts b/src/tags/page-getters.ts index 5d5d443..04366fe 100644 --- a/src/tags/page-getters.ts +++ b/src/tags/page-getters.ts @@ -52,3 +52,15 @@ export const getParentTags = ($tagPage: TagPage) => { }); return parentTags; }; +export const getChildTags = ($tagPage: TagPage) => { + const childTags: string[] = []; + $tagPage(".child > div > ul > li").each((i, element) => { + + //console.log(i, $tagPage(element).children().first().text()); + let childTag = $tagPage(element).children("a").first().text(); + if (childTag != '') { + childTags.push(childTag); + } + }) + return childTags; +} diff --git a/types/entities.ts b/types/entities.ts index 0cdad10..f7886c9 100644 --- a/types/entities.ts +++ b/types/entities.ts @@ -18,6 +18,7 @@ export interface Tag { // common and cannot be filtered on. canonicalName: string | null; parentTags: string[]; + childTags: string[]; } export interface User { From f9bb38eb8aae3b9a9f12124f8d2804174036c92e Mon Sep 17 00:00:00 2001 From: Nithin <10778861+darthnithin@users.noreply.github.com> Date: Thu, 2 Jan 2025 16:27:39 -0800 Subject: [PATCH 2/6] working but slow implentation of tag categories --- src/tags/page-getters.ts | 10 ++++++---- types/entities.ts | 5 ++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/tags/page-getters.ts b/src/tags/page-getters.ts index 0009b2b..0d15e34 100644 --- a/src/tags/page-getters.ts +++ b/src/tags/page-getters.ts @@ -54,13 +54,15 @@ export const getParentTags = ($tagPage: TagPage) => { return parentTags; }; export const getChildTags = ($tagPage: TagPage) => { - const childTags: string[] = []; + const childTags: {tagName: string; Category: TagCategory }[] = []; $tagPage(".child > div > ul > li").each((i, element) => { - - //console.log(i, $tagPage(element).children().first().text()); let childTag = $tagPage(element).children("a").first().text(); if (childTag != '') { - childTags.push(childTag); + // parent and parent's parent is an element + let classcatagory = (element.parent?.parent as Element).attributes[0].value.split(' ')[0] + //console.log(classcatagory != 'freeforms' ? classcatagory : 'additional tags'); + let tc = (classcatagory != 'freeforms' ? classcatagory : 'additional tags') as TagCategory; + childTags.push({tagName: childTag, Category: tc}); } }) return childTags; diff --git a/types/entities.ts b/types/entities.ts index c5d2046..a1d5acf 100644 --- a/types/entities.ts +++ b/types/entities.ts @@ -18,7 +18,10 @@ export interface Tag { // common and cannot be filtered on. canonicalName: string | null; parentTags: string[]; - childTags: string[]; + childTags: Array<{ + tagName: string; + Category: TagCategory; + }>; subTags: Array<{ tagName: string; parentSubTag: string | null; From c77fccaeb3adfc10c3202898b9edc10d24b2f232 Mon Sep 17 00:00:00 2001 From: Nithin <10778861+darthnithin@users.noreply.github.com> Date: Thu, 2 Jan 2025 16:59:05 -0800 Subject: [PATCH 3/6] iterate over each div instead of doing the backwards traversal --- src/tags/page-getters.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/tags/page-getters.ts b/src/tags/page-getters.ts index 0d15e34..6a327cc 100644 --- a/src/tags/page-getters.ts +++ b/src/tags/page-getters.ts @@ -54,7 +54,9 @@ export const getParentTags = ($tagPage: TagPage) => { return parentTags; }; export const getChildTags = ($tagPage: TagPage) => { + const childTags: {tagName: string; Category: TagCategory }[] = []; + /* $tagPage(".child > div > ul > li").each((i, element) => { let childTag = $tagPage(element).children("a").first().text(); if (childTag != '') { @@ -65,6 +67,18 @@ export const getChildTags = ($tagPage: TagPage) => { childTags.push({tagName: childTag, Category: tc}); } }) + */ + + const divCategory = $tagPage(".child > div"); + divCategory.each((i, divelement) => { + let classcatagory = divelement.attribs?.class?.split(' ')[0]; + let tagcatagory = (classcatagory != 'freeforms' ? classcatagory : 'additional tags') as TagCategory; + $tagPage(divelement).find('ul > li a').each((_, aElement) => { + let childTag = $tagPage(aElement).text(); + (childTag != '') && childTags.push({tagName: childTag, Category: tagcatagory}); + }) + }) + return childTags; } From 18eb2e2d2122c98fd97bf8e0d4bf700ce9d24818 Mon Sep 17 00:00:00 2001 From: Nithin <10778861+darthnithin@users.noreply.github.com> Date: Thu, 2 Jan 2025 17:26:19 -0800 Subject: [PATCH 4/6] use cheerio map --- src/tags/page-getters.ts | 35 +++++++++-------------------------- 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/src/tags/page-getters.ts b/src/tags/page-getters.ts index 6a327cc..730a7da 100644 --- a/src/tags/page-getters.ts +++ b/src/tags/page-getters.ts @@ -54,32 +54,15 @@ export const getParentTags = ($tagPage: TagPage) => { return parentTags; }; export const getChildTags = ($tagPage: TagPage) => { - - const childTags: {tagName: string; Category: TagCategory }[] = []; - /* - $tagPage(".child > div > ul > li").each((i, element) => { - let childTag = $tagPage(element).children("a").first().text(); - if (childTag != '') { - // parent and parent's parent is an element - let classcatagory = (element.parent?.parent as Element).attributes[0].value.split(' ')[0] - //console.log(classcatagory != 'freeforms' ? classcatagory : 'additional tags'); - let tc = (classcatagory != 'freeforms' ? classcatagory : 'additional tags') as TagCategory; - childTags.push({tagName: childTag, Category: tc}); - } - }) - */ - - const divCategory = $tagPage(".child > div"); - divCategory.each((i, divelement) => { - let classcatagory = divelement.attribs?.class?.split(' ')[0]; - let tagcatagory = (classcatagory != 'freeforms' ? classcatagory : 'additional tags') as TagCategory; - $tagPage(divelement).find('ul > li a').each((_, aElement) => { - let childTag = $tagPage(aElement).text(); - (childTag != '') && childTags.push({tagName: childTag, Category: tagcatagory}); - }) - }) - - return childTags; + return $tagPage(".child > div").map((_, divElement) => { + const $div = $tagPage(divElement); + const className = $div.attr("class")?.split(' ')[0] ?? ""; + const category = (className !== "freeforms" ? className : "additional tags") as TagCategory; + return $div.find("ul > li a").map((_, aElement) => { + const childTag = $tagPage(aElement).text(); + return childTag ? { tagName: childTag, Category: category} : null; + }).get(); + }).get(); } export const getSubTags = ($tagPage: TagPage) => { From 388238a7010e72056b25a194b4f469d3391424fd Mon Sep 17 00:00:00 2001 From: Nithin <10778861+darthnithin@users.noreply.github.com> Date: Thu, 2 Jan 2025 17:33:41 -0800 Subject: [PATCH 5/6] fix capitlization --- src/tags/page-getters.ts | 2 +- types/entities.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tags/page-getters.ts b/src/tags/page-getters.ts index 730a7da..00d204c 100644 --- a/src/tags/page-getters.ts +++ b/src/tags/page-getters.ts @@ -60,7 +60,7 @@ export const getChildTags = ($tagPage: TagPage) => { const category = (className !== "freeforms" ? className : "additional tags") as TagCategory; return $div.find("ul > li a").map((_, aElement) => { const childTag = $tagPage(aElement).text(); - return childTag ? { tagName: childTag, Category: category} : null; + return childTag ? { tagName: childTag, category: category} : null; }).get(); }).get(); } diff --git a/types/entities.ts b/types/entities.ts index a1d5acf..0b3ee43 100644 --- a/types/entities.ts +++ b/types/entities.ts @@ -20,7 +20,7 @@ export interface Tag { parentTags: string[]; childTags: Array<{ tagName: string; - Category: TagCategory; + category: TagCategory; }>; subTags: Array<{ tagName: string; From 006ae211b6f4f9db5f22f2d0a64381b0897d5b8e Mon Sep 17 00:00:00 2001 From: Nithin <10778861+darthnithin@users.noreply.github.com> Date: Tue, 7 Jan 2025 19:48:18 -0800 Subject: [PATCH 6/6] Implement GetWorkAdult --- src/works/index.ts | 4 ++-- src/works/work-getters.ts | 5 +++++ tests/work-chapter.test.ts | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/works/index.ts b/src/works/index.ts index 06f35d6..9eddaca 100644 --- a/src/works/index.ts +++ b/src/works/index.ts @@ -14,6 +14,7 @@ import { getChapterName, getChapterSummary, getWorkAdditionalTags, + getWorkAdult, getWorkAuthors, getWorkBookmarkCount, getWorkCategory, @@ -66,8 +67,7 @@ export const getWork = async ({ language: getWorkLanguage(workPage), rating: getWorkRating(workPage), category: getWorkCategory(workPage), - // TODO: figure out how to get this - adult: false, + adult: getWorkAdult(workPage), fandoms: getWorkFandoms(workPage), tags: { warnings: getWorkWarnings(workPage), diff --git a/src/works/work-getters.ts b/src/works/work-getters.ts index a56ce5d..1f9588e 100644 --- a/src/works/work-getters.ts +++ b/src/works/work-getters.ts @@ -191,6 +191,11 @@ export const getWorkLocked = ($workPage: WorkPage) => { return !!$workPage("#signin > .heading").text(); }; +export const getWorkAdult = ($workPage: WorkPage): boolean => { + const adultCategories: WorkRatings[] = [WorkRatings.EXPLICIT, WorkRatings.MATURE, WorkRatings.NOT_RATED]; + return adultCategories.includes(getWorkRating($workPage)); +} + // Chapter-specific (must be multi-chapter fic) export const getChapterIndex = ( $workPage: WorkPage diff --git a/tests/work-chapter.test.ts b/tests/work-chapter.test.ts index 81d2dcf..c3cfcd7 100644 --- a/tests/work-chapter.test.ts +++ b/tests/work-chapter.test.ts @@ -22,7 +22,7 @@ describe("Fetches chapter of work", () => { language: "English", rating: "Not Rated", category: null, - adult: false, + adult: true, fandoms: ["Batman - All Media Types"], tags: { warnings: ["Creator Chose Not To Use Archive Warnings"],