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