pek111 commited on
Commit
fb888b8
·
verified ·
1 Parent(s): a4aa29d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from io import BytesIO
2
+
3
+ import gradio as gr
4
+ from PIL import Image
5
+ from requests_html import HTMLSession
6
+
7
+ # Constants
8
+ FACEBOOK_PROFILE_URL = "https://m.facebook.com/profile.php?id=100090165138355"
9
+
10
+ # Cookies for Facebook login
11
+ COOKIES = {
12
+ "c_user": "100049138523091",
13
+ "datr": "FiyJZqdR_6ItSvyrxxNPrkly",
14
+ "dpr": "1.25",
15
+ "fr": "1daVgVcuXZ5gTpMk4.AWUr0b7-6dKtlOykP85ln02V2uc.Bmoi8K..AAA.0.0.Bmoi8K.AWXPp734bXQ",
16
+ "m_page_voice": "100049138523091",
17
+ "m_pixel_ratio": "1.25",
18
+ "presence": r"C%7B%22t3%22%3A%5B%5D%2C%22utc3%22%3A1721904913794%2C%22v%22%3A1%7D",
19
+ "ps_l": "1",
20
+ "ps_n": "1",
21
+ "sb": "GyyJZrXIlxVPLZLGofrwrPYO",
22
+ "wd": "1495x715",
23
+ "x-referer": "eyJyIjoiL2hvbWUucGhwIiwiaCI6Ii9ob21lLnBocCIsInMiOiJtIn0%3D",
24
+ "xs": "10%3AEGJu9zqGmTY_ug%3A2%3A1720265796%3A-1%3A12016%3A%3AAcUdSrluYLs6Yh9hjoEgKt1uLdU28tp0rjj6SiuL3A",
25
+ }
26
+
27
+
28
+ def fetch_img_url() -> str:
29
+ """Fetch the image URL from Facebook."""
30
+ with HTMLSession() as session:
31
+ response = session.get(FACEBOOK_PROFILE_URL, cookies=COOKIES, timeout=(100, 100)).text
32
+
33
+ # Extract photo URL
34
+ photo_url = extract_url(response, "https://m.facebook.com/photo.php?fbid=", '"').replace("&", "&")
35
+
36
+ # Fetch photo page and extract image URL
37
+ photo_response = session.get(photo_url, cookies=COOKIES, timeout=(100, 100)).text
38
+
39
+ # Full url https://scontent.fbkk6-2.fna.fbcdn.net/
40
+ img_url = extract_url(photo_response, "https://scontent.fbkk", '"')
41
+ return img_url.replace("&", "&")
42
+
43
+
44
+ def extract_url(text: str, start_str: str, end_str: str) -> str:
45
+ """Extract URL from text between start_str and end_str."""
46
+ start = text.find(start_str)
47
+ end = text.find(end_str, start)
48
+ return text[start:end]
49
+
50
+
51
+ def get_img(img_url: str) -> Image:
52
+ """Get the image from the URL."""
53
+ with HTMLSession() as session:
54
+ response = session.get(img_url, timeout=(100, 100))
55
+ return Image.open(BytesIO(response.content))
56
+
57
+
58
+ with gr.Blocks(css="footer {visibility: hidden}") as app:
59
+ img = gr.Image(
60
+ interactive=False,
61
+ type="pil",
62
+ show_download_button=False,
63
+ show_label=False,
64
+ height="100vh",
65
+ min_width="100vw",
66
+ container=False,
67
+ )
68
+
69
+ def update_img():
70
+ img_url = fetch_img_url()
71
+ print(f"Image URL: {img_url}")
72
+ return get_img(img_url)
73
+
74
+ app.load(update_img, inputs=None, outputs=img)
75
+
76
+ if __name__ == "__main__":
77
+ app.launch()