forked from ruthenic/wattpad-deno
Add `User` class + other things I forgor
parent
a104fb0775
commit
1210e75ca1
@ -1,5 +1,9 @@
|
||||
{
|
||||
"deno.enable": true,
|
||||
"deno.unstable": true,
|
||||
"editor.formatOnSave": true
|
||||
"editor.formatOnSave": true,
|
||||
"[typescript]": {
|
||||
"editor.tabSize": 4,
|
||||
"editor.defaultFormatter": "denoland.vscode-deno"
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,14 @@
|
||||
# wattpad-deno
|
||||
|
||||
early library for accessing Wattpad using Deno
|
||||
|
||||
## Q&A
|
||||
|
||||
### Why?
|
||||
|
||||
Why not
|
||||
|
||||
### Docs?
|
||||
The API *should* be pretty self-explanitory, if anything through deno.land's built in docs generator
|
||||
|
||||
The API _should_ be pretty self-explanitory, if anything through deno.land's
|
||||
built in docs generator
|
||||
|
@ -0,0 +1,61 @@
|
||||
import Story from "./Story.ts";
|
||||
import { SearchResults, Session, UserJSON } from "../types.d.ts";
|
||||
|
||||
export default class User {
|
||||
#session: Session;
|
||||
#searchLimit: number;
|
||||
username: string;
|
||||
displayName?: string;
|
||||
description?: string;
|
||||
createDate?: Date;
|
||||
userJSON!: UserJSON;
|
||||
stories: Story[] = [];
|
||||
|
||||
constructor(session: Session, username: string, limit: number = 20) {
|
||||
this.#session = session;
|
||||
this.username = username;
|
||||
this.#searchLimit = limit;
|
||||
}
|
||||
|
||||
async init() {
|
||||
this.userJSON = await (await this.#session.get(
|
||||
`/api/v3/users/${this.username}`,
|
||||
false,
|
||||
{
|
||||
params: new URLSearchParams({
|
||||
fields:
|
||||
"username,description,avatar,name,email,genderCode,language,birthdate,verified,isPrivate,ambassador,is_staff,follower,following,backgroundUrl,votesReceived,numFollowing,numFollowers,createDate,followerRequest,website,facebook,twitter,followingRequest,numStoriesPublished,numLists,location,externalId,programs,showSocialNetwork,verified_email,has_accepted_latest_tos,email_reverification_status,highlight_colour,safety(isMuted,isBlocked),has_writer_subscription",
|
||||
}),
|
||||
},
|
||||
))
|
||||
.json();
|
||||
|
||||
this.displayName = this.userJSON.name;
|
||||
this.description = this.userJSON.description;
|
||||
this.createDate = new Date(this.userJSON.createDate);
|
||||
}
|
||||
|
||||
async updateStories(pageNum: number) {
|
||||
const res: SearchResults = await (await this.#session.get(
|
||||
`/v4/users/${this.username}/stories/published`,
|
||||
false,
|
||||
{
|
||||
params: new URLSearchParams({
|
||||
fields: "stories(id)",
|
||||
limit: this.#searchLimit.toString(),
|
||||
offset: (pageNum * this.#searchLimit).toString(),
|
||||
mature: "1",
|
||||
}),
|
||||
},
|
||||
)).json();
|
||||
|
||||
for (let i = 0; i < res.stories.length; i++) {
|
||||
const story = new Story(
|
||||
res.stories[i].id,
|
||||
this.#session,
|
||||
);
|
||||
|
||||
this.stories.push(story);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,10 +1,12 @@
|
||||
import Wattpad from "../src/classes/Wattpad.ts";
|
||||
import workTest from "./story.ts";
|
||||
import storyTest from "./story.ts";
|
||||
import chaptersTest from "./chapter.ts";
|
||||
import searchTest from "./search.ts";
|
||||
import userTest from "./user.ts";
|
||||
|
||||
const wattpad = new Wattpad();
|
||||
|
||||
workTest(wattpad);
|
||||
storyTest(wattpad);
|
||||
chaptersTest(wattpad);
|
||||
searchTest(wattpad);
|
||||
userTest(wattpad);
|
||||
|
@ -0,0 +1,33 @@
|
||||
import Wattpad from "../mod.ts";
|
||||
import type { User } from "../mod.ts";
|
||||
import { assert } from "https://deno.land/std@0.167.0/testing/asserts.ts";
|
||||
|
||||
export default function test(watt: Wattpad) {
|
||||
Deno.test("users", async (test) => {
|
||||
let user: User;
|
||||
await test.step("initialization", async () => {
|
||||
user = watt.getUser("DustyAngel47");
|
||||
await user.init();
|
||||
});
|
||||
|
||||
await test.step("metadata", () => {
|
||||
assert(
|
||||
user.username === "DustyAngel47",
|
||||
"incorrect username (somehow)",
|
||||
);
|
||||
assert(
|
||||
user.displayName === "DustyAngel47",
|
||||
"incorrect display name",
|
||||
);
|
||||
});
|
||||
|
||||
await test.step("stories", async () => {
|
||||
await user.updateStories(0);
|
||||
// FIXME: should be resistant to me publishing more than one fic
|
||||
assert(
|
||||
user.stories.find((story) => story.id === "327425279"),
|
||||
"incorrect stories found",
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in new issue