2015-08-30 00:06:20 +02:00
|
|
|
from __future__ import absolute_import
|
2017-10-27 00:08:53 +02:00
|
|
|
import copy
|
|
|
|
|
2018-05-13 13:06:45 +02:00
|
|
|
from svtplay_dl.utils.output import output, ETA, progressbar
|
2018-03-13 00:33:39 +01:00
|
|
|
from svtplay_dl.utils.http import HTTP
|
2015-08-30 00:06:20 +02:00
|
|
|
|
2018-01-30 20:11:37 +01:00
|
|
|
|
2014-07-24 20:23:32 +02:00
|
|
|
class VideoRetriever(object):
|
2018-05-08 22:46:11 +02:00
|
|
|
def __init__(self, config, url, bitrate=0, **kwargs):
|
|
|
|
self.config = config
|
2014-04-21 16:50:24 +02:00
|
|
|
self.url = url
|
2014-05-01 17:13:46 +02:00
|
|
|
self.bitrate = int(bitrate)
|
|
|
|
self.kwargs = kwargs
|
2018-05-08 22:46:11 +02:00
|
|
|
self.http = HTTP(config)
|
2016-03-22 22:28:41 +01:00
|
|
|
self.finished = False
|
2016-03-26 21:38:31 +01:00
|
|
|
self.audio = kwargs.pop("audio", None)
|
2016-09-26 00:42:42 +02:00
|
|
|
self.files = kwargs.pop("files", None)
|
2018-01-04 00:45:52 +01:00
|
|
|
self.keycookie = kwargs.pop("keycookie", None)
|
2018-02-12 00:08:09 +01:00
|
|
|
self.authorization = kwargs.pop("authorization", None)
|
2018-05-13 13:06:45 +02:00
|
|
|
self.output = kwargs.pop("output", None)
|
|
|
|
self.segments = kwargs.pop("segments", None)
|
2018-05-13 01:45:23 +02:00
|
|
|
self.output_extention = None
|
2014-05-01 17:13:46 +02:00
|
|
|
|
2016-01-03 02:43:16 +01:00
|
|
|
def __repr__(self):
|
2018-05-25 22:47:48 +02:00
|
|
|
return "<Video(fetcher={}, bitrate={}>".format(self.__class__.__name__, self.bitrate)
|
2016-01-03 02:43:16 +01:00
|
|
|
|
2018-05-25 22:47:26 +02:00
|
|
|
@property
|
2014-05-01 17:13:46 +02:00
|
|
|
def name(self):
|
2014-07-24 20:19:22 +02:00
|
|
|
pass
|
2017-10-27 00:08:53 +02:00
|
|
|
|
|
|
|
def _download_url(self, url, audio=False, total_size=None):
|
|
|
|
cookies = self.kwargs["cookies"]
|
|
|
|
data = self.http.request("get", url, cookies=cookies, headers={'Range': 'bytes=0-8192'})
|
|
|
|
if not total_size:
|
|
|
|
try:
|
|
|
|
total_size = data.headers['Content-Range']
|
2018-01-30 20:11:37 +01:00
|
|
|
total_size = total_size[total_size.find("/") + 1:]
|
2017-10-27 00:08:53 +02:00
|
|
|
total_size = int(total_size)
|
|
|
|
except KeyError:
|
|
|
|
raise KeyError("Can't get the total size.")
|
|
|
|
|
|
|
|
bytes_so_far = 8192
|
|
|
|
if audio:
|
2018-05-08 22:46:11 +02:00
|
|
|
file_d = output(copy.copy(self.output), self.config, "m4a")
|
2017-10-27 00:08:53 +02:00
|
|
|
else:
|
2018-05-08 22:46:11 +02:00
|
|
|
file_d = output(self.output, self.config, "mp4")
|
2017-10-27 00:08:53 +02:00
|
|
|
|
|
|
|
if file_d is None:
|
|
|
|
return
|
|
|
|
file_d.write(data.content)
|
|
|
|
eta = ETA(total_size)
|
|
|
|
while bytes_so_far < total_size:
|
|
|
|
|
2018-05-08 22:46:11 +02:00
|
|
|
if not self.config.get("silent"):
|
2017-10-27 00:08:53 +02:00
|
|
|
eta.update(bytes_so_far)
|
|
|
|
progressbar(total_size, bytes_so_far, ''.join(["ETA: ", str(eta)]))
|
|
|
|
|
|
|
|
old = bytes_so_far + 1
|
|
|
|
bytes_so_far = total_size
|
|
|
|
|
|
|
|
bytes_range = "bytes={0}-{1}".format(old, bytes_so_far)
|
|
|
|
|
|
|
|
data = self.http.request("get", url, cookies=cookies, headers={'Range': bytes_range})
|
|
|
|
file_d.write(data.content)
|
|
|
|
|
|
|
|
file_d.close()
|
|
|
|
progressbar(bytes_so_far, total_size, "ETA: complete")
|
2018-05-13 13:06:45 +02:00
|
|
|
# progress_stream.write('\n')
|
2017-10-27 00:08:53 +02:00
|
|
|
self.finished = True
|