UNPKG

@ytb-dw/sdk

Version:

Official JavaScript SDK for ytb-dw YouTube download service - Download YouTube videos and audio with ease

82 lines (64 loc) 3.45 kB
// Example usage of ytb-dw SDK const YtbDwClient = require('../index.js'); async function examples() { // Replace with your actual API key const API_KEY = 'your-api-key-here'; const client = new YtbDwClient(API_KEY); const YOUTUBE_URL = 'https://youtube.com/watch?v=dQw4w9WgXcQ'; console.log('🎬 ytb-dw SDK Examples\n'); try { // Example 1: Get video information console.log('📋 Example 1: Getting video information...'); const info = await client.getVideoInfo(YOUTUBE_URL); console.log(`Title: ${info.title}`); console.log(`Duration: ${info.duration}`); console.log(`Uploader: ${info.uploader}`); console.log(`Views: ${info.view_count?.toLocaleString()}`); console.log(`Available formats: ${info.formats?.length || 0}`); if (info.formats && info.formats.length > 0) { console.log('\nFormats:'); info.formats.forEach(format => { console.log(` - ${format.type} (${format.quality}) - ${format.size}`); }); } // Example 2: Generate download URLs console.log('\n🔗 Example 2: Generating download URLs...'); const audioUrl = client.getDownloadUrl(YOUTUBE_URL, { format: 'audio' }); console.log('Audio download URL:', audioUrl); const videoUrl = client.getDownloadUrl(YOUTUBE_URL, { format: 'video', quality: '720' }); console.log('Video download URL (720p):', videoUrl); // Example 3: Download to file (uncomment to test actual downloads) /* console.log('\n⬇️ Example 3: Downloading files...'); // Download audio await client.downloadToFile(YOUTUBE_URL, './downloads/audio.mp3', { format: 'audio' }); console.log('✅ Audio downloaded to ./downloads/audio.mp3'); // Download video await client.downloadToFile(YOUTUBE_URL, './downloads/video.mp4', { format: 'video', quality: '720' }); console.log('✅ Video downloaded to ./downloads/video.mp4'); */ console.log('\n🎉 Exemples terminés avec succès!'); if (API_KEY === 'demo-api-key') { console.log('\n💡 Pour tester avec une vraie API:'); console.log(' export YTB_DW_API_KEY=votre-cle-api'); console.log(' npm run example'); } } catch (error) { console.error('\n❌ Erreur:', error.message); if (error.message.includes('Invalid or inactive API key')) { console.log('\n💡 Solutions:'); console.log(' 1. Vérifiez votre clé API sur ytb-dw.social-networking.me'); console.log(' 2. Définissez: export YTB_DW_API_KEY=votre-vraie-cle'); } else if (error.message.includes('fetch') || error.message.includes('ENOTFOUND')) { console.log('\n💡 Vérifiez votre connexion internet'); } else if (error.message.includes('quota') || error.message.includes('429')) { console.log('\n💡 Quota quotidien dépassé - attendez minuit ou upgradez votre compte'); } console.log('\n📚 Documentation complète: https://ytb-dw.social-networking.me/docs'); } } // Run examples if called directly if (require.main === module) { examples(); } module.exports = examples;