You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wattpad-deno/src/classes/Story.ts

78 lines
2.4 KiB

1 year ago
import Chapter from "./Chapter.ts";
import User from "./User.ts";
1 year ago
import { Session, StoryJSON } from "../types.d.ts";
1 year ago
export default class Story {
1 year ago
#session: Session;
1 year ago
/**
* ID of the work
*/
id: string;
/**
* internal story JSON; access for more detailed information
*/
storyJSON!: StoryJSON;
/**
* the name of the work
*/
name = "";
/**
* a list of Chapters in the work
*/
chapters: Chapter[] = [];
/**
* a list of tags in the work
*/
tags: string[] = [];
/**
* the approximate date the work was created
*/
created?: Date;
/**
* the approximate date the work was last updated
*/
updated?: Date;
/**
* represents a work on AO3
* @param id the ID of the work
* @param body the HTML body of the work (in text)
* @param session an axiod session (used for fetching additional details)
*/
constructor(
1 year ago
id: string,
session: Session,
1 year ago
) {
this.#session = session;
1 year ago
this.id = id;
1 year ago
}
async init() {
this.storyJSON = await (await this.#session.get(
`/api/v3/stories/${this.id}`,
false,
{
params: new URLSearchParams({
drafts: "0",
mature: "1",
1 year ago
include_deleted: "1",
fields:
"id,title,length,createDate,modifyDate,voteCount,readCount,commentCount,url,promoted,sponsor,language,user,description,cover,highlight_colour,completed,isPaywalled,paidModel,categories,numParts,readingPosition,deleted,dateAdded,lastPublishedPart(createDate),tags,copyright,rating,story_text_url(text),,parts(id,title,voteCount,commentCount,videoId,readCount,photoUrl,createDate,modifyDate,length,voted,deleted,text_url(text),dedication,url,wordCount),isAdExempt,tagRankings",
1 year ago
}),
},
)).json() as StoryJSON;
1 year ago
this.name = this.storyJSON.title;
this.tags = this.storyJSON.tags;
this.created = new Date(this.storyJSON.createDate);
this.updated = new Date(this.storyJSON.modifyDate);
this.chapters = this.storyJSON.parts.map((part) =>
new Chapter(this.storyJSON.id, part, this.#session)
);
}
getAuthor() {
return new User(this.#session, this.storyJSON.user.name);
}
1 year ago
}