|
| 1 | +import { genericUserAgent } from "../../config.js"; |
| 2 | + |
| 3 | +// Helper function to add delay between requests |
| 4 | +const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); |
| 5 | + |
| 6 | +// Helper function to check if response is a Cloudflare challenge |
| 7 | +const isCloudflareChallenge = (response) => { |
| 8 | + return ( |
| 9 | + response.status === 403 || |
| 10 | + response.status === 503 || |
| 11 | + (response.status === 200 && |
| 12 | + response.headers.get("server")?.includes("cloudflare")) |
| 13 | + ); |
| 14 | +}; |
| 15 | + |
| 16 | +// Enhanced fetch with retry logic for Cloudflare challenges |
| 17 | +const fetchWithRetry = async (url, options, maxRetries = 3) => { |
| 18 | + let lastError; |
| 19 | + |
| 20 | + for (let attempt = 1; attempt <= maxRetries; attempt++) { |
| 21 | + try { |
| 22 | + const response = await fetch(url, options); |
| 23 | + |
| 24 | + // If it's a Cloudflare challenge and not the last attempt, wait and retry |
| 25 | + if (isCloudflareChallenge(response) && attempt < maxRetries) { |
| 26 | + await delay(1000 * attempt); // Exponential backoff |
| 27 | + continue; |
| 28 | + } |
| 29 | + |
| 30 | + return response; |
| 31 | + } catch (error) { |
| 32 | + lastError = error; |
| 33 | + if (attempt < maxRetries) { |
| 34 | + await delay(1000 * attempt); |
| 35 | + continue; |
| 36 | + } |
| 37 | + throw error; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + throw lastError; |
| 42 | +}; |
| 43 | + |
| 44 | +export default async function (obj) { |
| 45 | + let videoId = obj.postId; |
| 46 | + if (!videoId) { |
| 47 | + return { error: "fetch.empty" }; |
| 48 | + } |
| 49 | + |
| 50 | + try { |
| 51 | + // For /p/ (post) URLs, use HTML parsing |
| 52 | + if (obj.postId) { |
| 53 | + return await handlePostUrl(obj.postId, obj); |
| 54 | + } |
| 55 | + |
| 56 | + return { error: "fetch.empty" }; |
| 57 | + } catch (error) { |
| 58 | + console.error("Sora service error:", error); |
| 59 | + return { error: "fetch.fail" }; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +async function handlePostUrl(postId, obj) { |
| 64 | + const targetUrl = `https://sora.com/p/${postId}`; |
| 65 | + |
| 66 | + const res = await fetchWithRetry(targetUrl, { |
| 67 | + headers: { |
| 68 | + "user-agent": genericUserAgent, |
| 69 | + accept: |
| 70 | + "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7", |
| 71 | + "accept-language": "en-US,en;q=0.9", |
| 72 | + "accept-encoding": "gzip, deflate, br", |
| 73 | + "sec-ch-ua": |
| 74 | + '"Google Chrome";v="138", "Chromium";v="138", "Not=A?Brand";v="99"', |
| 75 | + "sec-ch-ua-mobile": "?0", |
| 76 | + "sec-ch-ua-platform": '"Windows"', |
| 77 | + "sec-fetch-dest": "document", |
| 78 | + "sec-fetch-mode": "navigate", |
| 79 | + "sec-fetch-site": "none", |
| 80 | + "sec-fetch-user": "?1", |
| 81 | + "upgrade-insecure-requests": "1", |
| 82 | + "cache-control": "max-age=0", |
| 83 | + dnt: "1", |
| 84 | + }, |
| 85 | + }); |
| 86 | + |
| 87 | + if (!res.ok) { |
| 88 | + return { error: "fetch.fail" }; |
| 89 | + } |
| 90 | + |
| 91 | + const html = await res.text(); |
| 92 | + |
| 93 | + // Extract video URL from HTML and script tags |
| 94 | + let videoUrl; |
| 95 | + let title; |
| 96 | + |
| 97 | + // Look for video URLs in various patterns within the HTML and script content |
| 98 | + const videoPatterns = [ |
| 99 | + /https:\/\/videos\.openai\.com\/vg-assets\/[^"'>\s]+\.mp4[^"'>\s]*/g, |
| 100 | + /"(https:\/\/videos\.openai\.com\/vg-assets\/[^"]+\.mp4[^"]*)"/g, |
| 101 | + /'(https:\/\/videos\.openai\.com\/vg-assets\/[^']+\.mp4[^']*)'/g, |
| 102 | + /\\u[\da-f]{4}(https:\/\/videos\.openai\.com\/vg-assets\/[^\\]+\.mp4)/gi, |
| 103 | + /(https:\/\/videos\.openai\.com\/[^"'>\s\\]+\.mp4)/gi, |
| 104 | + ]; |
| 105 | + |
| 106 | + // First try to find video URL in the main HTML |
| 107 | + for (const pattern of videoPatterns) { |
| 108 | + const match = html.match(pattern); |
| 109 | + if (match) { |
| 110 | + videoUrl = match[0].replace(/^["']|["']$/g, ""); // Remove quotes |
| 111 | + break; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // If not found, search through script tags more thoroughly |
| 116 | + if (!videoUrl) { |
| 117 | + const scriptMatches = html.match(/<script[^>]*>(.*?)<\/script>/gs); |
| 118 | + if (scriptMatches) { |
| 119 | + for (const script of scriptMatches) { |
| 120 | + // Try each pattern on script content |
| 121 | + for (const pattern of videoPatterns) { |
| 122 | + const matches = script.match(pattern); |
| 123 | + if (matches) { |
| 124 | + for (const match of matches) { |
| 125 | + let candidate = match.replace(/^["']|["']$/g, ""); |
| 126 | + // Handle escaped characters |
| 127 | + candidate = candidate.replace(/\\u[\da-f]{4}/gi, ""); |
| 128 | + candidate = candidate.replace(/\\\//g, "/"); |
| 129 | + |
| 130 | + if ( |
| 131 | + candidate.includes("videos.openai.com") && |
| 132 | + candidate.includes(".mp4") |
| 133 | + ) { |
| 134 | + videoUrl = candidate; |
| 135 | + break; |
| 136 | + } |
| 137 | + } |
| 138 | + if (videoUrl) break; |
| 139 | + } |
| 140 | + } |
| 141 | + if (videoUrl) break; |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + // Extract title from HTML title tag |
| 147 | + const titleMatch = html.match(/<title>([^<]+)<\/title>/); |
| 148 | + if (titleMatch) { |
| 149 | + title = titleMatch[1].replace(" - Sora", "").replace(" | Sora", "").trim(); |
| 150 | + } |
| 151 | + |
| 152 | + // Decode HTML entities if present |
| 153 | + if (videoUrl) { |
| 154 | + videoUrl = videoUrl.replace(/&/g, "&"); |
| 155 | + } |
| 156 | + |
| 157 | + if (!videoUrl) { |
| 158 | + return { error: "fetch.empty" }; |
| 159 | + } |
| 160 | + |
| 161 | + // Generate filename |
| 162 | + const cleanId = postId.replace(/[^a-zA-Z0-9_-]/g, ""); |
| 163 | + const videoFilename = `sora_${cleanId}.mp4`; |
| 164 | + |
| 165 | + return { |
| 166 | + type: "proxy", |
| 167 | + urls: videoUrl, |
| 168 | + filename: videoFilename, |
| 169 | + fileMetadata: { |
| 170 | + title: title || `Sora Video ${cleanId}`, |
| 171 | + }, |
| 172 | + }; |
| 173 | +} |
0 commit comments