Passed
Pull Request — master (#33)
by Deepak
58s
created

Main.py (5 issues)

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