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-01-17 00:21:47 +01:00
|
|
|
import sys
|
2013-03-23 19:22:01 +01:00
|
|
|
import time
|
2014-08-20 20:27:45 +02:00
|
|
|
import re
|
|
|
|
import os
|
2014-08-27 22:40:31 +02:00
|
|
|
import io
|
2015-04-30 11:02:18 +02:00
|
|
|
import platform
|
2013-03-23 19:22:01 +01:00
|
|
|
from datetime import timedelta
|
2013-01-17 00:21:47 +01:00
|
|
|
|
2015-08-30 00:06:20 +02:00
|
|
|
from svtplay_dl.utils import is_py3, is_py2, filenamify, decode_html_entities, ensure_unicode
|
2014-06-26 22:55:54 +02:00
|
|
|
from svtplay_dl.utils.terminal import get_terminal_size
|
2014-08-20 20:27:45 +02:00
|
|
|
from svtplay_dl.log import log
|
2014-06-26 22:55:54 +02:00
|
|
|
|
2013-02-12 19:14:07 +01:00
|
|
|
progress_stream = sys.stderr
|
|
|
|
|
2013-03-23 19:22:01 +01:00
|
|
|
class ETA(object):
|
|
|
|
"""
|
|
|
|
An ETA class, used to calculate how long it takes to process
|
|
|
|
an arbitrary set of items. By initiating the object with the
|
|
|
|
number of items and continuously updating with current
|
|
|
|
progress, the class can calculate an estimation of how long
|
|
|
|
time remains.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, end, start=0):
|
|
|
|
"""
|
|
|
|
Parameters:
|
|
|
|
end: the end (or size, of start is 0)
|
|
|
|
start: the starting position, defaults to 0
|
|
|
|
"""
|
|
|
|
self.start = start
|
|
|
|
self.end = end
|
|
|
|
self.pos = start
|
|
|
|
|
|
|
|
self.now = time.time()
|
|
|
|
self.start_time = self.now
|
|
|
|
|
|
|
|
def update(self, pos):
|
|
|
|
"""
|
|
|
|
Set new absolute progress position.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
pos: new absolute progress
|
|
|
|
"""
|
|
|
|
self.pos = pos
|
|
|
|
self.now = time.time()
|
|
|
|
|
|
|
|
def increment(self, skip=1):
|
|
|
|
"""
|
|
|
|
Like update, but set new pos relative to old pos.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
skip: progress since last update (defaults to 1)
|
|
|
|
"""
|
|
|
|
self.update(self.pos + skip)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def left(self):
|
|
|
|
"""
|
|
|
|
returns: How many item remains?
|
|
|
|
"""
|
|
|
|
return self.end - self.pos
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
"""
|
|
|
|
returns: a time string of the format HH:MM:SS.
|
|
|
|
"""
|
|
|
|
duration = self.now - self.start_time
|
|
|
|
|
|
|
|
# Calculate how long it takes to process one item
|
|
|
|
try:
|
|
|
|
elm_time = duration / (self.end - self.left)
|
|
|
|
except ZeroDivisionError:
|
|
|
|
return "(unknown)"
|
|
|
|
|
|
|
|
return str(timedelta(seconds=int(elm_time * self.left)))
|
|
|
|
|
|
|
|
|
2014-12-26 02:04:29 +01:00
|
|
|
def progress(byte, total, extra=""):
|
2013-02-12 19:14:07 +01:00
|
|
|
""" Print some info about how much we have downloaded """
|
2013-03-03 10:52:48 +01:00
|
|
|
if total == 0:
|
|
|
|
progresstr = "Downloaded %dkB bytes" % (byte >> 10)
|
|
|
|
progress_stream.write(progresstr + '\r')
|
2013-04-19 17:44:21 +02:00
|
|
|
return
|
|
|
|
progressbar(total, byte, extra)
|
2013-02-12 19:14:07 +01:00
|
|
|
|
2013-01-17 00:21:47 +01:00
|
|
|
def progressbar(total, pos, msg=""):
|
|
|
|
"""
|
|
|
|
Given a total and a progress position, output a progress bar
|
|
|
|
to stderr. It is important to not output anything else while
|
|
|
|
using this, as it relies soley on the behavior of carriage
|
|
|
|
return (\\r).
|
|
|
|
|
|
|
|
Can also take an optioal message to add after the
|
2014-07-24 17:39:42 +02:00
|
|
|
progressbar. It must not contain newlines.
|
2013-01-17 00:21:47 +01:00
|
|
|
|
|
|
|
The progress bar will look something like this:
|
|
|
|
|
|
|
|
[099/500][=========...............................] ETA: 13:36:59
|
|
|
|
|
|
|
|
Of course, the ETA part should be supplied be the calling
|
|
|
|
function.
|
|
|
|
"""
|
2014-08-27 19:29:00 +02:00
|
|
|
width = get_terminal_size()[0] - 35
|
2013-01-17 00:21:47 +01:00
|
|
|
rel_pos = int(float(pos)/total*width)
|
2013-04-19 17:38:37 +02:00
|
|
|
bar = ''.join(["=" * rel_pos, "." * (width - rel_pos)])
|
2013-01-17 00:21:47 +01:00
|
|
|
|
|
|
|
# Determine how many digits in total (base 10)
|
|
|
|
digits_total = len(str(total))
|
|
|
|
fmt_width = "%0" + str(digits_total) + "d"
|
|
|
|
fmt = "\r[" + fmt_width + "/" + fmt_width + "][%s] %s"
|
|
|
|
|
|
|
|
progress_stream.write(fmt % (pos, total, bar, msg))
|
|
|
|
|
2015-04-30 11:02:18 +02:00
|
|
|
def filename(options, stream):
|
|
|
|
if options.output:
|
|
|
|
if is_py2:
|
|
|
|
if platform.system() == "Windows":
|
2015-04-30 11:03:52 +02:00
|
|
|
options.output = options.output.decode("latin1")
|
2015-04-30 11:02:18 +02:00
|
|
|
else:
|
|
|
|
options.output = options.output.decode("utf-8")
|
|
|
|
options.output = options.output.replace('"', '').replace("'", "").rstrip('\\')
|
|
|
|
if not options.output or os.path.isdir(options.output):
|
2015-08-30 00:06:20 +02:00
|
|
|
data = ensure_unicode(stream.get_urldata())
|
2015-04-30 11:02:18 +02:00
|
|
|
if data is None:
|
|
|
|
return False
|
|
|
|
match = re.search(r"(?i)<title[^>]*>\s*(.*?)\s*</title>", data, re.S)
|
|
|
|
if match:
|
|
|
|
options.output_auto = True
|
|
|
|
title_tag = decode_html_entities(match.group(1))
|
|
|
|
if not options.output:
|
|
|
|
options.output = filenamify(title_tag)
|
|
|
|
else:
|
|
|
|
# output is a directory
|
|
|
|
options.output = os.path.join(options.output, filenamify(title_tag))
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2015-08-31 22:46:45 +02:00
|
|
|
def output(options, extention="mp4", openfd=True, mode="wb"):
|
2014-08-27 22:40:31 +02:00
|
|
|
if is_py3:
|
|
|
|
file_d = io.IOBase
|
|
|
|
else:
|
|
|
|
file_d = file
|
|
|
|
|
2014-08-20 20:27:45 +02:00
|
|
|
if options.output != "-":
|
2014-12-30 21:18:01 +01:00
|
|
|
ext = re.search(r"(\.[a-z0-9]+)$", options.output)
|
2014-08-20 20:27:45 +02:00
|
|
|
if not ext:
|
|
|
|
options.output = "%s.%s" % (options.output, extention)
|
2015-01-16 21:58:42 +01:00
|
|
|
if extention == "srt" and ext:
|
|
|
|
options.output = "%s.srt" % options.output[:options.output.rfind(ext.group(1))]
|
2014-08-20 20:27:45 +02:00
|
|
|
log.info("Outfile: %s", options.output)
|
2015-01-28 21:02:27 +01:00
|
|
|
if os.path.isfile(options.output) or \
|
|
|
|
findexpisode(os.path.dirname(os.path.realpath(options.output)), options.service, os.path.basename(options.output)):
|
2014-12-31 19:55:53 +01:00
|
|
|
if extention == "srt":
|
|
|
|
if not options.force_subtitle:
|
|
|
|
log.error("File already exists. Use --force-subtitle to overwrite")
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
if not options.force:
|
|
|
|
log.error("File already exists. Use --force to overwrite")
|
|
|
|
return None
|
2014-08-20 20:27:45 +02:00
|
|
|
if openfd:
|
2015-08-31 22:46:45 +02:00
|
|
|
file_d = open(options.output, mode)
|
2014-08-20 20:27:45 +02:00
|
|
|
else:
|
|
|
|
if openfd:
|
|
|
|
if is_py3:
|
|
|
|
file_d = sys.stdout.buffer
|
|
|
|
else:
|
|
|
|
file_d = sys.stdout
|
|
|
|
|
|
|
|
return file_d
|
2014-08-27 22:40:31 +02:00
|
|
|
|
|
|
|
def findexpisode(directory, service, name):
|
2014-12-30 21:18:48 +01:00
|
|
|
match = re.search(r"-(\w+)-\w+.(\w{2,3})$", name)
|
2014-08-27 22:40:31 +02:00
|
|
|
if not match:
|
|
|
|
return False
|
|
|
|
videoid = match.group(1)
|
2014-12-30 21:18:48 +01:00
|
|
|
extention = match.group(2)
|
2014-08-27 22:40:31 +02:00
|
|
|
files = [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]
|
|
|
|
for i in files:
|
2014-12-30 21:18:48 +01:00
|
|
|
match = re.search(r"-(\w+)-\w+.(\w{2,3})$", i)
|
2014-08-27 22:40:31 +02:00
|
|
|
if match:
|
|
|
|
if service:
|
2014-12-30 21:18:48 +01:00
|
|
|
if extention == "srt":
|
|
|
|
if name.find(service) and match.group(1) == videoid and match.group(2) == extention:
|
|
|
|
return True
|
2015-01-01 22:11:27 +01:00
|
|
|
elif match.group(2) != "srt":
|
2014-12-30 21:18:48 +01:00
|
|
|
if name.find(service) and match.group(1) == videoid:
|
|
|
|
return True
|
2014-08-27 22:40:31 +02:00
|
|
|
|
|
|
|
return False
|