api: support new xiaohongshu links, add fallbacks to getRedirectingURL
closes #1394 Co-authored-by: wukko <me@wukko.me>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { request } from 'undici';
|
||||
import { request } from "undici";
|
||||
const redirectStatuses = new Set([301, 302, 303, 307, 308]);
|
||||
|
||||
export async function getRedirectingURL(url, dispatcher, headers) {
|
||||
@@ -8,18 +8,34 @@ export async function getRedirectingURL(url, dispatcher, headers) {
|
||||
headers,
|
||||
redirect: 'manual'
|
||||
};
|
||||
const getParams = {
|
||||
...params,
|
||||
method: 'GET',
|
||||
};
|
||||
|
||||
let location = await request(url, params).then(r => {
|
||||
const callback = (r) => {
|
||||
if (redirectStatuses.has(r.statusCode) && r.headers['location']) {
|
||||
return r.headers['location'];
|
||||
}
|
||||
}).catch(() => null);
|
||||
}
|
||||
|
||||
location ??= await fetch(url, params).then(r => {
|
||||
if (redirectStatuses.has(r.status) && r.headers.has('location')) {
|
||||
return r.headers.get('location');
|
||||
}
|
||||
}).catch(() => null);
|
||||
/*
|
||||
try request() with HEAD & GET,
|
||||
then do the same with fetch
|
||||
(fetch is required for shortened reddit links)
|
||||
*/
|
||||
|
||||
let location = await request(url, params)
|
||||
.then(callback).catch(() => null);
|
||||
|
||||
location ??= await request(url, getParams)
|
||||
.then(callback).catch(() => null);
|
||||
|
||||
location ??= await fetch(url, params)
|
||||
.then(callback).catch(() => null);
|
||||
|
||||
location ??= await fetch(url, getParams)
|
||||
.then(callback).catch(() => null);
|
||||
|
||||
return location;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user