Skip to content

Commit

Permalink
more updates to new progress bar UI
Browse files Browse the repository at this point in the history
thanks again @kevinmarks, this is shaping up great!
  • Loading branch information
snarfed committed Apr 2, 2016
1 parent 72708e6 commit f4a3b26
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def read(filename):
S3_BUCKET = 'huffduff-video'
S3_BASE = 'https://%s.s3-us-west-2.amazonaws.com/' % S3_BUCKET

# ffmpeg on Ryan's laptop is installed in /usr/local/bin, so add it to PATH.
if '/usr/local/bin' not in os.environ['PATH']:
os.environ['PATH'] += ':/usr/local/bin'


def application(environ, start_response):
"""Hand-rolled WSGI application so I can stream output.
Expand Down Expand Up @@ -62,9 +66,6 @@ def run():
<style> #progress span {display:none;}
#progress span:last-of-type {display:inline;}
</style>
<script type="text/javascript">
window.setInterval(function() { window.scrollTo(0, document.body.scrollHeight); }, 500);
</script>
<body>
<h1>huffduff-video</h1>
<div id="progress">
Expand All @@ -73,17 +74,22 @@ def run():
# function to print out status while downloading
def download_progress_hook(progress):
status = progress.get('status')
if status in ('finished', 'error'):
if status == 'finished':
msg = '<br />Extracting audio (this can take a while)...\n'
elif status == 'error':
# we always get an 'error' progress when the video finishes downloading.
# not sure why. ignore it.
return
elif status == 'downloading':
p = lambda field: progress.get(field) or ''
msg = '<span><progress max="100" value="%s"></progress> of %s at %s in %s...</span>' % (
p('_percent_str'), p('_total_bytes_str') or p('_total_bytes_estimate_str'),
p('_speed_str'), p('_eta_str'))
percent = float(p('_percent_str').strip('%') or '0')
msg = ('<span><progress max="100" value="%s"></progress><br /> '
'%s of %s at %s in %s...</span>\n' % (
percent, p('_downloaded_bytes_str'),
p('_total_bytes_str') or p('_total_bytes_estimate_str'),
p('_speed_str'), p('_eta_str')))
else:
msg = status+ '<br />\n'
msg = status + '<br />\n'
write(msg)

# fetch video info (resolves URL) to see if we've already downloaded it
Expand Down Expand Up @@ -127,7 +133,7 @@ def download_progress_hook(progress):
yield 'Already downloaded! <br />\n'
else:
# download video and extract mp3
yield 'Downloading and extracting audio...<br />\n'
yield 'Downloading...<br />\n'
with handle_errors(write):
youtube_dl.YoutubeDL(options).download([url])

Expand All @@ -136,7 +142,9 @@ def download_progress_hook(progress):
yield 'Uploading to S3...<br />\n'

def upload_callback(sent, total):
write('<span><progress max="100" value="%s"></progress> </span>\n' % (sent * 100 / total))
write('<span><progress max="100" value="%s"></progress><br /> '
'%.2fMB of %.2fMB</span>\n' % (
(sent * 100 / total), float(sent) / 1000000, float(total) / 1000000))

key.set_contents_from_filename(filename, cb=upload_callback)
key.make_public()
Expand All @@ -159,7 +167,7 @@ def upload_callback(sent, total):
description += footer

# open 'Huffduff it' page
yield """Opening Huffduffer dialog...
yield """\n<br />Opening Huffduffer dialog...
<script type="text/javascript">
window.location = "https://huffduffer.com/add?popup=true&%s";
</script>
Expand Down

0 comments on commit f4a3b26

Please sign in to comment.