1+ # ALL Imports
2+
3+ import tkinter as tk
4+ from requests import get , HTTPError , ConnectionError
5+ from re import findall
6+ from urllib .parse import unquote
7+
8+
9+ def Invalid_Url ():
10+ """ Sets Status bar label to error message """
11+ Status ["text" ] = "Invalid URL..."
12+ Status ["fg" ] = "red"
13+
14+ def get_downloadlink (url ):
15+
16+ url = url .replace ("www" , "mbasic" )
17+ try :
18+ r = get (url , timeout = 5 , allow_redirects = True )
19+ if r .status_code != 200 :
20+ raise HTTPError
21+ a = findall ("/video_redirect/" , r .text )
22+ if len (a ) == 0 :
23+ print ("[!] Video Not Found..." )
24+ exit (0 )
25+ else :
26+ return unquote (r .text .split ("?src=" )[1 ].split ('"' )[0 ])
27+ except (HTTPError , ConnectionError ):
28+ print ("[x] Invalid URL" )
29+ exit (1 )
30+
31+
32+
33+ def Download_vid ():
34+
35+ # Validates Link and download Video
36+ Download_Window .delete ("0.0" , "end" )
37+ global Url_Val
38+ url = Url_Val .get ()
39+
40+ Status ["text" ]= "Downloading"
41+ Status ["fg" ]= "green"
42+
43+
44+ # Validating Input
45+
46+ if not "www.facebook.com" in url :
47+ Invalid_Url ()
48+ return
49+
50+ link = get_downloadlink (url )
51+
52+ download (link )
53+
54+ Status ["text" ] = "Finished!!"
55+ Status ["fg" ] = "green"
56+
57+ def download (url ):
58+ block_size = 1024 # 1kB
59+ r = get (url , stream = True )
60+ total_size = int (r .headers .get ("content-length" ))
61+
62+ with open ('video.mp4' , 'wb' ) as file :
63+ totaldata = 0 ;
64+ for data in r .iter_content (block_size ):
65+ totaldata += len (data )
66+ per_downloaded = totaldata * 100 / total_size
67+ Download_Window .delete ("1.0" ,"end" )
68+ Download_Window .insert (tk .END ,f"Dowloaded.... { per_downloaded } %" )
69+ file .write (data )
70+ file .close ()
71+ print ("Download Finished" )
72+
73+ print ("Download Complete !!!" )
74+
75+ pass
76+
77+
78+ # GUI
79+
80+ ld_window = tk .Tk ()
81+ ld_window .title ("Facebook Video Downloader" )
82+ ld_window .geometry ("400x400" )
83+
84+ # Label for URL Input
85+ input_label = tk .Label (ld_window ,text = "Enter Facebook Video URL:" )
86+ input_label .pack ()
87+
88+ # Input of URL
89+ Url_Val = tk .StringVar ()
90+ Url_Input = tk .Entry (ld_window , textvariable = Url_Val , font = ("Calibri" , 9 ))
91+ Url_Input .place ( x = 25 ,y = 50 , width = 350 )
92+
93+ # Button for Download
94+ Download_button = tk .Button (ld_window , text = "Download" , font = ("Calibri" , 9 ), command = Download_vid )
95+ Download_button .place (x = 100 , y = 100 , width = 200 )
96+
97+ # Download Window
98+
99+ Download_Window = tk .Text (ld_window , font = ("Calibri" , 9 ), bg = "black" , fg = "white" , bd = 1 , relief = tk .SUNKEN , wrap = tk .WORD )
100+ Download_Window .insert (tk .END , "Welcome to Facebook Video Downloader, Provide a Facebook video link in the above box and click download to start the process. :D" )
101+ Download_Window .place (x = 25 , y = 200 , width = 350 , height = 250 )
102+
103+ # Text for Status of Downloading
104+ Status = tk .Label (ld_window , text = "Hello!! :D" , fg = "blue" , font = ("Calibri" , 9 ), bd = 1 , relief = tk .SUNKEN , anchor = tk .W , padx = 3 )
105+ Status .pack (side = tk .BOTTOM , fill = tk .X )
106+
107+ ld_window .mainloop ()
0 commit comments