all extensions to new format :P
This commit is contained in:
71
giphy.js
71
giphy.js
@@ -1,24 +1,22 @@
|
||||
class Giphy {
|
||||
baseUrl = "https://giphy.com";
|
||||
|
||||
constructor(fetchPath, cheerioPath, browser) {
|
||||
this.browser = browser;
|
||||
constructor() {
|
||||
this.type = "image-board";
|
||||
}
|
||||
|
||||
async fetchSearchResult(query = "hello", page = 1, perPage = 48) {
|
||||
async search(query = "hello", page = 1, perPage = 48) {
|
||||
const url = `${this.baseUrl}/search/${query.trim().replace(/\s+/g, "-")}`;
|
||||
|
||||
const data = await this.browser.scrape(
|
||||
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');
|
||||
|
||||
// solo coger sources válidos
|
||||
const srcWebp = el.querySelector('source[type="image/webp"][srcset^="http"]');
|
||||
const srcImg = el.querySelector('img');
|
||||
|
||||
@@ -27,20 +25,14 @@ class Giphy {
|
||||
srcImg?.src ||
|
||||
null;
|
||||
|
||||
// ignorar 1x1 base64
|
||||
if (!rawSrc || rawSrc.startsWith("data:")) return;
|
||||
|
||||
const imgUrl = rawSrc;
|
||||
|
||||
const alt = srcImg?.getAttribute("alt") || "";
|
||||
const tags = alt.trim().split(/\s+/).filter(Boolean);
|
||||
|
||||
results.push({
|
||||
id,
|
||||
image: imgUrl,
|
||||
sampleImageUrl: imgUrl,
|
||||
tags,
|
||||
type: "preview"
|
||||
image: rawSrc,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -48,24 +40,61 @@ class Giphy {
|
||||
results,
|
||||
hasNextPage: false
|
||||
};
|
||||
},
|
||||
{ waitSelector: 'picture img, a[data-giphy-id] img', scrollToBottom: true, timeout: 15000}
|
||||
}),
|
||||
{
|
||||
waitSelector: 'picture img, a[data-giphy-id] img',
|
||||
scrollToBottom: true,
|
||||
timeout: 15000
|
||||
}
|
||||
);
|
||||
|
||||
return {
|
||||
results: data.results,
|
||||
hasNextPage: data.hasNextPage,
|
||||
results: data.result.results.map(r => ({
|
||||
id: r.id,
|
||||
image: r.image
|
||||
})),
|
||||
hasNextPage: data.result.hasNextPage,
|
||||
page
|
||||
};
|
||||
}
|
||||
|
||||
async fetchInfo(id) {
|
||||
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,
|
||||
createdAt: Date.now(),
|
||||
rating: "Unknown"
|
||||
image: data.result.image
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { Giphy };
|
||||
module.exports = Giphy;
|
||||
Reference in New Issue
Block a user