2013-03-02 21:26:28 +01:00
|
|
|
# ex:ts=4:sw=4:sts=4:et
|
|
|
|
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2013-03-01 23:39:42 +01:00
|
|
|
from __future__ import absolute_import
|
2013-02-12 19:15:11 +01:00
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import re
|
|
|
|
|
2013-03-01 23:39:42 +01:00
|
|
|
from svtplay.output import progress # FIXME use progressbar() instead
|
|
|
|
from svtplay.log import log
|
2013-02-12 19:15:11 +01:00
|
|
|
|
|
|
|
if sys.version_info > (3, 0):
|
|
|
|
from urllib.request import urlopen
|
|
|
|
else:
|
|
|
|
from urllib2 import urlopen
|
|
|
|
|
|
|
|
def download_http(options, url):
|
|
|
|
""" Get the stream from HTTP """
|
|
|
|
response = urlopen(url)
|
2013-03-03 10:52:48 +01:00
|
|
|
try:
|
|
|
|
total_size = response.info()['Content-Length']
|
|
|
|
except KeyError:
|
|
|
|
total_size = 0
|
2013-02-12 19:15:11 +01:00
|
|
|
total_size = int(total_size)
|
|
|
|
bytes_so_far = 0
|
|
|
|
if options.output != "-":
|
|
|
|
extension = re.search("(\.[a-z0-9]+)$", url)
|
|
|
|
if extension:
|
|
|
|
options.output = options.output + extension.group(1)
|
|
|
|
log.info("Outfile: %s", options.output)
|
|
|
|
file_d = open(options.output, "wb")
|
|
|
|
else:
|
|
|
|
file_d = sys.stdout
|
|
|
|
|
|
|
|
lastprogress = 0
|
|
|
|
while 1:
|
|
|
|
chunk = response.read(8192)
|
|
|
|
bytes_so_far += len(chunk)
|
|
|
|
|
|
|
|
if not chunk:
|
|
|
|
break
|
|
|
|
|
|
|
|
file_d.write(chunk)
|
|
|
|
if options.output != "-":
|
|
|
|
now = time.time()
|
|
|
|
if lastprogress + 1 < now:
|
|
|
|
lastprogress = now
|
|
|
|
progress(bytes_so_far, total_size)
|
|
|
|
|
|
|
|
if options.output != "-":
|
|
|
|
file_d.close()
|
|
|
|
|