Skip to content

Commit

Permalink
yup
Browse files Browse the repository at this point in the history
  • Loading branch information
zavbala committed Mar 23, 2024
1 parent a8ca382 commit 07a5eb4
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 35 deletions.
1 change: 1 addition & 0 deletions apps/api/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ dist
.dev.vars
node_modules

.html
yarn.lock
wrangler.toml
pnpm-lock.yaml
Expand Down
Empty file removed apps/api/README.md
Empty file.
37 changes: 22 additions & 15 deletions apps/api/functions/spider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Args = {

export const resolver = (
nodes: HTMLElement[],
schema: Record<string, string>,
schema: Record<string, string>
) => {
const output: Record<string, HTMLElement> | any[] = [];

Expand All @@ -35,24 +35,31 @@ export const resolver = (
};

type Shot = {
views: string;
link: string;
upvotes: string;
image: string;
views: string;
avatar: string;
upvotes: string;
username: string;
displayName: string;
avatar: string;
};

export const define = (shots: Record<string, HTMLElement>[]) =>
shots.map((item) => ({
views: item.views.innerText.trim(),
link: item.link.getAttribute("href"),
upvotes: item.upvotes?.innerText.trim(),
image: item.image.getAttribute("data-src"),
author: {
displayName: item.displayName.innerText,
avatar: item.avatar.getAttribute("data-src"),
username: item.username?.getAttribute("href")?.split("/")[1],
},
}));
shots.map((item) => {
const link = item.link.getAttribute("href");

return {
link,
views: item.views.innerText.trim(),
upvotes: item.upvotes?.innerText.trim(),
id: link?.split("/shots/")[1].split("-")[0],
image: (
item.image?.getAttribute("data-src") || item.image?.getAttribute("src")
)?.split("?")[0],
author: {
displayName: item.displayName.innerText,
avatar: item.avatar.getAttribute("data-src")?.split("?")[0],
username: item.username?.getAttribute("href")?.split("/")[1],
},
};
});
2 changes: 1 addition & 1 deletion apps/api/lib/models.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export const Shot = {
image: "img",
link: "a.shot-thumbnail-link",
views: "span.js-shot-views-count",
username: "div.user-information a",
avatar: "div.user-information img",
upvotes: "span.js-shot-likes-count",
displayName: "div.user-information span",
image: "div.js-thumbnail-base > figure > img",
};
14 changes: 7 additions & 7 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
"deploy": "wrangler deploy --minify src/index.ts"
},
"dependencies": {
"@hono/zod-validator": "^0.1.9",
"hono": "^3.7.2",
"node-html-parser": "^6.1.10"
"@hono/zod-validator": "^0.1.11",
"hono": "^4.0.0",
"node-html-parser": "^6.1.12"
},
"devDependencies": {
"@babel/preset-env": "^7.23.3",
"@babel/preset-env": "^7.23.9",
"@babel/preset-typescript": "^7.23.3",
"@cloudflare/workers-types": "^4.20230914.0",
"@types/jest": "^29.5.8",
"@cloudflare/workers-types": "^4.20240208.0",
"@types/jest": "^29.5.12",
"jest": "^29.7.0",
"wrangler": "^3.9.0"
"wrangler": "^3.28.1"
}
}
41 changes: 29 additions & 12 deletions apps/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ app.get("/", (context) =>
author: @zavbala
docs: https://dunker.app/docs
`),
`)
);

app.get("/shots", async (context) => {
Expand All @@ -24,8 +24,9 @@ app.get("/shots", async (context) => {
};

const path =
"/shots/popular?" +
"/shots/popular" +
(tag ? "/" + tag : "") +
"?" +
new URLSearchParams({ page, per_page: "24" }).toString();

const document = await fetcher(path);
Expand All @@ -39,11 +40,19 @@ app.get("/shots", async (context) => {
});

app.get("/search", async (context) => {
const { query } = context.req.query() as { query: string };
const { query, page } = context.req.query() as {
query: string;
page: string;
};

const path = "/search" + (query ? "/" + query : "");
const document = await fetcher(path);
const path =
"/search" +
(query ? "/" + query : "") +
(page && page !== "1" ? `?page=${page}` : "");

console.log(path);

const document = await fetcher(path);
const shots = document.querySelectorAll("li.shot-thumbnail-container");

const items = resolver(shots, Shot);
Expand All @@ -56,7 +65,11 @@ app.get("/shots/:id", async (context) => {
const { id } = context.req.param() as { id: string };

const document = await fetcher("/shots/" + id);
const isVideo = document.querySelector("video")?.getAttribute("src") || false;

const isVideo =
document.querySelector("video")?.getAttribute("src") ||
document.querySelector("video")?.getAttribute("data-video-large") ||
false;

const data = {
title: document.querySelector("h1.shot-header__title")?.innerText,
Expand All @@ -68,12 +81,16 @@ app.get("/shots/:id", async (context) => {
document.querySelector("div.block-media a img")?.getAttribute("src"),
},

description: document
.querySelectorAll("div.content-block")[1]
.querySelectorAll("p")
.join(" ")
.replace(/<[^>]*>/g, "\n")
.trim(),
description: document.querySelectorAll("div.content-block").length
? document
?.querySelectorAll("div.content-block")[1]
.querySelectorAll("p")
.join(" ")
.replace(/<[^>]*>/g, "\n")
.replace(/\n/g, " ")
.replace(/\s+/g, " ")
.trim()
: null,

author: {
username: document.querySelector(".sticky-header__name a")?.innerText,
Expand Down

0 comments on commit 07a5eb4

Please sign in to comment.