# IMPORT

try:
    import sys
    from tkinter import Tk, Label, messagebox, Entry, Button, filedialog
    from tkinter import *
    from pytube import YouTube
except ModuleNotFoundError or ImportError:
    messagebox.showerror('Error!', 'Error importing modules!')
    sys.exit()

# DEFS

def about():

    #FUNC

    import sys

    if sys.platform == 'win32':
        a = 'for Windows.'
    if sys.platform == 'darwin':
        a = 'for MAC OS.'
    if sys.platform == 'linux' or sys.platform == 'linux2':
        a = 'for Linux.'

    #WINABOUT

    winabout = Tk()
    winabout.geometry('320x240')
    winabout.resizable(False, False)
    winabout.title('About the program')

    #WINABOUTLABELS

    namelabel = Label(winabout, text='YoutubeDownloader', font=('Arial', 15, 'bold'))
    namelabel.place(x = 60, y = 0)

    verlabel = Label(winabout, text= f' Version: 1.1 {a}', font=('Arial', 15, 'bold'))
    verlabel.place(x = 0, y = 40)

    copyrightlabel = Label(winabout, text= 'Advokat007. All Rights Reserved.')
    copyrightlabel.place(x = 145, y = 220)

    #WINABOUTMAINLOOP

    winabout.mainloop()

def clickbrowse():
    location_of_download = filedialog.askdirectory()
    path.set(location_of_download)


def seturl():
    url = urlentry.get()

    if url == '':
        messagebox.showerror('Error!', 'Enter the URL of the video!')

    global yt
    yt = YouTube(url)

    global videos
    videos = yt.streams.filter(mime_type="video/mp4").all()

    count = 1
    for v in videos:
        listbox.insert(END,
                       (str(count) + ". " + str(v)[str(v).find("res="):str(v).find("fps=")] + '"\n\n').replace('"', ''))
        count += 1
        if count == 3:
            break


def downloadvideo():
    if urlentry.get() == "":
        messagebox.showerror("Error!", "Enter the link!")
        return
    elif pathentry.get() == "":
        messagebox.showerror("Error!", "Specify the path to the files!")
        return
    try:
        select = listbox.curselection()
        quality = videos[select[0]]
        location = pathentry.get()
        quality.download(location)
        messagebox.showinfo("Success!", 'Download completed successfully!')
    except:
        messagebox.showerror('Error!', 'An error occurred while downloading!')


# MAINWIN

win = Tk()
win.resizable(False, False)
win.geometry('380x250')
win.title('YouTubeDownloader')

# VARIABLES

path = StringVar(win)

# LABELS

welcomelabel = Label(win, text='YoutubeDownloader', font=('Arial', 15, 'bold'))
welcomelabel.place(x=90, y=0)

label1 = Label(win, text='Enter the video URL:')
label1.place(x=0, y=25)

label2 = Label(win, text='Select a folder to save:')
label2.place(x=0, y=70)

label3 = Label(win, text='Choose Quality:')
label3.place(x=0, y=110)

# ENTRIES

urlentry = Entry(win, width=30)
urlentry.place(x=0, y=50)

pathentry = Entry(win, width=30, textvariable=path)
pathentry.place(x=0, y=90)

# BUTTONS

downbtn = Button(win, text='Download!', command=downloadvideo)
downbtn.place(x=0, y=220)

pathbtn = Button(win, text='View', command=clickbrowse)
pathbtn.place(x=190, y=80)

confirmbtn = Button(win, text='Confirm', command=seturl)
confirmbtn.place(x=190, y=40)

aboutbtn = Button(win, text='About Program', command=about)
aboutbtn.place(x=290, y=225)

# LISTBOXES

listbox = Listbox(win, width=30, height=5, bd=3, relief=SOLID, borderwidth=1)
listbox.place(x=0, y=130)

# MAINLOOP

win.mainloop()

# IMPORT

try:
    import sys
    from tkinter import Tk, Label, messagebox, Entry, Button, filedialog
    from tkinter import *
    from pytube import YouTube
except ModuleNotFoundError or ImportError:
    messagebox.showerror('Error!', 'Error importing modules!')
    sys.exit()

# DEFS

def about():

    #FUNC

    import sys

    if sys.platform == 'win32':
        a = 'for Windows.'
    if sys.platform == 'darwin':
        a = 'for MAC OS.'
    if sys.platform == 'linux' or sys.platform == 'linux2':
        a = 'for Linux.'

    #WINABOUT

    winabout = Tk()
    winabout.geometry('320x240')
    winabout.resizable(False, False)
    winabout.title('About the program')

    #WINABOUTLABELS

    namelabel = Label(winabout, text='YoutubeDownloader', font=('Arial', 15, 'bold'))
    namelabel.place(x = 60, y = 0)

    verlabel = Label(winabout, text= f' Version: 1.1 {a}', font=('Arial', 15, 'bold'))
    verlabel.place(x = 0, y = 40)

    copyrightlabel = Label(winabout, text= 'Advokat007. All Rights Reserved.')
    copyrightlabel.place(x = 145, y = 220)

    #WINABOUTMAINLOOP

    winabout.mainloop()

def clickbrowse():
    location_of_download = filedialog.askdirectory()
    path.set(location_of_download)


def seturl():
    url = urlentry.get()

    if url == '':
        messagebox.showerror('Error!', 'Enter the URL of the video!')

    global yt
    yt = YouTube(url)

    global videos
    videos = yt.streams.filter(mime_type="video/mp4").all()

    count = 1
    for v in videos:
        listbox.insert(END,
                       (str(count) + ". " + str(v)[str(v).find("res="):str(v).find("fps=")] + '"\n\n').replace('"', ''))
        count += 1
        if count == 3:
            break


def downloadvideo():
    if urlentry.get() == "":
        messagebox.showerror("Error!", "Enter the link!")
        return
    elif pathentry.get() == "":
        messagebox.showerror("Error!", "Specify the path to the files!")
        return
    try:
        select = listbox.curselection()
        quality = videos[select[0]]
        location = pathentry.get()
        quality.download(location)
        messagebox.showinfo("Success!", 'Download completed successfully!')
    except:
        messagebox.showerror('Error!', 'An error occurred while downloading!')


# MAINWIN

win = Tk()
win.resizable(False, False)
win.geometry('380x250')
win.title('YouTubeDownloader')

# VARIABLES

path = StringVar(win)

# LABELS

welcomelabel = Label(win, text='YoutubeDownloader', font=('Arial', 15, 'bold'))
welcomelabel.place(x=90, y=0)

label1 = Label(win, text='Enter the video URL:')
label1.place(x=0, y=25)

label2 = Label(win, text='Select a folder to save:')
label2.place(x=0, y=70)

label3 = Label(win, text='Choose Quality:')
label3.place(x=0, y=110)

# ENTRIES

urlentry = Entry(win, width=30)
urlentry.place(x=0, y=50)

pathentry = Entry(win, width=30, textvariable=path)
pathentry.place(x=0, y=90)

# BUTTONS

downbtn = Button(win, text='Download!', command=downloadvideo)
downbtn.place(x=0, y=220)

pathbtn = Button(win, text='View', command=clickbrowse)
pathbtn.place(x=190, y=80)

confirmbtn = Button(win, text='Confirm', command=seturl)
confirmbtn.place(x=190, y=40)

aboutbtn = Button(win, text='About Program', command=about)
aboutbtn.place(x=290, y=225)

# LISTBOXES

listbox = Listbox(win, width=30, height=5, bd=3, relief=SOLID, borderwidth=1)
listbox.place(x=0, y=130)

# MAINLOOP

win.mainloop()

# IMPORT # DEFS #FUNC #WINABOUT #WINABOUTLABELS #WINABOUTMAINLOOP # MAINWIN # VARIABLES # LABELS # ENTRIES # BUTTONS # LISTBOXES # MAINLOOP