File size: 2,478 Bytes
fb888b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from io import BytesIO

import gradio as gr
from PIL import Image
from requests_html import HTMLSession

# Constants
FACEBOOK_PROFILE_URL = "https://m.facebook.com/profile.php?id=100090165138355"

# Cookies for Facebook login
COOKIES = {
    "c_user": "100049138523091",
    "datr": "FiyJZqdR_6ItSvyrxxNPrkly",
    "dpr": "1.25",
    "fr": "1daVgVcuXZ5gTpMk4.AWUr0b7-6dKtlOykP85ln02V2uc.Bmoi8K..AAA.0.0.Bmoi8K.AWXPp734bXQ",
    "m_page_voice": "100049138523091",
    "m_pixel_ratio": "1.25",
    "presence": r"C%7B%22t3%22%3A%5B%5D%2C%22utc3%22%3A1721904913794%2C%22v%22%3A1%7D",
    "ps_l": "1",
    "ps_n": "1",
    "sb": "GyyJZrXIlxVPLZLGofrwrPYO",
    "wd": "1495x715",
    "x-referer": "eyJyIjoiL2hvbWUucGhwIiwiaCI6Ii9ob21lLnBocCIsInMiOiJtIn0%3D",
    "xs": "10%3AEGJu9zqGmTY_ug%3A2%3A1720265796%3A-1%3A12016%3A%3AAcUdSrluYLs6Yh9hjoEgKt1uLdU28tp0rjj6SiuL3A",
}


def fetch_img_url() -> str:
    """Fetch the image URL from Facebook."""
    with HTMLSession() as session:
        response = session.get(FACEBOOK_PROFILE_URL, cookies=COOKIES, timeout=(100, 100)).text

        # Extract photo URL
        photo_url = extract_url(response, "https://m.facebook.com/photo.php?fbid=", '"').replace("&", "&")

        # Fetch photo page and extract image URL
        photo_response = session.get(photo_url, cookies=COOKIES, timeout=(100, 100)).text

        # Full url https://scontent.fbkk6-2.fna.fbcdn.net/
        img_url = extract_url(photo_response, "https://scontent.fbkk", '"')
        return img_url.replace("&", "&")


def extract_url(text: str, start_str: str, end_str: str) -> str:
    """Extract URL from text between start_str and end_str."""
    start = text.find(start_str)
    end = text.find(end_str, start)
    return text[start:end]


def get_img(img_url: str) -> Image:
    """Get the image from the URL."""
    with HTMLSession() as session:
        response = session.get(img_url, timeout=(100, 100))
        return Image.open(BytesIO(response.content))


with gr.Blocks(css="footer {visibility: hidden}") as app:
    img = gr.Image(
        interactive=False,
        type="pil",
        show_download_button=False,
        show_label=False,
        height="100vh",
        min_width="100vw",
        container=False,
    )

    def update_img():
        img_url = fetch_img_url()
        print(f"Image URL: {img_url}")
        return get_img(img_url)

    app.load(update_img, inputs=None, outputs=img)

if __name__ == "__main__":
    app.launch()