mirror of
https://github.com/imputnet/cobalt.git
synced 2025-07-22 13:18:27 +00:00
api: support new xiaohongshu links, add fallbacks to getRedirectingURL
Some checks are pending
CodeQL / Analyze (${{ matrix.language }}) (none, javascript-typescript) (push) Waiting to run
Run service tests / test service: ${{ matrix.service }} (push) Blocked by required conditions
Run service tests / test service functionality (push) Waiting to run
Run tests / check lockfile correctness (push) Waiting to run
Run tests / web sanity check (push) Waiting to run
Run tests / api sanity check (push) Waiting to run
Some checks are pending
CodeQL / Analyze (${{ matrix.language }}) (none, javascript-typescript) (push) Waiting to run
Run service tests / test service: ${{ matrix.service }} (push) Blocked by required conditions
Run service tests / test service functionality (push) Waiting to run
Run tests / check lockfile correctness (push) Waiting to run
Run tests / web sanity check (push) Waiting to run
Run tests / api sanity check (push) Waiting to run
closes #1394 Co-authored-by: wukko <me@wukko.me>
This commit is contained in:
parent
b9042a94e9
commit
3f785e7cbe
@ -1,4 +1,4 @@
|
|||||||
import { request } from 'undici';
|
import { request } from "undici";
|
||||||
const redirectStatuses = new Set([301, 302, 303, 307, 308]);
|
const redirectStatuses = new Set([301, 302, 303, 307, 308]);
|
||||||
|
|
||||||
export async function getRedirectingURL(url, dispatcher, headers) {
|
export async function getRedirectingURL(url, dispatcher, headers) {
|
||||||
@ -8,18 +8,34 @@ export async function getRedirectingURL(url, dispatcher, headers) {
|
|||||||
headers,
|
headers,
|
||||||
redirect: 'manual'
|
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']) {
|
if (redirectStatuses.has(r.statusCode) && r.headers['location']) {
|
||||||
return 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')) {
|
try request() with HEAD & GET,
|
||||||
return r.headers.get('location');
|
then do the same with fetch
|
||||||
}
|
(fetch is required for shortened reddit links)
|
||||||
}).catch(() => null);
|
*/
|
||||||
|
|
||||||
|
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;
|
return location;
|
||||||
}
|
}
|
||||||
|
@ -207,7 +207,7 @@ export const services = {
|
|||||||
patterns: [
|
patterns: [
|
||||||
"explore/:id?xsec_token=:token",
|
"explore/:id?xsec_token=:token",
|
||||||
"discovery/item/:id?xsec_token=:token",
|
"discovery/item/:id?xsec_token=:token",
|
||||||
"a/:shareId"
|
":shareType/:shareId",
|
||||||
],
|
],
|
||||||
altDomains: ["xhslink.com"],
|
altDomains: ["xhslink.com"],
|
||||||
},
|
},
|
||||||
|
@ -78,7 +78,7 @@ export const testers = {
|
|||||||
|
|
||||||
"xiaohongshu": pattern =>
|
"xiaohongshu": pattern =>
|
||||||
pattern.id?.length <= 24 && pattern.token?.length <= 64
|
pattern.id?.length <= 24 && pattern.token?.length <= 64
|
||||||
|| pattern.shareId?.length <= 24,
|
|| pattern.shareId?.length <= 24 && pattern.shareType?.length === 1,
|
||||||
|
|
||||||
"newgrounds": pattern =>
|
"newgrounds": pattern =>
|
||||||
pattern.id?.length <= 12 || pattern.audioId?.length <= 12,
|
pattern.id?.length <= 12 || pattern.audioId?.length <= 12,
|
||||||
|
@ -6,13 +6,13 @@ const https = (url) => {
|
|||||||
return url.replace(/^http:/i, 'https:');
|
return url.replace(/^http:/i, 'https:');
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function ({ id, token, shareId, h265, isAudioOnly, dispatcher }) {
|
export default async function ({ id, token, shareType, shareId, h265, isAudioOnly, dispatcher }) {
|
||||||
let noteId = id;
|
let noteId = id;
|
||||||
let xsecToken = token;
|
let xsecToken = token;
|
||||||
|
|
||||||
if (!noteId) {
|
if (!noteId) {
|
||||||
const patternMatch = await resolveRedirectingURL(
|
const patternMatch = await resolveRedirectingURL(
|
||||||
`https://xhslink.com/a/${shareId}`,
|
`https://xhslink.com/${shareType}/${shareId}`,
|
||||||
dispatcher
|
dispatcher
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -99,7 +99,7 @@ function aliasURL(url) {
|
|||||||
|
|
||||||
case "xhslink":
|
case "xhslink":
|
||||||
if (url.hostname === 'xhslink.com' && parts.length === 3) {
|
if (url.hostname === 'xhslink.com' && parts.length === 3) {
|
||||||
url = new URL(`https://www.xiaohongshu.com/a/${parts[2]}`);
|
url = new URL(`https://www.xiaohongshu.com/${parts[1]}/${parts[2]}`);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
[
|
[
|
||||||
{
|
{
|
||||||
"name": "video (might have expired)",
|
"name": "video (might have expired)",
|
||||||
"url": "https://www.xiaohongshu.com/explore/67cc17a3000000000e00726a?xsec_token=CBSFRtbF57so920elY1kbIX4fE1nhrwlpGZs9m6pIFpwo=",
|
"url": "https://www.xiaohongshu.com/explore/685e63e1000000000b02ee3b?xsec_token=ABN8EQJCDMPcFX9RRggeIPSHLIJ8zkGceFDyBewLGUz30=",
|
||||||
"canFail": true,
|
"canFail": true,
|
||||||
"params": {},
|
"params": {},
|
||||||
"expected": {
|
"expected": {
|
||||||
@ -11,7 +11,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "picker with multiple live photos (might have expired)",
|
"name": "picker with multiple live photos (might have expired)",
|
||||||
"url": "https://www.xiaohongshu.com/explore/67c691b4000000000d0159cc?xsec_token=CB8p1eyB5DiFkwlUpy1BTeVsI9oOve6ppNjuDzo8V8p5w=",
|
"url": "https://www.xiaohongshu.com/explore/687128a2000000001203d94c?xsec_token=CBlDi5QDXDWZu2uUmbUrpKwg8lEL3uC10mc59lGf43r9w=",
|
||||||
"canFail": true,
|
"canFail": true,
|
||||||
"params": {},
|
"params": {},
|
||||||
"expected": {
|
"expected": {
|
||||||
@ -21,7 +21,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "one photo (might have expired)",
|
"name": "one photo (might have expired)",
|
||||||
"url": "https://www.xiaohongshu.com/explore/676e132d000000000b016f68?xsec_token=ABRv6LKzizOFeSaf2HnnBkdBqniB5Ak1fI8tMAHzO31jA",
|
"url": "https://www.xiaohongshu.com/explore/64726b99000000000800e115?xsec_token=ABoD3qPHqVZolCfS-J8UP9QQaPXZ6Z6PVyODrhaiUg27U=",
|
||||||
"canFail": true,
|
"canFail": true,
|
||||||
"params": {},
|
"params": {},
|
||||||
"expected": {
|
"expected": {
|
||||||
@ -31,7 +31,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "short link (might have expired)",
|
"name": "short link (might have expired)",
|
||||||
"url": "https://xhslink.com/a/czn4z6c1tic4",
|
"url": "https://xhslink.com/m/2wAnaTkLRc1",
|
||||||
"canFail": true,
|
"canFail": true,
|
||||||
"params": {},
|
"params": {},
|
||||||
"expected": {
|
"expected": {
|
||||||
|
Loading…
Reference in New Issue
Block a user