1
0
mirror of https://github.com/spaam/svtplay-dl.git synced 2024-11-28 06:04:17 +01:00
svtplay-dl/lib/svtplay_dl/fetcher/__init__.py

74 lines
2.6 KiB
Python
Raw Normal View History

2017-10-27 00:08:53 +02:00
import copy
from svtplay_dl.utils.http import HTTP
2019-08-25 00:40:39 +02:00
from svtplay_dl.utils.output import ETA
from svtplay_dl.utils.output import output
from svtplay_dl.utils.output import progressbar
2018-01-30 20:11:37 +01:00
2019-08-25 00:33:51 +02:00
class VideoRetriever:
2020-06-25 20:55:48 +02:00
def __init__(self, config, url, bitrate, output, **kwargs):
2018-05-08 22:46:11 +02:00
self.config = config
2014-04-21 16:50:24 +02:00
self.url = url
2020-06-25 20:55:48 +02:00
self.bitrate = int(bitrate) if bitrate else 0
self.kwargs = kwargs
2018-05-08 22:46:11 +02:00
self.http = HTTP(config)
self.finished = False
2016-03-26 21:38:31 +01:00
self.audio = kwargs.pop("audio", None)
self.files = kwargs.pop("files", None)
self.keycookie = kwargs.pop("keycookie", None)
self.authorization = kwargs.pop("authorization", None)
2020-06-25 20:55:48 +02:00
self.output = output
2018-05-13 13:06:45 +02:00
self.segments = kwargs.pop("segments", None)
2018-05-13 01:45:23 +02:00
self.output_extention = None
2020-06-25 20:55:48 +02:00
channels = kwargs.pop("channels", None)
codec = kwargs.pop("codec", "h264")
self.format = "{}-{}".format(codec, channels) if channels else codec
def __repr__(self):
2020-06-25 20:55:48 +02:00
return "<Video(fetcher={}, bitrate={} format={}>".format(self.__class__.__name__, self.bitrate, self.format)
2018-05-25 22:47:26 +02:00
@property
def name(self):
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"})
2017-10-27 00:08:53 +02:00
if not total_size:
try:
2019-08-25 00:27:31 +02:00
total_size = data.headers["Content-Range"]
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)
2019-08-25 00:27:31 +02:00
progressbar(total_size, bytes_so_far, "".join(["ETA: ", str(eta)]))
2017-10-27 00:08:53 +02:00
old = bytes_so_far + 1
bytes_so_far = total_size
2019-08-25 00:33:51 +02:00
bytes_range = "bytes={}-{}".format(old, bytes_so_far)
2017-10-27 00:08:53 +02:00
data = self.http.request("get", url, cookies=cookies, headers={"Range": bytes_range})
2017-10-27 00:08:53 +02:00
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