import requests
from bs4 import BeautifulSoup
import logging
import re
import time
import os

# ───── تنظیم لاگ برای ردیابی خطاها ─────
logging.basicConfig(
    filename='scraper_errors.log',
    level=logging.INFO,
    format='%(asctime)s - %(message)s'
)

# ───── خواندن کوکی ورود از مسیر بیرون پوشه importer ─────
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  # یک پوشه بالاتر
logedin_path = os.path.join(base_dir, 'logedin.txt')

try:
    with open(logedin_path, 'r') as f:
        logedin = f.readline().strip()
        cookies_base = {
            'wordpress_logged_in_7a04687845b0293923d85c8b5afee67b': logedin,
            'hide_top_message': '1'
        }
except FileNotFoundError:
    print("❌ Error: logedin.txt not found in parent directory!")
    exit()

# ───── لینک‌های لیست ─────
links = ['https://zhomis.info/series/page/', 'https://zhomis.info/all-movie/page/']

headers = {
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'accept-language': 'en-US,en;q=0.9,fa;q=0.8',
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
}

# ───── استخراج لینک‌ها از صفحات لیست ─────
def get_links_from_page(link, page):
    try:
        response = requests.get(f"{link}{page}/?sortby=modified", cookies=cookies_base, headers=headers, timeout=50)
        response.raise_for_status()
        soup = BeautifulSoup(response.text, 'html.parser')
        l = []

        for item in soup.find_all('div', class_='inner_cover'):
            try:
                a_tag = item.find('a', class_='bgbackitem')
                name = a_tag['title'] if a_tag and 'title' in a_tag.attrs else None
                href = a_tag['href'] if a_tag and 'href' in a_tag.attrs else None

                if name and href:
                    l.append((name, href))
                else:
                    logging.info(f"Skipping item on page {link}{page}: name={name}, href={href}")
            except Exception as e:
                logging.error(f"Error processing item on page {link}{page}: {str(e)}")

        return l

    except Exception as e:
        logging.error(f"Failed to process page {link}{page}: {str(e)}")
        return []

# ───── استخراج جزئیات (ID و IMDb) از صفحه محتوا ─────
def get_details_from_page(link):
    try:
        response = requests.get(link, cookies=cookies_base, headers=headers, timeout=15)
        response.raise_for_status()
        soup = BeautifulSoup(response.text, 'html.parser')

        # استخراج id از shortlink
        id = ''
        for shortlink in soup.find_all('link', rel='shortlink'):
            if '/?p=' in shortlink.get('href', ''):
                id = str(shortlink['href']).split('?p=')[1].strip()

        # استخراج کد IMDB
        imdb = ''
        for a_tag in soup.find_all('a'):
            href = a_tag.get('href', '')
            if 'imdb.com/title/' in href:
                match = re.search(r'tt\d+', href)
                if match:
                    imdb = match.group(0)
                    break

        return id, imdb

    except Exception as e:
        logging.error(f"Error processing details for {link}: {str(e)}")
        return None, None

# ───── خواندن idهای موجود از فایل ─────
ids_zar = []
ids_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'zarids.txt')

try:
    with open(ids_file, 'r') as lines:
        for line in lines:
            ids_zar.append(str(line.split(';')[0]))
except FileNotFoundError:
    logging.warning("File 'zarids.txt' not found, starting with empty list")

# ───── استخراج لینک‌ها از صفحات ─────
out = []
pages = 9  # برای تست، تعداد صفحات کم
for link in links:
    for page in range(1, pages + 1):
        print(f"Processing page: {link}{page}")
        l = get_links_from_page(link, page)
        out.extend(l)

# حذف موارد تکراری
out = list(set(out))

# ───── پردازش صفحات جزئیات ─────
for name, href in out:
    try:
        id, imdb = get_details_from_page(href)
        if id and id not in ids_zar:
            with open(ids_file, 'a') as add:
                add.write(f"{id};{imdb}\n")
            ids_zar.append(id)  # اضافه کردن به لیست برای جلوگیری از تکرار
            logging.info(f"Added ID {id} with IMDB {imdb} for {name}")
        else:
            logging.info(f"Skipped: ID {id} already exists or invalid for {name}")
        time.sleep(0.5)  # تأخیر برای جلوگیری از فشار به سرور
    except Exception as e:
        logging.error(f"Error processing {href}: {str(e)}")
