all extensions to new format :P

This commit is contained in:
2025-12-15 19:40:07 +01:00
parent 9986b64ace
commit 9fe48f93fe
19 changed files with 1725 additions and 1375 deletions

View File

@@ -1,139 +1,83 @@
class Gelbooru {
baseUrl = "https://gelbooru.com";
constructor(fetchPath, cheerioPath) {
this.fetch = require(fetchPath);
this.load = require(cheerioPath).load;
constructor() {
this.type = "image-board";
}
async fetchSearchResult(query, page = 1, perPage = 42) {
if (!query) query = "original";
async search(query = "thighs", page = 1, perPage = 42) {
const url = `${this.baseUrl}/index.php?page=post&s=list&tags=${encodeURIComponent(query)}&pid=${(page - 1) * perPage}`;
const url = `${this.baseUrl}/index.php?page=post&s=list&tags=${query}&pid=${(page - 1) * perPage}`;
const response = await this.fetch(url, {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
});
const data = await response.text();
const $ = this.load(data);
const html = await fetch(url, {
headers: { "User-Agent": "Mozilla/5.0" }
}).then(r => r.text());
const $ = this.cheerio.load(html);
const results = [];
$('.thumbnail-container a').each((i, e) => {
const $e = $(e);
const href = $e.attr('href');
$("article.thumbnail-preview > a[id^='p']").each((_, el) => {
const id = $(el).attr("id")?.slice(1); // p13123834 → 13123834
if (!id) return;
const idMatch = href.match(/id=(\d+)/);
const id = idMatch ? idMatch[1] : null;
const img = $(el).find("img");
const image = img.attr("src");
const image = $e.find('img').attr('src');
const tags = img.attr("alt")
?.replace(/^Rule 34 \|\s*/, "")
?.split(",")
?.map(t => t.trim())
?.filter(Boolean) || [];
const tags = $e.find('img').attr('alt')?.trim()?.split(' ').filter(tag => tag !== "");
if (id && image) {
results.push({
id: id,
image: image,
tags: tags,
type: 'preview'
});
}
results.push({ id, image, tags });
});
const pagination = $('.pagination a');
// pagination
const totalPages = Math.max(
page,
...$("a[href*='pid=']")
.map((_, el) =>
Math.floor(
parseInt($(el).attr("href")?.match(/pid=(\d+)/)?.[1] || 0) / perPage
) + 1
)
.get()
);
let totalPages = 1;
pagination.each((i, e) => {
const href = $(e).attr('href');
if (href && href.includes('pid=')) {
const pidMatch = href.match(/pid=(\d+)/);
if (pidMatch) {
const pid = parseInt(pidMatch[1], 10);
totalPages = Math.max(totalPages, Math.floor(pid / perPage) + 1);
}
}
});
const currentPage = page;
const nextPage = currentPage < totalPages ? currentPage + 1 : null;
const previousPage = currentPage > 1 ? currentPage - 1 : null;
const hasNextPage = nextPage !== null;
return {
total: totalPages * perPage,
next: nextPage !== null ? nextPage : 0,
previous: previousPage !== null ? previousPage : 0,
pages: totalPages,
page: currentPage,
hasNextPage,
results
return {
results,
page,
hasNextPage: page < totalPages
};
}
async fetchInfo(id) {
const url = `${this.baseUrl}/index.php?page=post&s=view&id=${id}`;
async getInfo(id) {
const html = await fetch(
`${this.baseUrl}/index.php?page=post&s=view&id=${id}`,
{ headers: { "User-Agent": "Mozilla/5.0" } }
).then(r => r.text());
const response = await this.fetch(url, {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
});
const original = await response.text();
const $ = this.cheerio.load(html);
const container = $("section.image-container");
const $ = this.load(original);
let image =
container.find("#image").attr("src") ||
container.attr("data-file-url") ||
container.attr("data-large-file-url") ||
null;
let fullImage;
fullImage = $('#gelcom_img').attr('src') || $('#gelcom_mp4').attr('src');
if (!fullImage) {
fullImage = $('#right-col a[href*="/images/"]').attr('href') || $('#right-col a[href*="/videos/"]').attr('href');
}
if (fullImage && fullImage.startsWith('/')) {
fullImage = new URL(fullImage, this.baseUrl).href;
}
const tagsList = $('#tag-list a');
const tags = tagsList.map((i, el) => $(el).text().trim()).get();
const stats = $('#post-view-image-container + br + br + br + br + ul, #stats');
const postedData = stats.find("li:contains('Posted:')").text().trim();
const createdAt = new Date(postedData.split("Posted: ")[1]).getTime();
const publishedBy = stats.find("li:contains('User:') a").text().trim() || null;
const rating = stats.find("li:contains('Rating:')").text().trim().split("Rating: ")[1];
const comments = $('#comment-list .comment').map((i, el) => {
const $e = $(el);
const id = $e.attr('id')?.replace('c', '');
const user = $e.find('.comment-user a').text().trim();
const comment = $e.find('.comment-body').text().trim();
return {
id,
user,
comment,
}
}).get().filter(Boolean).filter((comment) => comment.comment !== '');
// tags
const tags = container
.attr("data-tags")
?.trim()
?.split(/\s+/)
?.filter(Boolean) || [];
return {
id,
fullImage,
resizedImageUrl: fullImage,
tags,
createdAt,
publishedBy,
rating,
comments
}
image,
tags
};
}
}
module.exports = { Gelbooru };
module.exports = Gelbooru;