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