1
+ # ALL Imports
2
+ import time
3
+ from tkinter .ttk import *
4
+ import tkinter as tk
5
+ from requests import get , HTTPError , ConnectionError
6
+ from re import findall
7
+ from urllib .parse import unquote
8
+ from threading import Thread
9
+ import queue
10
+ from queue import Empty
11
+
12
+ def Invalid_Url ():
13
+ """ Sets Status bar label to error message """
14
+ Status ["text" ] = "Invalid URL..."
15
+ Status ["fg" ] = "red"
16
+
17
+ def get_downloadlink (url ):
18
+
19
+ url = url .replace ("www" , "mbasic" )
20
+ try :
21
+ r = get (url , timeout = 5 , allow_redirects = True )
22
+ if r .status_code != 200 :
23
+ raise HTTPError
24
+ a = findall ("/video_redirect/" , r .text )
25
+ if len (a ) == 0 :
26
+ print ("[!] Video Not Found..." )
27
+ exit (0 )
28
+ else :
29
+ return unquote (r .text .split ("?src=" )[1 ].split ('"' )[0 ])
30
+ except (HTTPError , ConnectionError ):
31
+ print ("[x] Invalid URL" )
32
+ exit (1 )
33
+
34
+
35
+
36
+ def Download_vid ():
37
+
38
+ # Validates Link and download Video
39
+ global Url_Val
40
+ url = Url_Val .get ()
41
+
42
+ Status ["text" ]= "Downloading"
43
+ Status ["fg" ]= "green"
44
+
45
+
46
+ # Validating Input
47
+
48
+ if not "www.facebook.com" in url :
49
+ Invalid_Url ()
50
+ return
51
+
52
+ link = get_downloadlink (url )
53
+
54
+ start_downloading ()
55
+
56
+ download_thread = VideoDownload (link )
57
+ download_thread .start ()
58
+ monitor (download_thread )
59
+
60
+
61
+
62
+ def monitor ( download_thread ):
63
+ """ Monitor the download thread """
64
+ if download_thread .is_alive ():
65
+
66
+ try :
67
+ bar ["value" ]= queue .get (0 )
68
+ ld_window .after (10 , lambda : monitor (download_thread ))
69
+ except Empty :
70
+ pass
71
+
72
+
73
+
74
+ class VideoDownload (Thread ):
75
+
76
+ def __init__ (self , url ):
77
+ super ().__init__ ()
78
+
79
+ self .url = url
80
+
81
+ def run (self ):
82
+ """ download video"""
83
+
84
+ # save the picture to a file
85
+ block_size = 1024 # 1kB
86
+ r = get (self .url , stream = True )
87
+ total_size = int (r .headers .get ("content-length" ))
88
+
89
+ with open ('video.mp4' , 'wb' ) as file :
90
+ totaldata = 0 ;
91
+ for data in r .iter_content (block_size ):
92
+ totaldata += len (data )
93
+ per_downloaded = totaldata * 100 / total_size
94
+ queue .put (per_downloaded )
95
+ bar ['value' ] = per_downloaded
96
+ file .write (data )
97
+ time .sleep (0.01 )
98
+ file .close ()
99
+ print ("Download Finished" )
100
+
101
+ print ("Download Complete !!!" )
102
+ Status ["text" ] = "Finished!!"
103
+ Status ["fg" ] = "green"
104
+
105
+
106
+
107
+ #start download
108
+ def start_downloading ():
109
+ bar ["value" ]= 0 ;
110
+
111
+ # GUI
112
+
113
+ ld_window = tk .Tk ()
114
+ ld_window .title ("Facebook Video Downloader" )
115
+ ld_window .geometry ("400x300" )
116
+
117
+ # Label for URL Input
118
+ input_label = tk .Label (ld_window ,text = "Enter Facebook Video URL:" )
119
+ input_label .pack ()
120
+
121
+ # Input of URL
122
+ Url_Val = tk .StringVar ()
123
+ Url_Input = tk .Entry (ld_window , textvariable = Url_Val , font = ("Calibri" , 9 ))
124
+ Url_Input .place ( x = 25 ,y = 50 , width = 350 )
125
+
126
+ # Button for Download
127
+ Download_button = tk .Button (ld_window , text = "Download" , font = ("Calibri" , 9 ), command = Download_vid )
128
+ Download_button .place (x = 100 , y = 100 , width = 200 )
129
+
130
+ # Progress Bar
131
+ bar = Progressbar (ld_window , length = 350 , style = 'grey.Horizontal.TProgressbar' ,mode = 'determinate' )
132
+ bar .place (y = 200 ,width = 350 ,x = 25 )
133
+
134
+ queue = queue .Queue ()
135
+ # Text for Status of Downloading
136
+ Status = tk .Label (ld_window , text = "Hello!! :D" , fg = "blue" , font = ("Calibri" , 9 ), bd = 1 , relief = tk .SUNKEN , anchor = tk .W , padx = 3 )
137
+ Status .pack (side = tk .BOTTOM , fill = tk .X )
138
+
139
+ ld_window .mainloop ()
0 commit comments