# Reproduit EXACTEMENT la methode fb_scrape.py (add_cookie + /saved/)
import json, time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
UA=("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
    "(KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36")
def charger():
    t=open("/home/videoweil/videodl/cookies_fb.txt",encoding="utf-8",errors="replace").read().strip()
    arr=json.loads(t)
    if isinstance(arr,dict): arr=arr.get("cookies",[])
    return [{"name":c.get("name"),"value":c.get("value",""),"domain":".facebook.com",
             "path":c.get("path","/") or "/"} for c in arr if "facebook" in (c.get("domain") or "")]
o=Options()
for a in ["--headless=new","--no-sandbox","--disable-dev-shm-usage","--disable-gpu",
          "--window-size=1366,2400","--lang=fr-FR","--user-agent="+UA]:
    o.add_argument(a)
o.binary_location="/usr/bin/google-chrome"
d=webdriver.Chrome(service=Service("/usr/local/bin/chromedriver"),options=o)
d.set_page_load_timeout(50)
d.get("https://www.facebook.com/"); time.sleep(2)
for c in charger():
    try: d.add_cookie(c)
    except Exception: pass
d.get("https://www.facebook.com/saved/"); time.sleep(5)
print("URL /saved/ :", d.current_url)
print("saved dans URL:", "saved" in d.current_url)
cks={c["name"]:1 for c in d.get_cookies()}
print("c_user present:", "c_user" in cks)
print("login dans URL:", "login" in d.current_url.lower())
d.quit()
