major search api updates + fixes
ci/woodpecker/push/woodpecker Pipeline failed Details
ci/woodpecker/tag/woodpecker Pipeline failed Details

pull/1/head v0.1.0
Drake 1 year ago
parent 5ec80d8837
commit 21a475a531

@ -4,5 +4,5 @@ export default Wattpad;
import Chapter from "./src/classes/Chapter.ts";
import Search from "./src/classes/Search.ts";
import Story from "./src/classes/Story.ts";
const VERSION = "v0.0.4";
const VERSION = "v0.1.0";
export { Chapter, Search, Story, VERSION, Wattpad };

@ -2,22 +2,25 @@ import { SearchResults, Session } from "../types.d.ts";
import Work from "./Story.ts";
export interface SearchParameters {
query: string;
isTitle?: boolean;
query: string | string[];
type: "text" | "title" | "tag";
sort?: string;
limit?: number;
}
export interface PrivateSearchParameters {
query: string;
isTitle: boolean;
query: string | string[];
type: "text" | "title" | "tag";
limit: number;
sort: string;
}
export default class Search {
#opts: PrivateSearchParameters = {
query: "",
isTitle: false,
type: "text",
limit: 30,
sort: "hot",
};
#session: Session;
results: Work[] = [];
@ -28,17 +31,31 @@ export default class Search {
) {
this.#session = session;
Object.assign(this.#opts, opts);
switch (this.#opts.type) {
case "title":
this.#opts.query = "title:" + this.#opts.query;
break;
case "tag":
if (this.#opts.query.constructor === Array) {
this.#opts.query = "#" + this.#opts.query.join(" #");
}
break;
case "text":
default:
break;
}
}
async update(pageNum: number) {
this.results = [];
const res: SearchResults =
await (await this.#session.get("/v4/search/stories", true, {
await (await this.#session.get("/v4/stories", false, {
params: new URLSearchParams({
query: this.#opts.isTitle
? `title: ${this.#opts.query}`
: this.#opts.query,
fields: "stories(id)",
query: this.#opts.query as string,
filter: this.#opts.sort,
limit: this.#opts.limit.toString(),
offset: (pageNum * this.#opts.limit).toString(),
mature: "1",

@ -1,52 +0,0 @@
import { SearchResults, Session } from "../types.d.ts";
import Work from "./Story.ts";
export interface SearchParameters {
tags: string[];
limit?: number;
}
export interface PrivateSearchParameters {
tags: string[];
limit: number;
}
export default class Search {
#opts: PrivateSearchParameters = {
tags: [],
limit: 20,
};
#session: Session;
results: Work[] = [];
constructor(
opts: SearchParameters,
session: Session,
) {
this.#session = session;
Object.assign(this.#opts, opts);
}
async update(pageNum: number) {
this.results = [];
const res: SearchResults =
await (await this.#session.get("/v5/hotlist", true, {
params: new URLSearchParams({
tags: this.#opts.tags.join(","),
offset: (pageNum * this.#opts.limit).toString(),
limit: this.#opts.limit.toString(),
mature: "1",
}),
})).json();
for (let i = 0; i < res.stories.length; i++) {
const work = new Work(
res.stories[i].id,
this.#session,
);
this.results.push(work);
}
}
}

@ -1,6 +1,5 @@
import Story from "./Story.ts";
import Search, { SearchParameters as QuerySearchParams } from "./Search.ts";
import TagSearch, { SearchParameters as TagSearchParams } from "./TagSearch.ts";
import { newSession, Options } from "../utils/http.ts";
import { Session } from "../types.d.ts";
@ -49,8 +48,4 @@ export default class Wattpad {
search(opts: QuerySearchParams) {
return new Search(opts, this.session);
}
tagSearch(opts: TagSearchParams) {
return new TagSearch(opts, this.session);
}
}

@ -6,6 +6,7 @@ export default function test(watt: Wattpad) {
await test.step("specific search", async () => {
const search = watt.search({
query: "Inky Desires [Bendy x Reader]",
type: "text",
}); // if a search for the exact name of my fic doesn't return the fic i will shit myself
await search.update(0);
@ -20,6 +21,7 @@ export default function test(watt: Wattpad) {
await test.step("broad search", async () => {
const search = watt.search({
query: "huggy wuggy smut",
type: "text",
});
await search.update(0);
@ -35,8 +37,9 @@ export default function test(watt: Wattpad) {
});
Deno.test("tag searches", async (test) => {
await test.step("specific search", async () => {
const search = watt.tagSearch({
tags: ["bluesourpachkid"],
const search = watt.search({
query: ["bluesourpachkid"],
type: "tag",
}); // if somebody else uses this exact tag (including misspelling) i will also shit myself
await search.update(0);
@ -49,8 +52,10 @@ export default function test(watt: Wattpad) {
);
});
await test.step("broad search", async () => {
const search = watt.tagSearch({
tags: ["batim", "bendyxreader"],
const search = watt.search({
query: ["batim", "bendyxreader"],
type: "tag",
limit: 20,
});
await search.update(0);

Loading…
Cancel
Save