mirror of
https://github.com/wukko/cobalt.git
synced 2025-05-29 13:00:12 +02:00
api/url: replace user-agent argument with headers
in redirect helpers
This commit is contained in:
parent
b1bde25dee
commit
a6240d0192
@ -1,11 +1,11 @@
|
||||
import { request } from 'undici';
|
||||
const redirectStatuses = new Set([301, 302, 303, 307, 308]);
|
||||
|
||||
export async function getRedirectingURL(url, dispatcher, userAgent) {
|
||||
export async function getRedirectingURL(url, dispatcher, headers) {
|
||||
const location = await request(url, {
|
||||
dispatcher,
|
||||
method: 'HEAD',
|
||||
headers: { 'user-agent': userAgent }
|
||||
headers: headers
|
||||
}).then(r => {
|
||||
if (redirectStatuses.has(r.statusCode) && r.headers['location']) {
|
||||
return r.headers['location'];
|
||||
|
@ -527,7 +527,7 @@ export default function instagram(obj) {
|
||||
// for some reason instagram decides to return HTML
|
||||
// instead of a redirect when requesting with a normal
|
||||
// browser user-agent
|
||||
'curl/7.88.1'
|
||||
{'User-Agent': 'curl/7.88.1'}
|
||||
).then(match => instagram({
|
||||
...obj, ...match,
|
||||
shareId: undefined
|
||||
|
@ -53,27 +53,18 @@ export default async function(obj) {
|
||||
const accessToken = await getAccessToken();
|
||||
|
||||
if (params.shortId) {
|
||||
let url = await fetch(`https://www.reddit.com/video/${params.shortId}`, {
|
||||
headers: {
|
||||
'User-Agent': genericUserAgent,
|
||||
'Authorization': `Bearer ${accessToken}`
|
||||
}
|
||||
}).then(r => r.url).catch(() => {});
|
||||
|
||||
if (!url) return { error: "fetch.fail" };
|
||||
|
||||
try {
|
||||
params = extract(normalizeURL(url)).patternMatch;
|
||||
} catch (error) {
|
||||
return { error: "fetch.fail" };
|
||||
}
|
||||
params = await resolveRedirectingURL(
|
||||
`https://www.reddit.com/video/${params.shortId}`,
|
||||
obj.dispatcher,
|
||||
{'User-Agent': genericUserAgent, 'Authorization': `Bearer ${accessToken}`}
|
||||
);
|
||||
}
|
||||
|
||||
if (!params.id && params.shareId) {
|
||||
params = await resolveRedirectingURL(
|
||||
`https://www.reddit.com/r/${params.sub}/s/${params.shareId}`,
|
||||
obj.dispatcher,
|
||||
genericUserAgent
|
||||
{'User-Agent': genericUserAgent}
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -239,11 +239,11 @@ export function extract(url) {
|
||||
return { host, patternMatch };
|
||||
}
|
||||
|
||||
export async function resolveRedirectingURL(url, dispatcher, userAgent) {
|
||||
export async function resolveRedirectingURL(url, dispatcher, headers) {
|
||||
const originalService = getHostIfValid(normalizeURL(url));
|
||||
if (!originalService) return;
|
||||
|
||||
const canonicalURL = await getRedirectingURL(url, dispatcher, userAgent);
|
||||
const canonicalURL = await getRedirectingURL(url, dispatcher, headers);
|
||||
if (!canonicalURL) return;
|
||||
|
||||
const { host, patternMatch } = extract(normalizeURL(canonicalURL));
|
||||
|
Loading…
x
Reference in New Issue
Block a user