-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathchapter-getters.ts
More file actions
55 lines (51 loc) · 1.86 KB
/
chapter-getters.ts
File metadata and controls
55 lines (51 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { Author, Chapter } from "types/entities";
import { getWorkDetailsFromUrl, getWorkUrl } from "src/urls";
import { ChapterIndexPage } from "src/page-loaders";
const TITLE_SEPARATOR = ". ";
export const getChaptersList = ($chapterIndexPage: ChapterIndexPage): Chapter[] => {
//return chapters;
return $chapterIndexPage("ol.index > li").map((index, li) => {
const link = $chapterIndexPage(li).find("a")[0];
const chapterText = $chapterIndexPage(link).text();
const { workId, chapterId } = getWorkDetailsFromUrl({
url: link.attribs.href,
});
const title = chapterText.substring(
chapterText.indexOf(TITLE_SEPARATOR) + TITLE_SEPARATOR.length
);
const dateNode = $chapterIndexPage(
$chapterIndexPage(li).find(".datetime")[0]
);
return {
id: chapterId!,
workId,
index: index + 1,
title,
// Remove parenthesis from the date
publishedAt: dateNode.text().replace(/[\(\)]/g, ""),
// We rebuild the url so it gets the full path
url: getWorkUrl({ workId, chapterId }),
}
}).get();
};
export const getWorkTitle = ($chapterIndexPage: ChapterIndexPage) => {
return $chapterIndexPage(".works-navigate h2 a[href^='/works/']").text();
};
export const getWorkAuthors = ($chapterIndexPage: ChapterIndexPage): Author[] => {
const authors: Author[] = [];
const authorNode = $chapterIndexPage(".works-navigate h2 a[rel='author']");
if (authorNode.text().trim() === "Anonymous") {
return [{ username: "Anonymous", pseud: "Anonymous", anonymous: true }];
}
return authorNode.length !== 0
? authorNode.map((_, element) => {
const url = element.attribs.href;
const [, username, pseud] = url.match(/users\/(.+)\/pseuds\/(.+)/)!;
return {
username: username,
pseud: decodeURI(pseud),
anonymous: false,
}
}).get()
: [] as Author[];
};