3
3
import tkinter as tk
4
4
import requests as req
5
5
import html
6
+ import time
7
+ from tkinter .ttk import *
8
+ from threading import Thread
9
+ import queue
10
+ from queue import Empty
6
11
7
12
8
13
def Invalid_Url ():
9
14
""" Sets Status bar label to error message """
10
- Download_Window .insert (tk .END ,f"Invalid URL" )
11
15
Status ["text" ] = "Invalid URL..."
12
16
Status ["fg" ] = "red"
13
17
14
18
15
19
def Download_vid ():
16
20
17
21
# Validates Link and download Video
18
- Download_Window .delete ("0.0" , "end" )
19
22
global Url_Val
20
23
url = Url_Val .get ()
21
24
@@ -44,38 +47,72 @@ def Download_vid():
44
47
if "dms.licdn.com" in source :
45
48
46
49
videourl = source .split (',' )[0 ].split ('"src":' )[1 ][1 :- 1 ]
50
+ start_downloading ()
47
51
48
- r = req .get (videourl , stream = True )
52
+ download_thread = VideoDownload (videourl )
53
+ download_thread .start ()
54
+ monitor (download_thread )
55
+ break
49
56
50
- total_size = int (r .headers .get ('content-length' , 0 ))
51
57
52
- block_size = 1024
58
+ class VideoDownload (Thread ):
59
+
60
+ def __init__ (self , url ):
61
+ super ().__init__ ()
53
62
54
- with open ('video.mp4' , 'wb' ) as file :
55
- totaldata = 0 ;
56
- for data in r .iter_content (block_size ):
57
- totaldata += len (data )
58
- per_downloaded = totaldata * 100 / total_size
59
- Download_Window .delete ("1.0" ,"end" )
60
- Download_Window .insert (tk .END ,f"Dowloaded.... { per_downloaded } %" )
61
- file .write (data )
62
- print ("Download Finished" )
63
- break
63
+ self .url = url
64
+
65
+ def run (self ):
66
+ """ download video"""
67
+
68
+ # save the picture to a file
69
+ block_size = 1024 # 1kB
70
+ r = req .get (self .url , stream = True )
71
+ total_size = int (r .headers .get ("content-length" ))
72
+
73
+ with open ('video.mp4' , 'wb' ) as file :
74
+ totaldata = 0 ;
75
+ for data in r .iter_content (block_size ):
76
+ totaldata += len (data )
77
+ per_downloaded = totaldata * 100 / total_size
78
+ queue .put (per_downloaded )
79
+ bar ['value' ] = per_downloaded
80
+ file .write (data )
81
+ time .sleep (0.01 )
82
+ file .close ()
83
+ print ("Download Finished" )
64
84
65
- Status ["text" ] = "Finished!!"
66
- Status ["fg" ] = "green"
85
+ print ("Download Complete !!!" )
86
+ Status ["text" ] = "Finished!!"
87
+ Status ["fg" ] = "green"
67
88
68
89
90
+ #start download
91
+ def start_downloading ():
92
+ bar ["value" ]= 0 ;
93
+
94
+ def monitor ( download_thread ):
95
+ """ Monitor the download thread """
96
+ if download_thread .is_alive ():
97
+
98
+ try :
99
+ bar ["value" ]= queue .get (0 )
100
+ ld_window .after (10 , lambda : monitor (download_thread ))
101
+ except Empty :
102
+ pass
103
+
69
104
# GUI
70
105
71
106
ld_window = tk .Tk ()
72
107
ld_window .title ("Linkedin Video Downloader" )
73
- ld_window .geometry ("400x400 " )
108
+ ld_window .geometry ("400x300 " )
74
109
75
110
# Label for URL Input
76
111
input_label = tk .Label (ld_window ,text = "Enter Linkedin Video URL:" )
77
112
input_label .pack ()
78
113
114
+ queue = queue .Queue ()
115
+
79
116
# Input of URL
80
117
Url_Val = tk .StringVar ()
81
118
Url_Input = tk .Entry (ld_window , textvariable = Url_Val , font = ("Calibri" , 9 ))
@@ -85,11 +122,10 @@ def Download_vid():
85
122
Download_button = tk .Button (ld_window , text = "Download" , font = ("Calibri" , 9 ), command = Download_vid )
86
123
Download_button .place (x = 100 , y = 100 , width = 200 )
87
124
88
- # Download Window
125
+ # Progress Bar
126
+ bar = Progressbar (ld_window , length = 350 , style = 'grey.Horizontal.TProgressbar' ,mode = 'determinate' )
127
+ bar .place (y = 200 ,width = 350 ,x = 25 )
89
128
90
- Download_Window = tk .Text (ld_window , font = ("Calibri" , 9 ), bg = "black" , fg = "white" , bd = 1 , relief = tk .SUNKEN , wrap = tk .WORD )
91
- Download_Window .insert (tk .END , "Welcome to Linkedin Video Downloader, Provide a Linkedin post link in the above box and click download to start the process. :D" )
92
- Download_Window .place (x = 25 , y = 200 , width = 350 , height = 250 )
93
129
94
130
# Text for Status of Downloading
95
131
Status = tk .Label (ld_window , text = "Hello!! :D" , fg = "blue" , font = ("Calibri" , 9 ), bd = 1 , relief = tk .SUNKEN , anchor = tk .W , padx = 3 )
0 commit comments