Skip to content

Commit

Permalink
download script
Browse files Browse the repository at this point in the history
  • Loading branch information
Egsago-n committed Jun 22, 2023
1 parent f46ccc8 commit e86c8a7
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/phub/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'''
Main PHUB script.
'''

import phub
import click

@click.command()
@click.option('--url', '-u', help = 'URL of the video.')
@click.option('--key', '-k', help = 'Viewkey of the video.')
@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.')

def main(url: str, key: str, quality: str, output: str, noconfirm: str) -> None:
'''
Main script.
'''

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)

except Exception as err:
return click.secho(f'Error: {type(err)} {err}', fg = 'red', err = 1)

return click.echo('\rDownloaded video at ' + click.style(path, fg = 'green'))


if __name__ == '__main__':
main()

# EOF

0 comments on commit e86c8a7

Please sign in to comment.