Skip to content

Commit

Permalink
feat: auto-retry
Browse files Browse the repository at this point in the history
  • Loading branch information
k0kag3 committed May 24, 2021
1 parent 4a3e07a commit 5ad8bf1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/dom.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import fetch from "node-fetch";
import { JSDOM } from "jsdom";

export async function getDOM(endpoint: string) {
const {
window: { document },
} = new JSDOM(await fetch(endpoint).then((res) => res.text()));
return document;
export async function getDOM(endpoint: string): Promise<Document | undefined> {
let retryCount = 3;
while (retryCount > 0) {
try {
const {
window: { document },
} = new JSDOM(await fetch(endpoint).then((res) => res.text()));
return document;
} catch (err) {
if (err.code === "ETIMEOUT") {
retryCount -= 1;
} else {
throw err;
}
}
}
}
6 changes: 6 additions & 0 deletions src/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export type Item = ImageItem | WritingItem | OtherMaterialItem;
export async function getItemDetail(itemId: string): Promise<Item> {
const viewUrl = `https://aryion.com/g4/view/${itemId}`;
const document = await getDOM(viewUrl);
if (!document) {
throw new Error("Cannot fetch document");
}

const authorAvatarUrl =
document.querySelector<HTMLImageElement>(".avatar")?.src;
Expand Down Expand Up @@ -133,6 +136,9 @@ export async function getItemDetail(itemId: string): Promise<Item> {

if (mimeType !== "application/pdf") {
const res = await getDOM(contentUrl);
if (!res) {
throw new Error("Cannot fetch document");
}
writing.content = {
text: res.body.textContent?.trim() || "",
html: res.body.innerHTML,
Expand Down
4 changes: 4 additions & 0 deletions src/latest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ export async function getLatestUpdates(
}

const document = await getDOM(target);
if (!document) {
throw new Error("Cannot fetch document");
}

const cursor = parsePageCursor(document)!;

const updates = Array.from(document.querySelectorAll(".detail-item")).map(
Expand Down
3 changes: 3 additions & 0 deletions src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { AryionUserNotFoundError } from "./error";

export async function verifyUser(aryionUsername: string) {
const document = await getDOM(`https://aryion.com/g4/user/${aryionUsername}`);
if (!document) {
throw new Error("Cannot fetch document");
}

if (
document.querySelector<HTMLSpanElement>(".g-box-title")!.textContent ===
Expand Down

0 comments on commit 5ad8bf1

Please sign in to comment.