Skip to content

Commit 6fb25e4

Browse files
authored
Continue work from #87: Implement getWorkAdult (#169)
1 parent 060a336 commit 6fb25e4

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

src/works/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
getChapterName,
1717
getChapterSummary,
1818
getWorkAdditionalTags,
19+
getWorkAdult,
1920
getWorkAuthors,
2021
getWorkBookmarkCount,
2122
getWorkCategory,
@@ -85,8 +86,7 @@ export const getWork = async ({
8586
language: getWorkLanguage(workPage),
8687
rating: getWorkRating(workPage),
8788
category: getWorkCategory(workPage),
88-
// TODO: figure out how to get this
89-
adult: false,
89+
adult: getWorkAdult(workPage),
9090
fandoms: getWorkFandoms(workPage),
9191
tags: {
9292
warnings: getWorkWarnings(workPage),

src/works/work-getters.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
import type { Author, BasicSeries, WorkSummary } from "types/entities";
2-
import { WorkRatings, WorkCategory, WorkWarnings } from "types/entities";
3-
import { WorkPage } from "src/page-loaders";
1+
import {
2+
Author,
3+
BasicSeries,
4+
WorkCategory,
5+
WorkRatings,
6+
WorkSummary,
7+
WorkWarnings,
8+
} from "types/entities";
49
import { isValidArchiveId, parseArchiveId } from "src/utils";
510

11+
import { WorkPage } from "src/page-loaders";
12+
613
export const getWorkAuthors = ($workPage: WorkPage): Author[] => {
714
const authorLinks = $workPage("h3.byline a[rel='author']");
815
const authors: Author[] = [];
@@ -194,6 +201,15 @@ export const getWorkLocked = ($workPage: WorkPage) => {
194201
return !!$workPage("#signin > .heading").text();
195202
};
196203

204+
const ADULT_CATEGORIES = [
205+
WorkRatings.EXPLICIT,
206+
WorkRatings.MATURE,
207+
WorkRatings.NOT_RATED
208+
] as const satisfies ReadonlyArray<WorkRatings>;
209+
export const getWorkAdult = ($workPage: WorkPage): boolean => {
210+
return (ADULT_CATEGORIES as readonly WorkRatings[]).includes(getWorkRating($workPage));
211+
};
212+
197213
export const getWorkContentHtml = ($workPage: WorkPage): string => {
198214
let content = $workPage('.userstuff.module[role="article"]').html();
199215
// Works with multiple chapters have a different structure for the content
@@ -208,14 +224,14 @@ export const getWorkContentHtml = ($workPage: WorkPage): string => {
208224

209225
export const getWorkStartNotes = ($workPage: WorkPage): string | null => {
210226
const startNotes = $workPage(
211-
".chapter.preface #notes.notes.module .userstuff"
227+
".chapter.preface #notes.notes.module .userstuff",
212228
).html();
213229
return startNotes ? startNotes.trim() : null;
214230
};
215231

216232
export const getWorkEndNotes = ($workPage: WorkPage): string | null => {
217233
const endNotes = $workPage(
218-
".chapter.preface .end.notes.module .userstuff"
234+
".chapter.preface .end.notes.module .userstuff",
219235
).html();
220236
return endNotes ? endNotes.trim() : null;
221237
};
@@ -225,15 +241,16 @@ export const getWorkContentSummary = ($workPage: WorkPage): string | null => {
225241
return summary ? summary.trim() : null;
226242
};
227243

244+
// Chapter-specific (must be multi-chapter fic)
228245
export const getChapterIndex = (
229-
$workPage: WorkPage
246+
$workPage: WorkPage,
230247
): NonNullable<WorkSummary["chapterInfo"]>["index"] => {
231248
const index = $workPage("#chapters h3.title a").text();
232249
return index ? parseInt(index.trim().replace("Chapter ", "")) : -1;
233250
};
234251

235252
export const getChapterName = (
236-
$workPage: WorkPage
253+
$workPage: WorkPage,
237254
): NonNullable<WorkSummary["chapterInfo"]>["name"] => {
238255
const title = $workPage("#chapters h3.title").text().trim();
239256
// 2 characters is the length of query string

tests/work-chapter.test.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
import { describe, expect, it } from "vitest";
2+
13
import type { WorkSummary } from "types/entities";
24
import { getWork } from "src/index";
3-
import { describe, it, expect } from "vitest";
45

56
describe("Work Chapter/chapter", () => {
67
it("should use null for single-chapter work", async () => {
78
const work = await getWork({ workId: 168768 });
89

9-
expect(work.locked).toBeFalsy();
10+
expect(!work.locked).toBeTruthy();
1011

1112
expect(work).toMatchObject({
1213
id: 168768,
@@ -39,7 +40,7 @@ describe("Work Chapter/chapter", () => {
3940
chapters: { published: 1, total: 1 },
4041
complete: true,
4142
series: [],
42-
stats: { bookmarks: 2, comments: 1, hits: expect.any(Number), kudos: 45 },
43+
stats: { bookmarks: 2, comments: 1, hits: expect.any(Number), kudos: 46 },
4344
locked: false,
4445
});
4546
});
@@ -53,10 +54,10 @@ describe("Work Chapter/chapter", () => {
5354
expect(!work.locked).toBeTruthy();
5455

5556
expect(work.summary).toMatchInlineSnapshot(
56-
`"<p><b>A Modern Thedas AU</b>, in which Fen&apos;Harel and the Second Inquisitor tore down the Veil a thousand years ago, reshaping Thedas into something entirely new. Thedas now has modern technology powered by magic, and a society still plagued with problems that are all too familiar - issues of race, classism, and power.</p><p>Fenina Lavellan, a student at the College of Enchanters: New Haven, often escapes her reality by playing the MMORPG Dragon Age (set in the ancient past during the time of the Second Inquisition) and is part of the most powerful guild aptly named &quot;TheInquisition&quot; - a guild which has been running since the game was released. But when the guild discovers that they all live in the same city and decide to meet up, they unknowingly stumble into a plot to destroy their world as they know it. Can they navigate the difficulties of actually being social in the real world? Will their in-game skills translate into abilities that will actually help them in stopping a madman? Or is this the end of the world as they know it?</p>"`
57+
`"<p><b>A Modern Thedas AU</b>, in which Fen&apos;Harel and the Second Inquisitor tore down the Veil a thousand years ago, reshaping Thedas into something entirely new. Thedas now has modern technology powered by magic, and a society still plagued with problems that are all too familiar - issues of race, classism, and power.</p><p>Fenina Lavellan, a student at the College of Enchanters: New Haven, often escapes her reality by playing the MMORPG Dragon Age (set in the ancient past during the time of the Second Inquisition) and is part of the most powerful guild aptly named &quot;TheInquisition&quot; - a guild which has been running since the game was released. But when the guild discovers that they all live in the same city and decide to meet up, they unknowingly stumble into a plot to destroy their world as they know it. Can they navigate the difficulties of actually being social in the real world? Will their in-game skills translate into abilities that will actually help them in stopping a madman? Or is this the end of the world as they know it?</p>"`,
5758
);
5859
expect(work.chapterInfo?.summary).toMatchInlineSnapshot(
59-
`"<p>Fenina Lavellan&apos;s MMO guild discovers they all live closer than they thought and decides to meet up.</p>"`
60+
`"<p>Fenina Lavellan&apos;s MMO guild discovers they all live closer than they thought and decides to meet up.</p>"`,
6061
);
6162
});
6263
});
@@ -79,7 +80,7 @@ describe("Work Chapter/work", () => {
7980
language: "English",
8081
rating: "Not Rated",
8182
category: null,
82-
adult: false,
83+
adult: true,
8384
fandoms: ["Batman - All Media Types"],
8485
tags: {
8586
warnings: ["Creator Chose Not To Use Archive Warnings"],

0 commit comments

Comments
 (0)