|
1 | | -import { NextResponse } from 'next/server'; |
| 1 | +import { NextResponse } from "next/server"; |
| 2 | + |
| 3 | +const SORA_UA = |
| 4 | + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"; |
| 5 | + |
| 6 | +function extractFromHtml(html: string): { mp4Url: string; prompt: string } { |
| 7 | + let mp4Url = ""; |
| 8 | + let prompt = ""; |
| 9 | + |
| 10 | + const metaVideoMatch = html.match(/<meta property="og:video" content="([^"]+)"/i); |
| 11 | + if (metaVideoMatch?.[1]) { |
| 12 | + mp4Url = metaVideoMatch[1]; |
| 13 | + } else { |
| 14 | + const videoMatch = html.match(/"([^"]*\.mp4[^"]*)"/i); |
| 15 | + if (videoMatch?.[1]) mp4Url = videoMatch[1]; |
| 16 | + } |
| 17 | + |
| 18 | + mp4Url = mp4Url.replace(/\\u0026/g, "&"); |
| 19 | + |
| 20 | + const metaDescMatch = html.match(/<meta property="og:description" content="([^"]+)"/i); |
| 21 | + if (metaDescMatch?.[1]) { |
| 22 | + prompt = metaDescMatch[1].trim(); |
| 23 | + } else { |
| 24 | + const titleMatch = html.match(/<title>([^<]+)<\/title>/i); |
| 25 | + if (titleMatch?.[1]) prompt = titleMatch[1].replace(/Sora/gi, "").trim(); |
| 26 | + } |
| 27 | + |
| 28 | + prompt = prompt.replace(/"/g, '"').replace(/'/g, "'"); |
| 29 | + |
| 30 | + return { mp4Url, prompt }; |
| 31 | +} |
| 32 | + |
| 33 | +async function safeFetch(url: string): Promise<Response | null> { |
| 34 | + try { |
| 35 | + return await fetch(url, { headers: { "User-Agent": SORA_UA } }); |
| 36 | + } catch { |
| 37 | + return null; |
| 38 | + } |
| 39 | +} |
2 | 40 |
|
3 | 41 | export async function POST(request: Request) { |
4 | 42 | try { |
5 | | - const body = await request.json(); |
6 | | - let url = body.url; |
| 43 | + const body = (await request.json()) as { url?: string }; |
| 44 | + const url = body.url?.trim() ?? ""; |
7 | 45 |
|
8 | 46 | if (!url) { |
9 | | - return NextResponse.json( |
10 | | - { error: 'No URL provided.' }, |
11 | | - { status: 400 } |
12 | | - ); |
| 47 | + return NextResponse.json({ error: "No URL provided." }, { status: 400 }); |
13 | 48 | } |
14 | 49 |
|
15 | | - if (!url.startsWith('https://sora.chatgpt.com/')) { |
| 50 | + if (!url.startsWith("https://sora.chatgpt.com/")) { |
16 | 51 | return NextResponse.json( |
17 | | - { error: 'RemoveBanana requires a Sora share URL starting with https://sora.chatgpt.com/' }, |
| 52 | + { error: "RemoveBanana requires a Sora URL starting with https://sora.chatgpt.com/." }, |
18 | 53 | { status: 400 } |
19 | 54 | ); |
20 | 55 | } |
21 | 56 |
|
22 | | - // Fetch the raw HTML of the Sora page |
23 | | - const response = await fetch(url, { |
24 | | - headers: { |
25 | | - 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36' |
26 | | - } |
27 | | - }); |
| 57 | + let mp4Url = ""; |
| 58 | + let prompt = ""; |
28 | 59 |
|
29 | | - if (!response.ok) { |
30 | | - throw new Error(`HTTP error! status: ${response.status}`); |
31 | | - } |
| 60 | + const genMatch = url.match(/\/g\/(gen_[a-zA-Z0-9]+)/); |
| 61 | + if (genMatch) { |
| 62 | + const genId = genMatch[1]; |
| 63 | + const apiRes = await safeFetch(`https://sora.chatgpt.com/backend-api/video/generations/${genId}`); |
| 64 | + |
| 65 | + if (apiRes?.ok) { |
| 66 | + try { |
| 67 | + const data = (await apiRes.json()) as Record<string, unknown>; |
| 68 | + mp4Url = typeof data.video_url === "string" ? data.video_url : typeof data.url === "string" ? data.url : ""; |
| 69 | + prompt = |
| 70 | + typeof data.prompt === "string" |
| 71 | + ? data.prompt |
| 72 | + : typeof data.caption === "string" |
| 73 | + ? data.caption |
| 74 | + : ""; |
| 75 | + } catch { |
| 76 | + // Ignore non-JSON responses and fall back to HTML scraping. |
| 77 | + } |
| 78 | + } |
32 | 79 |
|
33 | | - const html = await response.text(); |
| 80 | + if (!mp4Url) { |
| 81 | + const htmlRes = await safeFetch(url); |
| 82 | + if (!htmlRes) { |
| 83 | + return NextResponse.json( |
| 84 | + { error: "Could not reach the Sora link. Verify the URL and try again." }, |
| 85 | + { status: 400 } |
| 86 | + ); |
| 87 | + } |
34 | 88 |
|
35 | | - // The Sora video URL is typically embedded in the meta tags or script tags payload |
36 | | - let mp4Url = ''; |
37 | | - let prompt = ''; |
| 89 | + if (!htmlRes.ok) { |
| 90 | + if (htmlRes.status === 401 || htmlRes.status === 403) { |
| 91 | + return NextResponse.json( |
| 92 | + { |
| 93 | + error: |
| 94 | + "This /g/ link is private and requires login. Use Sora Share to create a public /p/ link.", |
| 95 | + }, |
| 96 | + { status: 400 } |
| 97 | + ); |
| 98 | + } |
38 | 99 |
|
39 | | - // 1. Try to extract MP4 URL |
40 | | - // Look for cdns that OpenAI uses for video or regular mp4 og:video tags |
41 | | - const videoUrlRegex = /"([^"]*\.mp4[^"]*)"/i; |
42 | | - const metaVideoRegex = /<meta property="og:video" content="([^"]+)"/i; |
| 100 | + return NextResponse.json( |
| 101 | + { error: `Sora returned HTTP ${htmlRes.status}. Try using a public /p/ share link.` }, |
| 102 | + { status: 400 } |
| 103 | + ); |
| 104 | + } |
43 | 105 |
|
44 | | - // We try looking for og:video first, as it's the standard way |
45 | | - const metaVideoMatch = html.match(metaVideoRegex); |
46 | | - if (metaVideoMatch && metaVideoMatch[1]) { |
47 | | - mp4Url = metaVideoMatch[1]; |
| 106 | + const html = await htmlRes.text(); |
| 107 | + ({ mp4Url, prompt } = extractFromHtml(html)); |
| 108 | + } |
48 | 109 | } else { |
49 | | - // Fallback to searching for an mp4 url within the HTML |
50 | | - const videoMatch = html.match(videoUrlRegex); |
51 | | - if (videoMatch && videoMatch[1]) { |
52 | | - mp4Url = videoMatch[1]; |
| 110 | + const htmlRes = await safeFetch(url); |
| 111 | + if (!htmlRes) { |
| 112 | + return NextResponse.json( |
| 113 | + { error: "Could not reach the Sora link. Verify the URL and try again." }, |
| 114 | + { status: 400 } |
| 115 | + ); |
53 | 116 | } |
54 | | - } |
55 | 117 |
|
56 | | - // Unescape the URL if it's JSON encoded |
57 | | - mp4Url = mp4Url.replace(/\\u0026/g, '&'); |
| 118 | + if (!htmlRes.ok) { |
| 119 | + if (htmlRes.status === 401 || htmlRes.status === 403) { |
| 120 | + return NextResponse.json( |
| 121 | + { error: "This link is private. Use Sora Share to generate a public /p/ link." }, |
| 122 | + { status: 400 } |
| 123 | + ); |
| 124 | + } |
58 | 125 |
|
59 | | - // 2. Try to extract the prompt |
60 | | - // Look for og:description or title |
61 | | - const metaDescRegex = /<meta property="og:description" content="([^"]+)"/i; |
62 | | - const metaDescMatch = html.match(metaDescRegex); |
63 | | - if (metaDescMatch && metaDescMatch[1]) { |
64 | | - prompt = metaDescMatch[1].trim(); |
65 | | - } else { |
66 | | - const titleRegex = /<title>([^<]+)<\/title>/i; |
67 | | - const titleMatch = html.match(titleRegex); |
68 | | - if (titleMatch && titleMatch[1]) { |
69 | | - prompt = titleMatch[1].replace('Sora', '').trim(); |
| 126 | + return NextResponse.json( |
| 127 | + { error: `Sora returned HTTP ${htmlRes.status}. Make sure the link is public.` }, |
| 128 | + { status: 400 } |
| 129 | + ); |
70 | 130 | } |
71 | | - } |
72 | 131 |
|
73 | | - prompt = prompt.replace(/"/g, '"').replace(/'/g, "'"); |
| 132 | + const html = await htmlRes.text(); |
| 133 | + ({ mp4Url, prompt } = extractFromHtml(html)); |
| 134 | + } |
74 | 135 |
|
75 | 136 | if (!mp4Url) { |
76 | 137 | return NextResponse.json( |
77 | | - { error: 'RemoveBanana could not extract a video from that link. Make sure it is a valid public Sora share link.' }, |
| 138 | + { |
| 139 | + error: |
| 140 | + "Could not find a video in this link. For /g/ URLs, the generation may be private. Use a public /p/ share link.", |
| 141 | + }, |
78 | 142 | { status: 400 } |
79 | 143 | ); |
80 | 144 | } |
81 | 145 |
|
82 | 146 | return NextResponse.json({ |
83 | | - prompt: prompt || 'Prompt not found.', |
84 | | - mp4Url: mp4Url |
| 147 | + prompt: prompt || "Prompt not available.", |
| 148 | + mp4Url, |
85 | 149 | }); |
86 | | - |
87 | 150 | } catch (error) { |
88 | | - console.error('RemoveBanana Sora extraction error:', error); |
| 151 | + console.error("RemoveBanana Sora extraction error:", error); |
89 | 152 | return NextResponse.json( |
90 | | - { error: 'RemoveBanana hit an error while fetching the Sora video.' }, |
| 153 | + { error: "An unexpected error occurred. Please try again with a valid public Sora link." }, |
91 | 154 | { status: 500 } |
92 | 155 | ); |
93 | 156 | } |
|
0 commit comments