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