Added predata step. Using RedGifs temporary API key.

This commit is contained in:
2024-09-11 05:16:58 +02:00
parent 4acbe16fb8
commit bed4fe288f
16 changed files with 9877 additions and 1524 deletions

View File

@@ -10,6 +10,8 @@ async function imgurImageApi(host) {
},
});
console.log('imgur headers', res.headers);
if (res.status !== 200) {
throw new Error(`Imgur API returned HTTP ${res.status} for source '${host.url}'`);
}

View File

@@ -11,7 +11,7 @@ async function redditPreview(host, post) {
datetime: post.datetime,
original: post,
} : null,
items: post.preview.map(image => ({
items: post.preview.map((image) => ({
id: post.host.id || post.id,
url: image.url,
title: post.title,

View File

@@ -2,16 +2,19 @@
const fetch = require('node-fetch');
const mime = require('mime');
// const unprint = require('unprint');
const { version } = require('../../package.json');
function scrapeGallery(data) {
const oldestDate = Math.min(...data.gifs.map(gif => gif.createDate));
const oldestDate = Math.min(...data.gifs.map((gif) => gif.createDate));
return {
album: {
id: data.id,
datetime: new Date(oldestDate * 1000),
},
items: data.gifs.map(gif => ({
items: data.gifs.map((gif) => ({
id: gif.id,
url: gif.urls.hd,
description: gif.tags.join(', '),
@@ -33,12 +36,22 @@ async function fetchGallery(galleryId) {
return scrapeGallery(data);
}
async function redgifs(host) {
const res = await fetch(`https://api.redgifs.com/v2/gifs/${host.id.toLowerCase()}`);
async function redgifsApi(host, post, { predata }) {
if (!predata?.token) {
throw new Error('No RedGifs token provided');
}
const res = await fetch(`https://api.redgifs.com/v2/gifs/${host.id.toLowerCase()}`, {
headers: {
authorization: `Bearer ${predata.token}`,
'user-agent': predata.userAgent,
},
});
const data = await res.json();
if (data.errorMessage) {
throw new Error(`RedGifs API returned error for source '${host.url}' (${res.status}): ${data.errorMessage.description}`);
if (data.errorMessage || data.error) {
throw new Error(`RedGifs API returned error for source '${host.url}' (${res.status}): ${data.errorMessage?.description || data.error?.description}`);
}
if (data.id && data.gifs) {
@@ -53,7 +66,7 @@ async function redgifs(host) {
return fetchGallery(data.gif.gallery);
}
return {
const curated = {
album: null,
items: [{
id: data.gif.id,
@@ -63,7 +76,78 @@ async function redgifs(host) {
datetime: new Date(data.gif.createDate * 1000),
original: data.gif,
}],
headers: {
'user-agent': predata.userAgent,
},
};
return curated;
}
module.exports = redgifs;
async function redgifs(host, post, { predata }) {
if (predata?.token) {
return redgifsApi(host, post, { predata });
}
throw new Error('No RedGifs token provided');
/*
const res = await unprint.get(host.url);
if (!res.ok) {
throw new Error(`RedGifs returned error for source '${host.url}' (${res.status})`);
}
const data = res.context.query.json('script[type="application/ld+json"]');
if (!data.video) {
return null;
}
// console.log(data);
const curatedData = {
album: null,
items: [{
id: host.id,
url: data.video.contentUrl,
description: data.video.keywords,
type: mime.getType(new URL(data.video.contentUrl).pathname),
datetime: new Date(data.video.uploadDate),
original: data.video,
}],
};
// console.log(curatedData);
return null;
// return curatedData;
*/
}
async function fetchPredata() {
const userAgent = `ripunzel/${version}`;
const res = await fetch('https://api.redgifs.com/v2/auth/temporary', {
headers: {
'user-agent': userAgent,
},
});
const data = await res.json();
if (res.ok) {
return {
address: data.addr,
agent: data.agent,
token: data.token,
userAgent,
};
}
return null;
}
module.exports = {
fetchInfo: redgifs,
fetchPredata,
};