Skip to content

Commit

Permalink
added UI + applied __main__ updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Egsago-n committed Jun 24, 2023
1 parent bb1023f commit 23a1d46
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/phub/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,33 @@
@click.option('--quality', '-q', help = 'Video quality (default=best)', default = 'best')
@click.option('--output', '-o', help = 'File output', default = '.')
@click.option('--noconfirm', '-n', help = 'Prevent confirming things.')
@click.option('ui', '-ui', help = 'Start in UI mode.', default = False)

def main(url: str, key: str, quality: str, output: str, noconfirm: str) -> None:
def main(url: str, key: str, quality: str, output: str, noconfirm: str, ui: str) -> None:
'''
Small downloading CLI script for PHUB.
See https://github.com/Egsagon/PHUB.
'''

client = phub.Client()

# Run in UI mode
if ui:
import phub_ui
return phub_ui.main(client)

if not any((url, key)):
return click.secho('Error: Specify either --url or --key', fg = 'red', err = 1)

client = phub.Client()
video = client.get(url, key)

if not bool(noconfirm):
if not click.confirm('Download ' + click.style(video, fg = 'green'), default = 1):
return click.secho('Download aborted', fg = 'red', err = 1)

# Display downloading process.
def log(cur: int, total: int) -> None:
click.echo(f'\rDownloading: {round((cur / total) * 100)}%', nl = 0)

# Download
try:
path = video.download(path = output, quality = phub.Quality(quality),
callback = log, quiet = True)
path = video.download(path = output, quality = phub.Quality(quality))

except Exception as err:
return click.secho(f'Error: {type(err)} {err}', fg = 'red', err = 1)
Expand Down
138 changes: 138 additions & 0 deletions src/phub/phub_ui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
'''
Simple downloading UI script.
'''

try:
import tkinter as tk
from tkinter import ttk
import tkinter.messagebox as tkmb
import tkinter.filedialog as tkfd

except ModuleNotFoundError:
print('Please install tkinter on your system before starting the UI.')
exit()

import phub
from phub.utils import download_presets as dlp

import threading

class App(tk.Tk):

def __init__(self, client: phub.Client) -> None:
'''
Represents an instance of the app.
'''

super().__init__()
self.title('PHUB downloader')
self.geometry('400x200')

self.client = client

# Widgets
tk.Label(self, text = 'Enter video URL').pack()

urlbox = tk.Frame(self)
urlbox.pack(expand = True, fill = 'x', padx = 10)

self.url = ttk.Entry(urlbox)
self.url.bind('<Return>', self.run)
self.url.pack(side = 'left', fill = 'x', expand = True)

ttk.Button(urlbox, text = 'OK', command = self.run).pack(side = 'right')
ttk.Button(urlbox, text = '...', command = self.params).pack(side = 'right')

self.prog = ttk.Progressbar(self)
self.prog.pack(fill = 'x', side = 'bottom', padx = 10, pady = 10)

# Settings
self.path = '.'
self.quality = 'best'

def params(self, *_) -> None:
'''
Open settings window.
'''

def save(*_) -> None:
# Called on save

self.quality = quality.get()
self.path = pathvar.get()
popup.destroy()

print(self.quality, self.path)

def fetch_path(*_) -> None:
# Fetch path

if path := tkfd.askdirectory(title = 'Select download directory'):
pathvar.set(path)

popup = tk.Toplevel(self)
popup.title('Settings')

tk.Label(popup, text = 'Settings').pack()
ttk.Separator(popup).pack()

tk.Label(popup, text = 'Video quality').pack()
quality = tk.StringVar(popup, 'best')
ttk.OptionMenu(popup, quality, 'best', 'best', 'middle', 'worst').pack()
ttk.Separator(popup).pack()

tk.Label(popup, text = 'Download path').pack()
pathvar = tk.StringVar(self, '.')
ttk.Button(popup, text = 'Open', command = fetch_path).pack()
ttk.Separator(popup).pack()

ttk.Button(popup, text = 'Save', command = save).pack()
popup.mainloop()

def updatebar(self, value: int) -> None:
'''
Update the progress bar.
'''

self.prog.config(value = value)

def download(self, url) -> None:
'''
Download a video.
'''

try:
path = self.client.get(url).download(
path = self.path,
quality = phub.Quality(self.quality),
callback = dlp.percent(self.updatebar)
)

tkmb.showinfo('Success', 'Video downloaded: ' + path)
self.prog.config(value = 0) # Reset progress bar

except Exception as err:
tkmb.showerror('Error', 'Something wrong happened:' + repr(err))

def run(self, *_) -> None:
'''
Start the download.
'''

# Check if URL is right
url = self.url.get().strip()

if not phub.consts.regexes.is_valid_video_url(url):
return tkmb.showwarning('Error', 'Invalid video URL.')

# Download
threading.Thread(target = self.download, args = [url]).start()


def main(client) -> None:
App(client).mainloop()

if __name__ == '__main__':
main(phub.Client())

# EOF

0 comments on commit 23a1d46

Please sign in to comment.