100 lines
2.9 KiB
JavaScript
100 lines
2.9 KiB
JavaScript
class Giphy {
|
|
baseUrl = "https://giphy.com";
|
|
|
|
constructor() {
|
|
this.type = "image-board";
|
|
}
|
|
|
|
async search(query = "hello", page = 1, perPage = 48) {
|
|
const url = `${this.baseUrl}/search/${query.trim().replace(/\s+/g, "-")}`;
|
|
|
|
const data = await this.scrape(
|
|
url,
|
|
(page) => page.evaluate(() => {
|
|
const items = document.querySelectorAll('a[data-giphy-id]');
|
|
const results = [];
|
|
|
|
items.forEach(el => {
|
|
const id = el.getAttribute('data-giphy-id');
|
|
|
|
const srcWebp = el.querySelector('source[type="image/webp"][srcset^="http"]');
|
|
const srcImg = el.querySelector('img');
|
|
|
|
let rawSrc =
|
|
srcWebp?.getAttribute("srcset")?.split(" ")[0] ||
|
|
srcImg?.src ||
|
|
null;
|
|
|
|
if (!rawSrc || rawSrc.startsWith("data:")) return;
|
|
|
|
const alt = srcImg?.getAttribute("alt") || "";
|
|
const tags = alt.trim().split(/\s+/).filter(Boolean);
|
|
|
|
results.push({
|
|
id,
|
|
image: rawSrc,
|
|
});
|
|
});
|
|
|
|
return {
|
|
results,
|
|
hasNextPage: false
|
|
};
|
|
}),
|
|
{
|
|
waitSelector: 'picture img, a[data-giphy-id] img',
|
|
scrollToBottom: true,
|
|
timeout: 15000
|
|
}
|
|
);
|
|
|
|
return {
|
|
results: data.result.results.map(r => ({
|
|
id: r.id,
|
|
image: r.image
|
|
})),
|
|
hasNextPage: data.result.hasNextPage,
|
|
page
|
|
};
|
|
}
|
|
|
|
async getInfo(id) {
|
|
const url = `https://giphy.com/gifs/${id}`;
|
|
|
|
const data = await this.scrape(
|
|
url,
|
|
(page) => page.evaluate(() => {
|
|
const scripts = document.querySelectorAll(
|
|
'script[type="application/ld+json"]'
|
|
);
|
|
|
|
let imgsrc = null;
|
|
|
|
scripts.forEach(script => {
|
|
try {
|
|
const json = JSON.parse(script.textContent);
|
|
|
|
if (json?.["@type"] === "Article" && json?.image?.url) {
|
|
imgsrc = json.image.url;
|
|
}
|
|
} catch {}
|
|
});
|
|
|
|
return {
|
|
image: imgsrc
|
|
};
|
|
}),
|
|
{
|
|
waitSelector: 'script[type="application/ld+json"]',
|
|
timeout: 15000
|
|
}
|
|
);
|
|
|
|
return {
|
|
id,
|
|
image: data.result.image
|
|
};
|
|
}
|
|
}
|
|
|
|
module.exports = Giphy; |