mirror of
https://github.com/spaam/svtplay-dl.git
synced 2024-11-24 04:05:39 +01:00
Breakout http fetcher to own module
This commit is contained in:
parent
5fed1b2a4a
commit
6266a757f3
45
lib/svtplay/http.py
Normal file
45
lib/svtplay/http.py
Normal file
@ -0,0 +1,45 @@
|
||||
import sys
|
||||
import time
|
||||
import re
|
||||
|
||||
from lib.svtplay.output import progress # FIXME use progressbar() instead
|
||||
from lib.svtplay.log import log
|
||||
|
||||
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)
|
||||
total_size = response.info()['Content-Length']
|
||||
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()
|
||||
|
Loading…
Reference in New Issue
Block a user