Passed
Pull Request — master (#30)
by Ram
01:19
created

Main.check_it()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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