fix(api/threads): update threads and support gif posts

This commit is contained in:
Snazzah 2025-01-24 19:33:17 -06:00
parent 0f3280f836
commit 9bf943362d
No known key found for this signature in database
GPG key ID: EA479766A94CEB61
3 changed files with 35 additions and 4 deletions

View file

@ -13,7 +13,8 @@ const VALID_SERVICES = new Set([
'reddit', 'reddit',
'twitter', 'twitter',
'youtube', 'youtube',
'youtube_oauth' 'youtube_oauth',
'threads'
]); ]);
const invalidCookies = {}; const invalidCookies = {};

View file

@ -1,5 +1,6 @@
import { createStream } from "../../stream/manage.js"; import { createStream } from "../../stream/manage.js";
import { getCookie, updateCookie } from "../cookie/manager.js"; import { getCookie, updateCookie } from "../cookie/manager.js";
import { genericUserAgent } from "../../config.js";
const commonHeaders = { const commonHeaders = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8",
@ -17,10 +18,10 @@ const commonHeaders = {
"Sec-Fetch-Site": "same-origin", "Sec-Fetch-Site": "same-origin",
"Sec-Gpc": "1", "Sec-Gpc": "1",
"Upgrade-Insecure-Requests": "1", "Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36", "User-Agent": genericUserAgent,
}; };
const DATA_REGEX = /<script type="application\/json" {2}data-content-len="\d+" data-sjs>({"require":\[\["ScheduledServerJS","handle",null,\[{"__bbox":{"require":\[\["RelayPrefetchedStreamCache(?:(?:@|\\u0040)[0-9a-f]{32})?","next",\[],\["adp_BarcelonaPostPageQueryRelayPreloader_[0-9a-f]{23}",[^\n]+})<\/script>\n/; const DATA_REGEX = /<script type="application\/json" {2}data-content-len="\d+" data-sjs>({"require":\[\["ScheduledServerJS","handle",null,\[{"__bbox":{"require":\[\["RelayPrefetchedStreamCache(?:(?:@|\\u0040)[0-9a-f]{32})?","next",\[],\["adp_BarcelonaPostPage(?:Direct)?QueryRelayPreloader_[0-9a-f]{23}",[^\n]+})<\/script>\n/;
export default async function({ user, id, quality, alwaysProxy, dispatcher }) { export default async function({ user, id, quality, alwaysProxy, dispatcher }) {
const cookie = getCookie("threads"); const cookie = getCookie("threads");
@ -117,5 +118,25 @@ export default async function({ user, id, quality, alwaysProxy, dispatcher }) {
} }
} }
// GIPHY GIF
if (post.media_type === 19) {
if (!post.giphy_media_info?.images?.fixed_height?.webp) {
return { error: "fetch.empty" };
}
let giphyUrl = post.giphy_media_info.images.fixed_height.webp;
// In a regular browser (probably through the Accept header), the GIPHY link shows an HTML page with an ad that shows the GIF
// I'd rather have the GIF directly to save bandwidth and avoid ads.
let giphyId = giphyUrl.split("/")?.[5];
if (giphyId && !giphyId.includes(".")) giphyUrl = `https://i.giphy.com/${giphyId}.webp`;
return {
urls: giphyUrl,
filename: `${filenameBase}.webp`,
isPhoto: true
}
}
return { error: "fetch.fail" }; return { error: "fetch.fail" };
} }

View file

@ -14,7 +14,7 @@
"params": {}, "params": {},
"expected": { "expected": {
"code": 200, "code": 200,
"status": "redirect" "status": "tunnel"
} }
}, },
{ {
@ -25,5 +25,14 @@
"code": 200, "code": 200,
"status": "picker" "status": "picker"
} }
},
{
"name": "giphy gif",
"url": "https://www.threads.net/@zjy4fun/post/DDvzVsgpZNQ",
"params": {},
"expected": {
"code": 200,
"status": "tunnel"
}
} }
] ]