1
0
mirror of https://github.com/spaam/svtplay-dl.git synced 2024-11-24 12:15:40 +01:00
svtplay-dl/lib/svtplay_dl/fetcher/http.py

39 lines
1.1 KiB
Python
Raw Normal View History

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 -*-
from __future__ import absolute_import
2013-02-12 19:15:11 +01:00
import time
from svtplay_dl.output import progress, output # FIXME use progressbar() instead
2014-04-21 16:50:24 +02:00
from svtplay_dl.fetcher import VideoRetriever
2013-02-12 19:15:11 +01:00
2014-04-21 16:50:24 +02:00
class HTTP(VideoRetriever):
def name(self):
return "http"
2014-04-21 16:50:24 +02:00
def download(self):
""" Get the stream from HTTP """
data = self.http.request("get", self.url, stream=True)
2014-04-21 16:50:24 +02:00
try:
2015-08-30 14:41:59 +02:00
total_size = data.headers['content-length']
2014-04-21 16:50:24 +02:00
except KeyError:
total_size = 0
total_size = int(total_size)
bytes_so_far = 0
file_d = output(self.options, "mp4")
2014-08-21 22:10:16 +02:00
if hasattr(file_d, "read") is False:
return
2013-02-12 19:15:11 +01:00
2014-04-21 16:50:24 +02:00
lastprogress = 0
2015-08-30 14:41:59 +02:00
for i in data.iter_content(8192):
bytes_so_far += len(i)
file_d.write(i)
2014-04-21 16:50:24 +02:00
if self.options.output != "-":
now = time.time()
if lastprogress + 1 < now:
lastprogress = now
progress(bytes_so_far, total_size)
2013-02-12 19:15:11 +01:00
2014-04-21 16:50:24 +02:00
if self.options.output != "-":
file_d.close()