113 lines
3.4 KiB
JavaScript
113 lines
3.4 KiB
JavaScript
class ZeroChan {
|
|
baseUrl = "https://zerochan.net";
|
|
|
|
constructor(fetchPath, cheerioPath, browser) {
|
|
this.browser = browser;
|
|
this.type = "image-board";
|
|
}
|
|
|
|
async fetchSearchResult(query = "hello", page = 1, perPage = 48) {
|
|
const url = `${this.baseUrl}/${query.trim().replace(/\s+/g, "+")}?p=${page}`;
|
|
|
|
const data = await this.browser.scrape(
|
|
url,
|
|
() => {
|
|
const list = document.querySelectorAll("#thumbs2 li");
|
|
if (list.length === 0) {
|
|
return { results: [], hasNextPage: false };
|
|
}
|
|
const results = [];
|
|
|
|
list.forEach(li => {
|
|
const id = li.getAttribute("data-id");
|
|
if (!id) return;
|
|
|
|
const img = li.querySelector("img");
|
|
const imgUrl =
|
|
img?.getAttribute("data-src") ||
|
|
img?.getAttribute("src") ||
|
|
null;
|
|
|
|
if (!imgUrl) return;
|
|
|
|
const tagLinks = li.querySelectorAll("p a");
|
|
const tags = [...tagLinks]
|
|
.map(a => a.textContent.trim())
|
|
.filter(Boolean);
|
|
|
|
results.push({
|
|
id,
|
|
image: imgUrl,
|
|
sampleImageUrl: imgUrl,
|
|
tags,
|
|
type: "preview"
|
|
});
|
|
});
|
|
|
|
const hasNextPage = document.querySelector('nav.pagination a[rel="next"]') !== null;
|
|
|
|
return {
|
|
results,
|
|
hasNextPage
|
|
};
|
|
},
|
|
{ waitSelector: "#thumbs2 li", timeout: 15000, renderWaitTime: 3000, loadImages: true }
|
|
);
|
|
|
|
console.log(data)
|
|
|
|
return {
|
|
results: data.results,
|
|
hasNextPage: data.hasNextPage,
|
|
page
|
|
};
|
|
}
|
|
|
|
async fetchInfo(id) {
|
|
const url = `${this.baseUrl}/${id}`;
|
|
|
|
const data = await this.browser.scrape(
|
|
url,
|
|
() => {
|
|
const preview = document.querySelector("a.preview");
|
|
if (!preview) {
|
|
return {
|
|
fullImage: null,
|
|
tags: [],
|
|
createdAt: Date.now()
|
|
};
|
|
}
|
|
|
|
const fullImage = preview.getAttribute("href") || null;
|
|
const img = preview.querySelector("img");
|
|
const alt = img?.getAttribute("alt") || "";
|
|
|
|
let tags = [];
|
|
if (alt.startsWith("Tags:")) {
|
|
tags = alt
|
|
.replace("Tags:", "")
|
|
.split(",")
|
|
.map(t => t.trim())
|
|
.filter(Boolean);
|
|
}
|
|
|
|
return {
|
|
fullImage,
|
|
tags,
|
|
createdAt: Date.now()
|
|
};
|
|
},
|
|
{ waitSelector: "a.preview img", timeout: 15000 }
|
|
);
|
|
|
|
return {
|
|
id,
|
|
fullImage: data.fullImage,
|
|
tags: data.tags,
|
|
createdAt: data.createdAt,
|
|
rating: "Unknown"
|
|
};
|
|
}
|
|
}
|
|
|
|
module.exports = { ZeroChan }; |