ملاحظة: بعد الحفظ، قد يلزمك إفراغ الكاش لرؤية التغييرات.

const fetch = require('node-fetch');
const fs = require('fs');
const path = require('path');

// بيانات الاعتماد الخاصة بك في Copyleaks
const API_EMAIL = 'your-email@example.com';
const API_KEY = 'your-api-key';

// توثيق مع Copyleaks API
async function authenticate(api_email, api_key) {
    const url = 'https://api.copyleaks.com/v3/account/login/api';
    const payload = {
        email: api_email,
        key: api_key
    };
    const response = await fetch(url, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(payload)
    });

    if (response.ok) {
        const data = await response.json();
        return data.access_token;
    } else {
        throw new Error('فشل التوثيق: ' + response.statusText);
    }
}

// إرسال المحتوى لفحص الانتحال
async function submitForCheck(token, content) {
    const url = 'https://api.copyleaks.com/v3/education/submit/file';
    const encodedContent = Buffer.from(content).toString('base64');
    const payload = {
        base64: encodedContent,
        filename: 'content.txt'
    };

    const response = await fetch(url, {
        method: 'POST',
        headers: {
            'Authorization': 'Bearer ' + token,
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(payload)
    });

    if (response.ok) {
        const data = await response.json();
        return data.id;
    } else {
        throw new Error('فشل إرسال المحتوى: ' + response.statusText);
    }
}

// التحقق من حالة فحص الانتحال
async function checkStatus(token, scanId) {
    const url = `https://api.copyleaks.com/v3/education/${scanId}/status`;
    const response = await fetch(url, {
        headers: {
            'Authorization': 'Bearer ' + token
        }
    });

    if (response.ok) {
        const data = await response.json();
        return data;
    } else {
        throw new Error('فشل التحقق من الحالة: ' + response.statusText);
    }
}

// استرجاع تقرير الانتحال
async function getReport(token, scanId) {
    const url = `https://api.copyleaks.com/v3/education/${scanId}/result`;
    const response = await fetch(url, {
        headers: {
            'Authorization': 'Bearer ' + token
        }
    });

    if (response.ok) {
        const data = await response.json();
        return data;
    } else {
        throw new Error('فشل استرجاع التقرير: ' + response.statusText);
    }
}

// الدالة الرئيسية
async function main(content) {
    try {
        const token = await authenticate(API_EMAIL, API_KEY);
        const scanId = await submitForCheck(token, content);

        // الانتظار حتى يكتمل الفحص
        while (true) {
            const status = await checkStatus(token, scanId);
            if (status.status === 'completed') {
                break;
            }
            await new Promise(resolve => setTimeout(resolve, 30000)); // الانتظار لمدة 30 ثانية قبل التحقق مرة أخرى
        }

        const report = await getReport(token, scanId);
        console.log(JSON.stringify(report, null, 4));
    } catch (error) {
        console.error(error.message);
    }
}

// استخدام الكود
const content = 'ضع محتوى ويكيبيديا هنا';
main(content);