Passed
Pull Request — master (#30)
by Deepak
48s
created

Main.py (1 issue)

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, Frame, X, Y, LEFT, RIGHT, \
11
         N, FLAT, CENTER, Canvas, Scrollbar, VERTICAL, BOTH, SUNKEN, SOLID
12
import tkinter as tk
13
from tkinter import ttk
14
import clipboard
15
import webbrowser
16
import requests
17
# ============================ Window Design ================================ 
18
#top = tk.Tk()
19
top = ThemedTk(theme="breeze")
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable ThemedTk does not seem to be defined.
Loading history...
20
# Styles and Designs Functions for Screens
21
# Color set for youtube for selectcolor, bg & activebackground #520B00
22
yt_col = "#520B00"  # Youtube bg Color
23
# Color set for facebook for selectcolor, bg & activebackground #075C90
24
fb_col = '#075C90' # Facebook bg Color
25
col_show = 'white'  # pure white fg
26
col_select = 'gray'  # gray for activeforeground
27
COLOR_1 = "#90B3DD" # TabControl Config Color
28
29
# Font Style and Size
30
large_font = font = ('Cascadia Mono', 16)  # style='my.head'
31
DisFont = font = ('', 14)  # style='my.default'
32
SubFont = font = ('Consolas', 12)  # style='my.sub'
33
tab_font = font = ('Consolas', 12)  # Tab font style
34
35
# ------------------------- Tab Control Style Config ----------------------
36
37
tab_show = "#90B3DD"
38
tab_select = "#C8D9EE"
39
style = ttk.Style()
40
style.theme_create("pop",
41
                   parent="alt",
42
                   settings={
43
                       "TNotebook": {
44
                           "configure": {
45
                               "tabmargins": [5, 5, 2, 0]
46
                           }
47
                       },
48
                       "TNotebook.Tab": {
49
                           "configure": {
50
                               "padding": [100, 5],
51
                               "font": tab_font,
52
                               "background": tab_show
53
                           },
54
                           "map": {
55
                               "background": [("selected", tab_select)],
56
                               "expand": [("selected", [1, 2, 2, 0])]
57
                           }
58
                       }
59
                   })
60
61
style.theme_use("pop")
62
63
# Style for Buttons
64
s_btn = ttk.Style() # Toggle Button
65
s_btn.configure('my.TButton', 
66
                anchor='center', 
67
                font=('Cascadia Mono', 8))
68
69
m_btn = ttk.Style() # Download and Cancel Button for both Tab1 & 2
70
m_btn.configure('WD.TButton', anchor='center',
71
                activebackground="lightgreen",                  
72
                activeforeground="blue",
73
                font=('Cascadia Mono', 16))
74
75
# Notebook Style Config 
76
noteStyler = ttk.Style()
77
noteStyler.configure("TNotebook", 
78
                    background=COLOR_1, 
79
                    anchor=CENTER, 
80
                    borderwidth=0)
81
82
# ------------------------ Screen and Tab -----------------------------------
83
84
tabControl = ttk.Notebook(top)
85
86
tab1 = ttk.Frame(tabControl)        # Tab1 - Youtube
87
tabControl.add(tab1, text='Youtube')
88
89
tab2 = ttk.Frame(tabControl)        # Tab2 - Facebook
90
tabControl.add(tab2, text='Facebook')
91
92
tab3 = ttk.Frame(tabControl)        # Tab3 - About
93
tabControl.add(tab3, text='About')
94
tabControl.pack(expand=1, fill="both")
95
96
top.iconbitmap('Assets/YoutubeDownloader.ico')  # Window Title Icon 
97
top.title("FYIT Download Manager :")  # Title Label
98
top.geometry("800x500+300+100")  # Screen Size
99
100
photo = PhotoImage(file="Assets/youtube_bg.png")  # Tab1 Background
101
w = Label(tab1, image=photo)
102
w.pack()
103
104
photo2 = PhotoImage(file="Assets/facebook_bg.png")  # Tab2 Background
105
w2 = Label(tab2, image=photo2)
106
w2.pack()
107
108
top.resizable(0, 0)     # Disable the Maximize & Minimize option
109
110
# -----------Close Function--------------------------------------------------
111
112
def close_btn():
113
    if messagebox.askyesno("FYIT..", "Are you Sure you want to exit") is True:
114
        top.withdraw()
115
        top.destroy()
116
        top.exit()
117
118
# ---------------------------------------------------------------------------
119
120
var1 = StringVar()
121
# Entry Widget for Youtube Downloader Tab
122
url = Entry(tab1, textvariable=var1, font=large_font, fg='darkblue')
123
url.place(x=70, y=117, width=500, height=30)
124
125
# Entry Widget for Facebook Downloader Tab
126
url_fb = Entry(tab2, textvariable=var1, font=large_font, fg='darkblue')
127
url_fb.place(x=70, y=117, width=500, height=30)
128
 
129
# Quit_button placed in tab1
130
quit_button = Button(tab1,
131
                         text="Quit",
132
                         relief=SOLID,
133
                         font=SubFont,
134
                         cursor='hand2',
135
                         command=close_btn)
136
quit_button.place(x=60, y=400, width=200, height=30) 
137
138
# ----------------------- Auto URL Paste Functions --------------------------
139
temp = clipboard.paste()
140
url.insert('end', temp)
141
url_fb.insert('end', temp)
142
var1.set(temp)
143
144
def paste():  # Paste text from Clipboard - Function
145
    variable = clipboard.paste()
146
    url.insert('end', variable)
147
    url_fb.insert('end', variable)
148
    var1.set(variable)  
149
150
def e1_delete():  # Clear text from Entry - Function
151
    url.delete(0, 'end')        
152
    url_fb.delete(0, 'end')     
153
154
def toggle(tog=[0]):  # Toggle Button for clear and paste
155
    tog[0] = not tog[0]
156
    if tog[0]:
157
        t_btn.config(text='Paste')
158
        t_btn1.config(text='Paste')
159
        e1_delete()
160
    else:
161
        t_btn.config(text='Clear')
162
        t_btn1.config(text='Clear')
163
        paste()
164
165
# tab1 clear & paste
166
t_btn1 = ttk.Button(tab1, text="Clear", cursor='hand2', style='my.TButton', command=toggle)
167
t_btn1.place(x=571, y=118, width=45, height=29)
168
169
# tab2 clear & paste
170
t_btn = ttk.Button(tab2, text="Clear", cursor='hand2', style='my.TButton', command=toggle)
171
t_btn.place(x=571, y=118, width=45, height=29)
172
173
# ------------------ Fetching Part from Youtube ----------------------------
174
175
new = 1
176
url1 = "https://bit.ly/site-fyit" 
177
res_url = "https://github.com/DeepakChakravarthy/YoutubeDownloader-FYI/releases/tag/V3.0"
178
179
def openweb():      # Software Update response
180
	webbrowser.open(url1, new=new)
181
182
def update_check():     # AutoCheck Software Update
183
    response = requests.get(res_url)
184
    response.raise_for_status()
185
    if messagebox.askyesno("Software Update", 
186
            "New Update is Availabe, Click yes to install.") is True:
187
        openweb()
188
189
def get_fetch():
190
    resolution = Rvideo.get()       
191
    Select = A_Video.get()
192
    Selection = A_Audio.get()
193
    
194
    try:
195
        update_check()	    
196
    except requests.exceptions.HTTPError as err:
197
	    messagebox.showinfo("FYIT", "Select Location to Save the File.")
198
    except requests.ConnectionError as e:
199
        messagebox.showinfo("Connection Error", "Check the Internet Connection")        
200
201
    try:
202
        if var1 is None:
203
            print("error")
204
        dirname = filedialog.askdirectory(parent=tab1,
205
                                          initialdir="/",
206
                                          title='Please select a directory:')
207
        if dirname:
208
            try:
209
                # Youtube Video with Resolution
210
                if resolution <= 3:
211
                    yt = pytube.YouTube(var1.get())
212
                    if resolution == 1:
213
                        progress_bar = ttk.Progressbar(tab1,
214
                                                       orient='horizontal',
215
                                                       length=500,
216
                                                       mode='determinate')
217
                        progress_bar.place(x=70, y=160)
218
                        progress_bar.start()
219
                        messagebox.showinfo(
220
                            "Download",
221
                            "Downloading.. Please Wait for a Minute.")
222
                        video = yt.streams.get_by_itag(22)
223
                        video.download(dirname)
224
                        messagebox.showinfo("Downloaded. ", "Thank You..")
225
                    elif resolution == 2:
226
                        progress_bar = ttk.Progressbar(tab1,
227
                                                       orient='horizontal',
228
                                                       length=500,
229
                                                       mode='determinate')
230
                        progress_bar.place(x=70, y=160)
231
                        progress_bar.start()
232
                        messagebox.showinfo("Download", "Downloading...")
233
                        video = yt.streams.first()
234
                        video.download(dirname)
235
                        messagebox.showinfo("Downloaded. ", "Thank You..")
236
                    elif resolution == 3:
237
                        progress_bar = ttk.Progressbar(tab1,
238
                                                       orient='horizontal',
239
                                                       length=500,
240
                                                       mode='determinate')
241
                        progress_bar.place(x=70, y=160)
242
                        progress_bar.start()
243
                        messagebox.showinfo("Download", "Downloading...")
244
                        video = yt.streams.get_by_itag(36)
245
                        video.download(dirname)
246
                        messagebox.showinfo("Downloaded. ", "Thank You..")
247
248
                # Download Playlist
249
                if Select == 1:
250
                    playlist = pytube.Playlist(var1.get())
251
                    progress_bar = ttk.Progressbar(tab1,
252
                                                   orient='horizontal',
253
                                                   length=500,
254
                                                   mode='determinate')
255
                    progress_bar.place(x=70, y=160)
256
                    progress_bar.start()
257
                    playlist.populate_video_urls()  # To load bulk list
258
                    messagebox.showinfo("Download", "Downloading...")
259
                    playlist.download_all(dirname)
260
                    messagebox.showinfo("Downloaded. ", "Thank You..")
261
262
                # Audio files
263
                if Selection <= 2:
264
                    link = YouTube(var1.get())
265
                    format_a = link.streams.filter(only_audio=True).all()
266
                    if Selection == 1:  # mp4
267
                        progress_bar = ttk.Progressbar(tab1,
268
                                                       orient='horizontal',
269
                                                       length=500,
270
                                                       mode='determinate')
271
                        progress_bar.place(x=70, y=160)
272
                        progress_bar.start()
273
                        messagebox.showinfo("Download", "Downloading...")
274
                        format_a[0].download(dirname)
275
                        messagebox.showinfo("Downloaded. ", "Thank You..")
276
                    elif Selection == 2:  # webm
277
                        progress_bar = ttk.Progressbar(tab1,
278
                                                       orient='horizontal',
279
                                                       length=500,
280
                                                       mode='determinate')
281
                        progress_bar.place(x=70, y=160)
282
                        progress_bar.start()
283
                        messagebox.showinfo("Download", "Downloading...")
284
                        format_a[1].download(dirname)
285
                        messagebox.showinfo("Downloaded. ", "Thank You..")
286
                messagebox.showinfo("FYIT", "Please Select Valid option")
287
            except Exception as a:
288
                messagebox.showwarning(" FYIT.. ", "Failed")
289
        else:
290
            messagebox.showwarning(" FYIT. ", "Cancelled")
291
    except Exception as a:
292
        messagebox.showwarning(" FYIT. ", "Cancelled")
293
        sys.exit()
294
295
downbtn = Button(tab1,
296
                 text="Download",
297
                 relief=SOLID,
298
                 font=SubFont,
299
                 command=get_fetch,
300
                 cursor='hand2' )
301
downbtn.place(x=500, y=400, width=200, height=30)   # Download button Tab1
302
yt_label = Label(tab1,
303
                 text="Enter the Link to Download",
304
                 font=large_font,
305
                 bg="#520B00",
306
                 fg="White")
307
yt_label.place(x=65, y=65)                          # Label placed in Tab1
308
309
# -----------------Radio button for video resolution-------------------------
310
311
Rvideo = IntVar()       # Variable Daclaraton for Youtube Video (options)
312
resolution_label = Label(tab1,
313
                         text="Video Resolution:",
314
                         font=DisFont,
315
                         bg="#520B00",
316
                         fg="white")
317
resolution_label.place(x=75, y=200)
318
R1 = Radiobutton(tab1,
319
                 text="Mp4 720",
320
                 cursor='hand2',
321
                 font=SubFont,
322
                 variable=Rvideo,
323
                 value=1,
324
                 fg=col_show,
325
                 bg=yt_col,
326
                 activebackground=yt_col,
327
                 activeforeground=col_select,
328
                 selectcolor=yt_col)
329
R1.place(x=80, y=240)
330
R2 = Radiobutton(tab1,
331
                 text="Mp4 360px",
332
                 cursor='hand2',
333
                 font=SubFont,
334
                 variable=Rvideo,
335
                 value=2,
336
                 fg=col_show,
337
                 bg=yt_col,
338
                 activebackground=yt_col,
339
                 activeforeground=col_select,
340
                 selectcolor=yt_col)
341
R2.place(x=80, y=280)
342
R3 = Radiobutton(tab1,
343
                 text="3gp 144px",
344
                 cursor='hand2',
345
                 font=SubFont,
346
                 variable=Rvideo,
347
                 value=3,
348
                 fg=col_show,
349
                 bg=yt_col,
350
                 activebackground=yt_col,
351
                 activeforeground=col_select,
352
                 selectcolor=yt_col)
353
R3.place(x=80, y=320)
354
355
# -----------------Radio button for video to Audio----------------------------
356
357
A_Audio = IntVar()      # Variable Daclaraton for Youtube Audio (options)
358
format_label = Label(tab1,
359
                     text="Only Audio:",
360
                     font=DisFont,
361
                     bg="#520B00",
362
                     fg="white")
363
format_label.place(x=260, y=200)
364
R4 = Radiobutton(tab1,
365
                 text="Mp4 Audio",
366
                 cursor='hand2',
367
                 font=SubFont,
368
                 variable=A_Audio,
369
                 value=1,
370
                 fg=col_show,
371
                 bg=yt_col,
372
                 activebackground=yt_col,
373
                 activeforeground=col_select,
374
                 selectcolor=yt_col)
375
R4.place(x=265, y=240)
376
R5 = Radiobutton(tab1,
377
                 text="webm",
378
                 cursor='hand2',
379
                 font=SubFont,
380
                 variable=A_Audio,
381
                 value=2,
382
                 fg=col_show,
383
                 bg=yt_col,
384
                 activebackground=yt_col,
385
                 activeforeground=col_select,
386
                 selectcolor=yt_col)
387
R5.place(x=265, y=280)
388
389
# -----------------Radio button for Playlist video --------------------------
390
391
A_Video = IntVar()      # Variable Daclaraton for Youtube Playlist (options)
392
list_label = Label(tab1,
393
                   text="Download Playlist:",
394
                   font=DisFont,
395
                   bg="#520B00",
396
                   fg="white")
397
list_label.place(x=415, y=200)
398
R7 = Radiobutton(tab1,
399
                 text="High (Default)",
400
                 cursor='hand2',
401
                 font=SubFont,
402
                 variable=A_Video,
403
                 value=1,
404
                 fg=col_show,
405
                 bg=yt_col,
406
                 activebackground=yt_col,
407
                 activeforeground=col_select,
408
                 selectcolor=yt_col)
409
R7.place(x=420, y=240)
410
411
# ======================= Facebook Control ==================================
412
413
quit_button = Button(tab2,
414
                         text="Quit",
415
                         relief=SOLID,
416
                         font=SubFont,
417
                         cursor='hand2',
418
                         command=close_btn)
419
quit_button.place(x=60, y=400, width=200, height=30)    # Quit_button placed in tab2
420
421
fb_label = Label(tab2,
422
                 text="Enter the Link to Download",
423
                 font=large_font,
424
                 bg="#075C90",
425
                 fg="White")
426
fb_label.place(x=65, y=65)      # Label placed in Tab2
427
428
def FacebookDownload():         # FacebookDownloader Main Fuction 
429
    try:
430
        update_check()
431
    except requests.exceptions.HTTPError as err:
432
	    messagebox.showinfo("FYIT", "Select Location to Download.")
433
    except requests.ConnectionError as e:
434
        messagebox.showinfo("Connection Error",
435
                            "Check the Internet Connection")
436
437
    try:
438
        html = r.get(var1.get())
439
        dirname = filedialog.askdirectory(parent=tab2,
440
                                          initialdir="/",
441
                                          title='Please select a directory:')
442
        if dirname:
443
            hdvideo_url = re.search('hd_src:"(.+?)"', html.text)[1]
444
        try:
445
            hd_url = hdvideo_url.replace('hd_src:"', '')
446
            messagebox.showinfo("FYIT", "[+] Video Started Downloading")
447
            progress_bar = ttk.Progressbar(tab2,
448
                                           orient='horizontal',
449
                                           length=500,
450
                                           mode='determinate')
451
            progress_bar.place(x=60, y=180)
452
            progress_bar.start()
453
            wget.download(hd_url, dirname)
454
            ERASE_LINE = '\x1b[2K'
455
            sys.stdout.write(ERASE_LINE)
456
            messagebox.showinfo("FYIT", "Video downloaded")
457
            progress_bar.stop()
458
        except r.ConnectionError as e:
459
            messagebox.showerror("FYIT", "ConnectionError")
460
        except r.Timeout as e:
461
            messagebox.showinfo("FYIT", "Tomeout")
462
        except r.RequestException as e:
463
            messagebox.showerror("FYIT", "General Error or Invalid URL")
464
        except (KeyboardInterrupt, SystemExit):
465
            messagebox.showinfo("FYIT", "SystemExit")
466
            sys.exit()
467
        except TypeError:
468
            messagebox.showerror("FYIT", "Video May Private or InvalidURL")
469
    except Exception as e:
470
        messagebox.showwarning("FYIT", "Cancelled")
471
472
downbtnfb = Button(tab2,
473
                   text="Download",
474
                   relief=SOLID,
475
                   font=SubFont,
476
                   command=FacebookDownload,
477
                   cursor='hand2')
478
downbtnfb.place(x=500, y=400, width=200, height=30)    # Download placed in Tab2
479
480
# ======================= About Tab Control ===================================
481
482
def check_it(hyper_link):       # About tab Redirect Link (Frame1)
483
    webbrowser.open_new(hyper_link)
484
485
486
def find_update():              # Check Software Update - 2
487
    try:
488
        update_check()
489
        messagebox.showinfo("FYIT", "Thank You.")
490
    except requests.ConnectionError as e:
491
        messagebox.showinfo("Connection Error",
492
                            "Check the Internet Connection")
493
    except requests.exceptions.HTTPError as err: 
494
        messagebox.showinfo("FYIT","No Update yet..")
495
496
tab3_style = ttk.Style()
497
498
frame1 = tk.Frame(master=tab3, width=260)           # Tab3_Frame1 
499
frame1.pack(fill=tk.BOTH, side=tk.LEFT, expand=True)
500
501
imge = PhotoImage(file='Assets/side_bar.png')
502
pht = Label(frame1, image=imge)
503
pht.pack()
504
505
link_1 = Label(frame1, 
506
                text="How to use..?", 
507
                font=tab_font,
508
                fg="blue", 
509
                bg="#90B3DD",
510
                activeforeground="red",
511
                activebackground="green",
512
                cursor="hand2",
513
                underline=0)
514
link_1.place(x=20, y=260)
515
link_1.bind("<Button-1>", 
516
            lambda e: check_it())
517
518
link_2 = Label(frame1, 
519
                text="Help..!", 
520
                font=tab_font,
521
                fg="blue", 
522
                bg="#90B3DD",
523
                activeforeground="red",
524
                activebackground="green",
525
                cursor="hand2",
526
                underline=0)
527
link_2.place(x=20, y=300)
528
link_2.bind("<Button-1>", 
529
            lambda e: check_it("http://bit.ly/site-fyit"))
530
531
link_3 = Label(frame1,
532
               text="Contact us..",
533
               font=tab_font,
534
               fg="blue",
535
               bg="#90B3DD",
536
               activeforeground="red",
537
               activebackground="green",
538
               cursor="hand2",
539
               underline=0)
540
link_3.place(x=20, y=340)
541
link_3.bind("<Button-1>",
542
            lambda e: check_it("https://gitter.im/FYIT-DOWNLOADER/DEV-FYI?utm_source=share-link&utm_medium=link&utm_campaign=share-link"))
543
544
link_4 = Label(frame1, 
545
                text="Visit us..", 
546
                font=tab_font,
547
                fg="blue", 
548
                bg="#90B3DD",
549
                activeforeground="red",
550
                activebackground="green",
551
                cursor="hand2",
552
                underline=0)
553
link_4.place(x=20, y=380)
554
link_4.bind("<Button-1>", 
555
            lambda e: check_it("http://bit.ly/site-fyit"))
556
557
link_5 = Button(frame1, 
558
                text="Check Update..", 
559
                font=tab_font,
560
                command=find_update,
561
                fg="blue", 
562
                bg="#90B3DD",
563
                activeforeground="red",
564
                activebackground="#90B3DD",
565
                cursor="hand2",
566
                relief=FLAT,
567
                underline=0)
568
link_5.place(x=20, y=420)
569
link_5.config(highlightthickness=0)
570
571
frame2 = tk.Frame(master=tab3, width=640, bg="#C8D9EE")     # Tab3_Frame2
572
frame2.pack(fill=tk.BOTH, side=tk.LEFT, expand=True)
573
574
canvas=Canvas(frame2,
575
                bg='#FFFFFF', 
576
                width=640, 
577
                height=500, 
578
                scrollregion=(0,0,640,500))
579
                
580
vbar=Scrollbar(frame2, orient=VERTICAL)
581
vbar.pack(side=RIGHT,fill=Y)
582
vbar.config(command=canvas.yview)
583
584
canvas.config(width=600, height=500)
585
canvas.config(yscrollcommand=vbar.set)
586
canvas.pack(side=RIGHT, expand=True, fill=BOTH)
587
588
top.mainloop()
589
590
# ----------------------------- Close Function -------------------------------
591
592
def on_close():
593
    top.destroy()
594
    sys.exit()
595
596
top.protocol("WM_DELETE_WINDOW", on_close)
597
598
# ----------------------------------------------------------------------------
599