HerzaJ commited on
Commit
6278bca
·
verified ·
1 Parent(s): fb9b458

Create sora2.js

Browse files
Files changed (1) hide show
  1. plugins/sora2.js +137 -0
plugins/sora2.js ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const axios = require('axios');
2
+
3
+ async function sora2(prompt, { ratio = 'portrait' } = {}) {
4
+ try {
5
+ if (!prompt) throw new Error('Prompt is required.');
6
+ if (!['portrait', 'landscape'].includes(ratio)) {
7
+ throw new Error('Available ratios: portrait, landscape.');
8
+ }
9
+
10
+ const uniqueId = Array.from({ length: 32 }, () =>
11
+ Math.floor(Math.random() * 16).toString(16)
12
+ ).join('');
13
+
14
+ const api = axios.create({
15
+ baseURL: 'https://api.bilo.ai/aimodels/api/v1/ai',
16
+ headers: {
17
+ 'accept': 'application/json, text/plain, */*',
18
+ 'accept-language': 'id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7',
19
+ 'content-type': 'application/json',
20
+ 'origin': 'https://bylo.ai',
21
+ 'referer': 'https://bylo.ai/',
22
+ 'sec-ch-ua': '"Chromium";v="130", "Not?A_Brand";v="99"',
23
+ 'sec-ch-ua-mobile': '?0',
24
+ 'sec-ch-ua-platform': '"Windows"',
25
+ 'sec-fetch-dest': 'empty',
26
+ 'sec-fetch-mode': 'cors',
27
+ 'sec-fetch-site': 'same-site',
28
+ 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36',
29
+ 'uniqueId': uniqueId
30
+ }
31
+ });
32
+
33
+ const { data: createResp } = await api.post('/video/create', {
34
+ prompt: prompt,
35
+ channel: 'SORA2',
36
+ pageId: 536,
37
+ source: 'bylo.ai',
38
+ watermarkFlag: false,
39
+ privateFlag: false,
40
+ isTemp: true,
41
+ model: 'sora_video2',
42
+ videoType: 'text-to-video',
43
+ aspectRatio: ratio
44
+ });
45
+
46
+ if (createResp.code !== 200 || !createResp.data) {
47
+ throw new Error(createResp.message || 'Failed to create task');
48
+ }
49
+
50
+ const taskId = createResp.data;
51
+
52
+ let attempts = 0;
53
+ const maxAttempts = 180;
54
+
55
+ while (attempts < maxAttempts) {
56
+ await new Promise(res => setTimeout(res, 3000));
57
+
58
+ const { data: statusResp } = await api.get(`/${taskId}?channel=SORA2`);
59
+
60
+ if (statusResp.code !== 200) {
61
+ throw new Error(statusResp.message || 'Failed to check status');
62
+ }
63
+
64
+ const taskData = statusResp.data;
65
+
66
+ if (taskData.state === 1 && taskData.completeData) {
67
+ const result = JSON.parse(taskData.completeData);
68
+
69
+ if (result.code === 200 && result.data) {
70
+ return {
71
+ status: 'success',
72
+ taskId: taskId,
73
+ videoUrl: result.data.result_urls?.[0] || null,
74
+ watermarkUrl: result.data.result_object?.watermark_urls?.[0] || null,
75
+ noWatermarkUrl: result.data.result_object?.no_watermark_urls?.[0] || null,
76
+ prompt: prompt,
77
+ aspectRatio: ratio
78
+ };
79
+ }
80
+ }
81
+
82
+ if (taskData.failCode || taskData.failMsg) {
83
+ throw new Error(`Generation failed: ${taskData.failMsg || taskData.failCode}`);
84
+ }
85
+
86
+ attempts++;
87
+ }
88
+
89
+ throw new Error('Timeout: Video generation took too long');
90
+
91
+ } catch (error) {
92
+ if (error.response) {
93
+ throw new Error(`API Error: ${error.response.status} - ${JSON.stringify(error.response.data)}`);
94
+ }
95
+ throw error;
96
+ }
97
+ }
98
+
99
+ const handler = async (req, res) => {
100
+ try {
101
+ const { prompt, ratio = 'portrait' } = req.query;
102
+
103
+ if (!prompt) {
104
+ return res.status(400).json({
105
+ success: false,
106
+ error: 'Missing required parameter: prompt'
107
+ });
108
+ }
109
+
110
+ const result = await sora2(prompt, { ratio });
111
+
112
+ res.json({
113
+ author: "Herza",
114
+ success: true,
115
+ data: result
116
+ });
117
+
118
+ } catch (error) {
119
+ res.status(500).json({
120
+ success: false,
121
+ error: error.message
122
+ });
123
+ }
124
+ };
125
+
126
+ module.exports = {
127
+ name: 'Sora2 Video Generator',
128
+ description: 'Generate videos using Sora2 AI model',
129
+ type: 'GET',
130
+ routes: ['api/AI/sora2'],
131
+ tags: ['ai', 'video', 'sora'],
132
+ main: ['AI'],
133
+ parameters: ['prompt', 'ratio', 'key'],
134
+ enabled: true,
135
+ limit: 10,
136
+ handler
137
+ };