| import asyncio |
| from pathlib import Path |
| from playwright.async_api import async_playwright, TimeoutError as PlaywrightTimeoutError |
|
|
| async def post_to_facebook(email: str, password: str, message: str, image_path: str = None): |
| async with async_playwright() as p: |
| browser = await p.chromium.launch(headless=False) |
| context = await browser.new_context() |
| page = await context.new_page() |
|
|
| try: |
| print("[*] تسجيل الدخول...") |
| await page.goto("https://www.facebook.com/", timeout=30000) |
| |
| await page.wait_for_selector('input[name="email"]', timeout=15000) |
| await page.fill('input[name="email"]', email) |
| await page.fill('input[name="pass"]', password) |
| await page.click('button[name="login"]') |
|
|
| |
| await page.wait_for_selector('div[aria-label="إنشاء منشور"]', timeout=20000) |
|
|
| print("[*] فتح نافذة المنشور...") |
| await page.click('div[aria-label="إنشاء منشور"]') |
| await page.wait_for_selector('div[role="textbox"]', timeout=10000) |
|
|
| |
| textbox = await page.query_selector('div[role="textbox"]') |
| await textbox.focus() |
| await page.keyboard.type(message, delay=50) |
|
|
| |
| if image_path and Path(image_path).is_file(): |
| print("[*] رفع الصورة...") |
| file_input = await page.query_selector('input[type="file"]') |
| if file_input: |
| await file_input.set_input_files(image_path) |
| |
| await page.wait_for_timeout(5000) |
| else: |
| print("[!] لم يتم العثور على عنصر رفع الملف.") |
|
|
| print("[*] النشر جارٍ...") |
| await page.click('div[aria-label="نشر"]') |
| |
| await page.wait_for_timeout(5000) |
|
|
| print("[✓] تم النشر بنجاح.") |
|
|
| except PlaywrightTimeoutError as te: |
| print("[!] انتهت مهلة الانتظار:", te) |
| except Exception as e: |
| print("[!] حدث خطأ:", e) |
| finally: |
| await browser.close() |
| print("[*] تم إغلاق المتصفح.") |
|
|
| |
| if __name__ == "__main__": |
| email = "geregesdodi@gmail.com" |
| password = "osama1986" |
| message = "منشور جديد مع صورة!" |
| image_path = "path/to/your/image.jpg" |
| asyncio.run(post_to_facebook(email, password, message, image_path)) |
|
|