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 time
|
|
|
|
|
2014-08-20 20:27:45 +02:00
|
|
|
from svtplay_dl.output import progress, output # FIXME use progressbar() instead
|
2013-03-17 19:55:19 +01:00
|
|
|
from svtplay_dl.log import log
|
2013-04-21 13:42:33 +02:00
|
|
|
from svtplay_dl.utils.urllib import urlopen, Request, HTTPError
|
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):
|
2014-05-01 17:13:46 +02:00
|
|
|
def name(self):
|
|
|
|
return "http"
|
|
|
|
|
2014-04-21 16:50:24 +02:00
|
|
|
def download(self):
|
|
|
|
""" Get the stream from HTTP """
|
|
|
|
request = Request(self.url)
|
|
|
|
request.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
|
|
|
|
try:
|
|
|
|
response = urlopen(request)
|
|
|
|
except HTTPError as e:
|
|
|
|
log.error("Something wrong with that url")
|
2014-12-08 23:07:02 +01:00
|
|
|
return
|
2014-04-21 16:50:24 +02:00
|
|
|
try:
|
|
|
|
total_size = response.info()['Content-Length']
|
|
|
|
except KeyError:
|
|
|
|
total_size = 0
|
|
|
|
total_size = int(total_size)
|
|
|
|
bytes_so_far = 0
|
2014-08-20 20:27:45 +02:00
|
|
|
|
2014-12-30 21:18:01 +01:00
|
|
|
file_d = output(self.options, "mp4")
|
2014-08-21 22:10:16 +02:00
|
|
|
if hasattr(file_d, "read") is False:
|
2014-08-20 20:27:45 +02:00
|
|
|
return
|
2013-02-12 19:15:11 +01:00
|
|
|
|
2014-04-21 16:50:24 +02:00
|
|
|
lastprogress = 0
|
|
|
|
while 1:
|
|
|
|
chunk = response.read(8192)
|
|
|
|
bytes_so_far += len(chunk)
|
2013-02-12 19:15:11 +01:00
|
|
|
|
2014-04-21 16:50:24 +02:00
|
|
|
if not chunk:
|
|
|
|
break
|
2013-02-12 19:15:11 +01:00
|
|
|
|
2014-04-21 16:50:24 +02:00
|
|
|
file_d.write(chunk)
|
|
|
|
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()
|
2013-02-12 19:15:11 +01:00
|
|
|
|