Passed
Pull Request — master (#39)
by Deepak
54s
created

Main.py (1 issue)

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