#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Test de connexion : confirme qu'on est loggé en tant que profil admin."""
import time, re
from fb_common import session_facebook

d = session_facebook()
d.get("https://www.facebook.com/me")
time.sleep(5)
url = d.current_url
html = d.page_source

cks = {c["name"]: c["value"] for c in d.get_cookies()}
c_user = cks.get("c_user")

logged = bool(c_user) and "login" not in url.lower() and "checkpoint" not in url.lower()
has_dtsg = bool(re.search(r'"DTSGInitialData",\[\],\{"token":"([^"]+)"', html))

print("URL apres /me :", url)
print("c_user present:", "OUI (id=" + c_user + ")" if c_user else "NON")
print("token DTSG present:", "OUI" if has_dtsg else "NON")
print("checkpoint/login dans URL:", "OUI" if ("login" in url.lower() or "checkpoint" in url.lower()) else "non")
print("RESULTAT:", "CONNECTE" if logged else "NON CONNECTE")

# nom du profil si possible
m = re.search(r'<title>([^<]+)</title>', html)
if m:
    print("Titre page:", m.group(1)[:120])

d.quit()

