Passed
Push — master ( 4c061f...55b85e )
by Deepak
58s
created

Main.FacebookDownload()   B

Complexity

Conditions 8

Size

Total Lines 31
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 31
nop 0
dl 0
loc 31
rs 7.2693
c 0
b 0
f 0
1
# ------------------Import Files-----------------------------------------
2
import pytube
3
from pytube import YouTube
4
from pytube import Playlist
5
import sys
6
import re
7
import requests as r
8
import wget
9
from tkinter import PhotoImage,Label,StringVar,Entry,Button,IntVar,\
10
Radiobutton,messagebox,filedialog
11
import tkinter as tk
12
from tkinter import ttk
13
import clipboard
14
# -------------------------------------------------------------------------#
15
# Toplevel is used for Supporting the Background Image
16
top = tk.Tk()
17
tabControl = ttk.Notebook(top)
18
DisFont = font = ('Comic Sans MS', 16)
19
SubFont = font = ('Comic Sans MS', 12)
20
21
# ------------TAB PROCESS----------------------------------
22
tab1 = ttk.Frame(tabControl)
23
24
tabControl.add(tab1, text='Youtube')
25
26
tab2 = ttk.Frame(tabControl)
27
28
tabControl.add(tab2, text='Facebook',)
29
30
tabControl.pack(expand=1, fill="both")
31
32
top.iconbitmap('Assets/YoutubeDownloader.ico')     # Icon
33
34
top.title("FYI Download Manager :")             # Title
35
36
top.geometry("800x500")                         # Resolution
37
38
photo = PhotoImage(file="Assets/youtube_bg.png")
39
40
w = Label(tab1, image=photo)
41
w.pack()
42
43
photo2 = PhotoImage(file="Assets/facebook_bg.png")
44
45
w2 = Label(tab2, image=photo2)
46
w2.pack()
47
48
top.resizable(0, 0)
49
50
# -----------Close Function--------------------------------------------------
51
def close_btn():
52
    if messagebox.askyesno("FYI...", "Are you Sure you want to exit") is True:
53
        top.withdraw()
54
        top.destroy()
55
        top.exit()
56
57
# ---------------------------------------------------------------------------
58
var1 = StringVar()
59
large_font = ('Verdana', 15)
60
url = Entry(top, textvariable=var1,
61
                    font=large_font)
62
url.place(x=20, y=100, width=500, height=30)
63
64
cancelbtn = Button(top, text="Quit", font=DisFont, cursor='hand2',
65
            command=close_btn, activebackground="lightgreen",
66
            activeforeground="blue")
67
cancelbtn.place(x=60, y=430, width=200, height=30)
68
#---Auto Paste Without Using the Function----#
69
temp = clipboard.paste()
70
url.insert('end', temp)
71
#fburl.insert('end',temp)
72
var1.set(temp)
73
#-- Auto paste Function-----#
74
def paste():
75
    variable = clipboard.paste()
76
    url.insert('end', variable)
77
    #fburl.insert('end',variable)
78
    var1.set(variable)                         # Disable the Maximize
79
#---Clear Function-----#
80
def e1_delete():
81
    url.delete(0,'end')
82
#--------------Button for Paste and Clear----#
83
def toggle(tog=[0]):
84
    tog[0] = not tog[0]
85
    if tog[0]:
86
        t_btn.config(text='paste')
87
        e1_delete()
88
    else:
89
        t_btn.config(text='clear')
90
        paste()
91
t_btn = tk.Button(top,text="Clear", width=8, command=toggle)
92
t_btn.place(x=530,y=100)
93
# --------------Fetching Part from Youtube----------------------------------
94
def get_fetch():
95
    resolution = Rvideo.get()
96
    Select = A_Video.get()
97
    Selection = A_Audio.get()
98
    # pb.start()
99
    try:
100
        if var1 is None:
101
            print("error")
102
        dirname = filedialog.askdirectory(parent=tab1, initialdir="/",
103
                    title='Please select a directory')
104
        if dirname:
105
            try:
106
                # Video with Resolution
107
                if resolution <= 3:
108
                    yt = pytube.YouTube(var1.get())
109
                    if resolution == 1:
110
                        progress_bar = ttk.Progressbar(tab1, orient = 'horizontal', length = 500, mode = 'determinate')
111
                        progress_bar.place(x=60,y=140)
112
                        progress_bar.start()
113
                        messagebox.showinfo("Download",
114
                            "Downloading.. Please Wait for a Minute.")
115
                        video = yt.streams.get_by_itag(22)
116
                        video.download(dirname)
117
                    elif resolution == 2:
118
                        progress_bar = ttk.Progressbar(tab1, orient = 'horizontal', length = 500, mode = 'determinate')
119
                        progress_bar.place(x=60,y=140)
120
                        progress_bar.start()
121
                        messagebox.showinfo("Download","Downloading...")
122
                        video = yt.streams.first()
123
                        video.download(dirname)
124
                    elif resolution == 3:
125
                        progress_bar = ttk.Progressbar(tab1, orient = 'horizontal', length = 500, mode = 'determinate')
126
                        progress_bar.place(x=60,y=140)
127
                        progress_bar.start()
128
                        messagebox.showinfo("Download","Downloading...")
129
                        video = yt.streams.get_by_itag(36)
130
                        video.download(dirname)
131
                # Download Playlist
132
                if Select == 1:
133
                    playlist = pytube.Playlist(var1.get())
134
                    progress_bar = ttk.Progressbar(tab1, orient = 'horizontal', length = 500, mode = 'determinate')
135
                    progress_bar.place(x=60,y=140)
136
                    progress_bar.start()
137
                    playlist.populate_video_urls()          # To load bulk list
138
                    messagebox.showinfo("Download", "Downloading...")
139
                    playlist.download_all(dirname)
140
141
                # Audio files
142
                if Selection <= 2:
143
                    link = YouTube(var1.get())
144
                    format_a = link.streams.filter(only_audio=True).all()
145
                    if Selection == 1:
146
                        # mp4
147
                        progress_bar = ttk.Progressbar(tab1, orient = 'horizontal', length = 500, mode = 'determinate')
148
                        progress_bar.place(x=60,y=140)
149
                        progress_bar.start()
150
                        messagebox.showinfo("Download", "Downloading...")
151
                        format_a[0].download(dirname)
152
                    elif Selection == 2:                # webm
153
                        progress_bar = ttk.Progressbar(tab1, orient = 'horizontal', length = 500, mode = 'determinate')
154
                        progress_bar.place(x=60,y=140)
155
                        progress_bar.start()
156
                        messagebox.showinfo("Download", "Downloading...")
157
                        format_a[1].download(dirname)
158
159
                messagebox.showinfo("Downloading.. ", "Thank You.")
160
            except Exception as a:
161
                messagebox.showwarning(" FYI.. ", "Failed")
162
        else:
163
            messagebox.showwarning(" FYI. ", "Cancelled")
164
    except Exception as a:
165
166
        messagebox.showwarning(" FYI. ", "Cancelled")
167
        #print(a)
168
        sys.exit()
169
170
downbtn = Button(tab1, text="Download", font=DisFont,
171
        command=get_fetch, cursor='hand2', activebackground="lightgreen",
172
        activeforeground="blue")
173
downbtn.place(x=500, y=405, width=200, height=30)
174
name = Label(top, text="Enter the Link to Download",
175
        font=('Comic Sans MS', 16), bg="#520B00", fg="White")
176
name.place(x=60, y=60)
177
178
# -----------------Radio button for video resolution-------------------------
179
Rvideo = IntVar()
180
resolution_label = Label(tab1, text="Video Resolution:",
181
            font=DisFont, bg="#520B00", fg="white")
182
resolution_label.place(x=75, y=160)
183
R1 = Radiobutton(tab1, text="Mp4 720", cursor='hand2', font=SubFont,
184
            variable=Rvideo, value=1, command=get_fetch, bg="#520B00",
185
            fg="white")
186
R1.place(x=80, y=200)
187
R2 = Radiobutton(tab1, text="Mp4 360px", cursor='hand2', font=SubFont,
188
            variable=Rvideo, value=2, command=get_fetch, bg="#520B00",
189
            fg="white")
190
R2.place(x=80, y=240)
191
R3 = Radiobutton(tab1, text="3gp 144px", cursor='hand2', font=SubFont,
192
            variable=Rvideo, value=3, command=get_fetch, bg="#520B00",
193
            fg="white")
194
R3.place(x=80, y=280)
195
196
# -----------------Radio button for video to Audio----------------------------
197
A_Audio = IntVar()
198
format_label = Label(tab1, text="Only Audio:",
199
                font=DisFont, bg="#520B00", fg="white")
200
format_label.place(x=260, y=160)
201
R4 = Radiobutton(tab1, text="Mp4 Audio", cursor='hand2',
202
                font=SubFont, variable=A_Audio, value=1, command=get_fetch,
203
                bg="#520B00", fg="white")
204
R4.place(x=265, y=200)
205
R5 = Radiobutton(tab1, text="webm", cursor='hand2', font=SubFont,
206
                variable=A_Audio, value=2, command=get_fetch,
207
                bg="#520B00", fg="white")
208
R5.place(x=265, y=240)
209
210
# -----------------Radio button for Playlist video --------------------------
211
A_Video = IntVar()
212
list_label = Label(tab1, text="Download Playlist:", font=DisFont,
213
                bg="#520B00", fg="white")
214
list_label.place(x=415, y=160)
215
R7 = Radiobutton(tab1, text="High (Default)", cursor='hand2', font=SubFont,
216
                variable=A_Video, value=1, command=get_fetch, bg="#520B00",
217
                fg="white")
218
R7.place(x=420, y=200)
219
220
# ======================= Facebook Control ==================================
221
222
#burl = Entry(tab2, textvariable=Var1,
223
#                    font=large_font)
224
#fburl.place(x=60, y=100, width=500, height=30)
225
226
227
228
def FacebookDownload():
229
    try:
230
        html = r.get(var1.get())
231
        dirname = filedialog.askdirectory(parent=tab2, initialdir="/",
232
                    title='Please select a directory')
233
        if dirname:
234
            hdvideo_url = re.search('hd_src:"(.+?)"', html.text)[1]
235
        try:
236
            hd_url = hdvideo_url.replace('hd_src:"', '')
0 ignored issues
show
introduced by
The variable hdvideo_url does not seem to be defined in case dirname on line 233 is False. Are you sure this can never be the case?
Loading history...
237
            messagebox.showinfo("FYIT","[+] Video Started Downloading")
238
            progress_bar = ttk.Progressbar(tab2, orient = 'horizontal', length = 500, mode = 'determinate')
239
            progress_bar.place(x=60,y=140)
240
            progress_bar.start()
241
            wget.download(hd_url, dirname)
242
            ERASE_LINE = '\x1b[2K'
243
            sys.stdout.write(ERASE_LINE)
244
            messagebox.showinfo("FYIT","Video downloaded")
245
            progress_bar.stop()
246
        except r.ConnectionError as e:
247
            messagebox.showerror("FYIT","ConnectionError")
248
        except r.Timeout as e:
249
            messagebox.showinfo("FYIT","TIMEOUT")
250
        except r.RequestException as e:
251
            messagebox.showerror("FYIT","GENRAL ERROR OR INVALID URL")
252
        except(KeyboardInterrupt,SystemExit):
253
            messagebox.showinfo("FYIT","SystemExit")
254
            sys.exit()
255
        except TypeError:
256
            messagebox.showerror("FYIT","Video May Private or InvalidURL")
257
    except Exception as e:
258
        messagebox.showwarning("FYIT","CANCELED") 
259
downbtnfb = Button(tab2, text="Download", font=DisFont,
260
                 command=FacebookDownload, cursor='hand2', activebackground="lightgreen",
261
                 activeforeground="blue")
262
downbtnfb.place(x=500, y=405, width=200, height=30)
263
top.mainloop()
264
# -----------------------------Close Function--------------------------------
265
def on_close():
266
    top.destroy()
267
    sys.exit()
268
top.protocol("WM_DELETE_WINDOW", on_close)
269
# ---------------------------------------------------------------------------
270