const { google } = require('googleapis'); const fs = require('fs'); const path = require('path'); /** * YouTube Uploader Utility */ async function uploadToYouTube(videoPath, metadata) { const { custom_client_id, custom_client_secret } = metadata; let client_id, client_secret, redirect_uris; if (!custom_client_id || !custom_client_secret) { throw new Error('❌ YouTube Production Failed: Custom Client ID or Secret missing for this user.'); } client_id = custom_client_id; client_secret = custom_client_secret; redirect_uris = [(process.env.SPACE_ID || process.env.NODE_ENV === 'production') ? 'https://yogesh7705-youtube-auto-bot.hf.space/api/auth/callback' : 'http://localhost:7860/api/auth/callback']; const oauth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]); // Use passed token const token = metadata.token; oauth2Client.setCredentials(token); const youtube = google.youtube({ version: 'v3', auth: oauth2Client }); const res = await youtube.videos.insert({ part: 'snippet,status', requestBody: { snippet: { title: metadata.title, description: metadata.description + '\n\n' + metadata.hashtags.join(' '), tags: metadata.hashtags.map(h => h.replace('#', '')), categoryId: '27', // Education/Tech defaultLanguage: 'en', defaultAudioLanguage: 'en' }, status: { privacyStatus: 'public', selfDeclaredMadeForKids: false }, }, media: { body: fs.createReadStream(videoPath), }, }); return res.data; } module.exports = { uploadToYouTube };