crunchyroll-toolkit
Version:
Toolkit Node.js complet pour extraire données d'animés, métadonnées et thumbnails depuis Crunchyroll avec techniques anti-détection 2024/2025
120 lines (119 loc) • 4.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZenRowsBrowserManager = void 0;
const UndetectedChrome = require('undetected-chromedriver-js');
class ZenRowsBrowserManager {
constructor(options = {}) {
this.options = {
headless: true,
timeout: 15000,
maxRetries: 3,
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
locale: 'fr-FR',
...options
};
}
async initialize() {
if (!this.driver) {
console.log('🔧 Initialisation ZenRows Method (Selenium + undetected-chromedriver-js)...');
try {
// Initialiser undetected-chromedriver-js
this.undetectedChrome = new UndetectedChrome({
headless: this.options.headless,
userAgent: this.options.userAgent,
executablePath: '/usr/bin/google-chrome-stable',
args: [
'--no-sandbox',
'--disable-dev-shm-usage',
'--disable-blink-features=AutomationControlled',
'--exclude-switches=enable-automation',
'--window-size=1366,768'
]
});
console.log('🎯 Création du driver avec undetected-chromedriver-js...');
// Mode headless optionnel
if (this.options.headless) {
console.log('🔇 Mode headless activé');
}
else {
console.log('🖥️ Mode visible activé');
}
// Création du driver avec anti-détection
this.driver = await this.undetectedChrome.build();
console.log('✅ ZenRows Method: Driver initialisé avec succès');
// Timeout par défaut
await this.driver.manage().setTimeouts({ implicit: this.options.timeout });
}
catch (error) {
console.error('❌ Erreur initialisation ZenRows Method:', error);
throw error;
}
}
}
async navigateTo(url) {
if (!this.driver) {
throw new Error('Driver not initialized');
}
console.log(`🌐 Navigation ZenRows vers: ${url}`);
await this.driver.get(url);
}
async getDriver() {
if (!this.driver) {
throw new Error('Driver not initialized');
}
return this.driver;
}
async getPageSource() {
if (!this.driver) {
throw new Error('Driver not initialized');
}
return await this.driver.getPageSource();
}
async getTitle() {
if (!this.driver) {
throw new Error('Driver not initialized');
}
return await this.driver.getTitle();
}
async getCurrentUrl() {
if (!this.driver) {
throw new Error('Driver not initialized');
}
return await this.driver.getCurrentUrl();
}
async executeScript(script) {
if (!this.driver) {
throw new Error('Driver not initialized');
}
return await this.driver.executeScript(script);
}
async close() {
if (this.driver) {
try {
await this.driver.quit();
this.driver = undefined;
this.undetectedChrome = undefined;
console.log('✅ ZenRows Driver fermé');
}
catch (error) {
console.error('⚠️ Erreur fermeture driver:', error);
}
}
}
async retry(fn) {
let lastError;
for (let i = 0; i < this.options.maxRetries; i++) {
try {
return await fn();
}
catch (error) {
lastError = error;
if (i < this.options.maxRetries - 1) {
await new Promise(resolve => setTimeout(resolve, 1000 * (i + 1)));
}
}
}
throw lastError;
}
}
exports.ZenRowsBrowserManager = ZenRowsBrowserManager;