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-04-21 12:56:59 +02:00
|
|
|
import socket
|
2013-01-17 00:21:47 +01:00
|
|
|
import logging
|
2013-03-24 20:20:16 +01:00
|
|
|
import re
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
import json
|
2014-01-05 16:32:23 +01:00
|
|
|
import time
|
2013-01-17 00:21:47 +01:00
|
|
|
|
2013-12-30 01:35:08 +01:00
|
|
|
is_py2 = (sys.version_info[0] == 2)
|
|
|
|
is_py3 = (sys.version_info[0] == 3)
|
|
|
|
is_py2_old = (sys.version_info < (2, 7))
|
|
|
|
|
2013-04-27 13:46:38 +02:00
|
|
|
# Used for UA spoofing in get_http_data()
|
|
|
|
FIREFOX_UA = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3'
|
|
|
|
|
|
|
|
from svtplay_dl.utils.urllib import build_opener, Request, HTTPCookieProcessor, \
|
2013-04-21 13:42:33 +02:00
|
|
|
HTTPRedirectHandler, HTTPError, URLError, \
|
|
|
|
addinfourl, CookieJar
|
2013-01-17 00:21:47 +01:00
|
|
|
|
|
|
|
log = logging.getLogger('svtplay_dl')
|
|
|
|
progress_stream = sys.stderr
|
|
|
|
|
2013-04-14 21:08:12 +02:00
|
|
|
class NoRedirectHandler(HTTPRedirectHandler):
|
2013-04-27 13:46:38 +02:00
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
2013-04-14 21:08:12 +02:00
|
|
|
def http_error_302(self, req, fp, code, msg, headers):
|
|
|
|
infourl = addinfourl(fp, headers, req.get_full_url())
|
|
|
|
infourl.status = code
|
|
|
|
infourl.code = code
|
|
|
|
return infourl
|
|
|
|
http_error_300 = http_error_302
|
|
|
|
http_error_301 = http_error_302
|
|
|
|
http_error_303 = http_error_302
|
|
|
|
http_error_307 = http_error_302
|
|
|
|
|
2013-08-13 20:06:46 +02:00
|
|
|
def get_http_data(url, header=None, data=None, useragent=FIREFOX_UA,
|
2013-04-27 13:46:38 +02:00
|
|
|
referer=None, cookiejar=None):
|
2013-01-17 00:21:47 +01:00
|
|
|
""" Get the page to parse it for streams """
|
2013-03-14 22:19:13 +01:00
|
|
|
if not cookiejar:
|
|
|
|
cookiejar = CookieJar()
|
2013-01-17 00:21:47 +01:00
|
|
|
|
2014-01-05 16:32:23 +01:00
|
|
|
log.debug("HTTP getting %r", url)
|
|
|
|
starttime = time.time()
|
|
|
|
|
2013-08-13 20:06:46 +02:00
|
|
|
request = Request(url)
|
|
|
|
standard_header = {'Referer': referer, 'User-Agent': useragent}
|
|
|
|
for key, value in [head for head in standard_header.items() if head[1]]:
|
|
|
|
request.add_header(key, value)
|
|
|
|
if header:
|
|
|
|
for key, value in [head for head in header.items() if head[1]]:
|
|
|
|
request.add_header(key, value)
|
|
|
|
if data:
|
2013-12-30 01:37:13 +01:00
|
|
|
request.add_data(data)
|
2013-04-27 13:46:38 +02:00
|
|
|
|
|
|
|
opener = build_opener(HTTPCookieProcessor(cookiejar))
|
|
|
|
|
2013-01-17 00:21:47 +01:00
|
|
|
try:
|
2013-04-27 13:46:38 +02:00
|
|
|
response = opener.open(request)
|
2013-01-17 00:21:47 +01:00
|
|
|
except HTTPError as e:
|
|
|
|
log.error("Something wrong with that url")
|
2013-12-30 01:43:59 +01:00
|
|
|
log.error("Error code: %s", e.code)
|
2013-01-17 00:21:47 +01:00
|
|
|
sys.exit(5)
|
|
|
|
except URLError as e:
|
|
|
|
log.error("Something wrong with that url")
|
2013-12-30 01:43:59 +01:00
|
|
|
log.error("Error code: %s", e.reason)
|
2013-01-17 00:21:47 +01:00
|
|
|
sys.exit(5)
|
|
|
|
except ValueError as e:
|
|
|
|
log.error("Try adding http:// before the url")
|
|
|
|
sys.exit(5)
|
2013-12-30 01:35:08 +01:00
|
|
|
if is_py3:
|
2013-01-17 00:21:47 +01:00
|
|
|
data = response.read()
|
|
|
|
try:
|
|
|
|
data = data.decode("utf-8")
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
data = response.read()
|
|
|
|
except socket.error as e:
|
|
|
|
log.error("Lost the connection to the server")
|
|
|
|
sys.exit(5)
|
|
|
|
response.close()
|
2014-01-05 16:32:23 +01:00
|
|
|
|
|
|
|
spent_time = time.time() - starttime
|
|
|
|
bps = 8 * len(data) / max(spent_time, 0.001)
|
|
|
|
|
|
|
|
log.debug("HTTP got %d bytes from %r in %.2fs (= %dbps)",
|
|
|
|
len(data), url, spent_time, bps)
|
|
|
|
|
2013-01-17 00:21:47 +01:00
|
|
|
return data
|
|
|
|
|
2013-04-14 21:08:12 +02:00
|
|
|
def check_redirect(url):
|
2013-04-27 13:46:38 +02:00
|
|
|
opener = build_opener(NoRedirectHandler())
|
|
|
|
opener.addheaders += [('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')]
|
|
|
|
response = opener.open(url)
|
2013-04-14 21:08:12 +02:00
|
|
|
if response.code in (300, 301, 302, 303, 307):
|
|
|
|
return response.headers["location"]
|
|
|
|
else:
|
|
|
|
return url
|
|
|
|
|
2013-03-12 19:52:49 +01:00
|
|
|
def timestr(msec):
|
|
|
|
"""
|
|
|
|
Convert a millisecond value to a string of the following
|
|
|
|
format:
|
|
|
|
|
|
|
|
HH:MM:SS,SS
|
|
|
|
|
|
|
|
with 10 millisecond precision. Note the , seperator in
|
|
|
|
the seconds.
|
|
|
|
"""
|
|
|
|
sec = float(msec) / 1000
|
|
|
|
|
|
|
|
hours = int(sec / 3600)
|
|
|
|
sec -= hours * 3600
|
|
|
|
|
|
|
|
minutes = int(sec / 60)
|
|
|
|
sec -= minutes * 60
|
|
|
|
|
|
|
|
output = "%02d:%02d:%05.2f" % (hours, minutes, sec)
|
2013-03-10 13:47:12 +01:00
|
|
|
return output.replace(".", ",")
|
|
|
|
|
|
|
|
def norm(name):
|
|
|
|
if name[0] == "{":
|
2013-04-21 12:33:35 +02:00
|
|
|
_, tag = name[1:].split("}")
|
2013-03-10 13:47:12 +01:00
|
|
|
return tag
|
|
|
|
else:
|
|
|
|
return name
|
|
|
|
|
2013-05-29 23:19:37 +02:00
|
|
|
def subtitle_tt(options, subtitle):
|
2013-03-10 13:47:12 +01:00
|
|
|
i = 1
|
|
|
|
data = ""
|
|
|
|
skip = False
|
2013-05-29 23:19:37 +02:00
|
|
|
tree = ET.ElementTree(ET.fromstring(subtitle))
|
2013-03-10 13:47:12 +01:00
|
|
|
for node in tree.iter():
|
|
|
|
tag = norm(node.tag)
|
|
|
|
if tag == "p":
|
|
|
|
if skip:
|
|
|
|
data = data + "\n"
|
2013-09-15 00:28:23 +02:00
|
|
|
begin = node.attrib["begin"]
|
2013-10-28 20:16:12 +01:00
|
|
|
if not ("dur" in node.attrib):
|
|
|
|
duration = node.attrib["duration"]
|
|
|
|
else:
|
|
|
|
duration = node.attrib["dur"]
|
2013-09-15 00:28:23 +02:00
|
|
|
if not ("end" in node.attrib):
|
|
|
|
begin2 = begin.split(":")
|
|
|
|
duration2 = duration.split(":")
|
|
|
|
sec = float(begin2[2]) + float(duration2[2])
|
|
|
|
end = "%02d:%02d:%06.3f" % (int(begin[0]), int(begin[1]), sec)
|
2013-10-28 20:16:12 +01:00
|
|
|
else:
|
|
|
|
end = node.attrib["end"]
|
2013-09-15 00:28:23 +02:00
|
|
|
data += '%s\n%s --> %s\n' % (i, begin.replace(".",","), end.replace(".",","))
|
2013-03-10 13:47:12 +01:00
|
|
|
data += '%s\n' % node.text.strip(' \t\n\r')
|
|
|
|
skip = True
|
|
|
|
i += 1
|
|
|
|
if tag == "br":
|
|
|
|
if node.tail:
|
|
|
|
data += '%s\n\n' % node.tail.strip(' \t\n\r')
|
|
|
|
skip = False
|
2013-05-05 12:57:42 +02:00
|
|
|
filename = re.search(r"(.*)\.[a-z0-9]{2,3}$", options.output)
|
2013-03-10 13:47:12 +01:00
|
|
|
if filename:
|
|
|
|
options.output = "%s.srt" % filename.group(1)
|
|
|
|
log.info("Subtitle: %s", options.output)
|
|
|
|
fd = open(options.output, "w")
|
2013-12-30 01:35:08 +01:00
|
|
|
if is_py2:
|
2013-05-29 23:20:15 +02:00
|
|
|
data = data.encode('utf8')
|
2013-03-10 13:47:12 +01:00
|
|
|
fd.write(data)
|
|
|
|
fd.close()
|
|
|
|
|
|
|
|
def subtitle_json(options, data):
|
|
|
|
data = json.loads(data)
|
|
|
|
number = 1
|
|
|
|
subs = ""
|
|
|
|
for i in data:
|
|
|
|
subs += "%s\n%s --> %s\n" % (number, timestr(int(i["startMillis"])), timestr(int(i["endMillis"])))
|
|
|
|
subs += "%s\n\n" % i["text"]
|
|
|
|
number += 1
|
|
|
|
|
2013-05-05 12:57:42 +02:00
|
|
|
filename = re.search(r"(.*)\.[a-z0-9]{2,3}$", options.output)
|
2013-03-10 13:47:12 +01:00
|
|
|
if filename:
|
|
|
|
options.output = "%s.srt" % filename.group(1)
|
|
|
|
log.info("Subtitle: %s", options.output)
|
|
|
|
fd = open(options.output, "w")
|
|
|
|
fd.write(subs)
|
|
|
|
fd.close()
|
|
|
|
|
|
|
|
def subtitle_sami(options, data):
|
|
|
|
tree = ET.XML(data)
|
|
|
|
subt = tree.find("Font")
|
|
|
|
subs = ""
|
2013-06-27 23:17:16 +02:00
|
|
|
n = 0
|
2013-03-10 13:47:12 +01:00
|
|
|
for i in subt.getiterator():
|
|
|
|
if i.tag == "Subtitle":
|
2013-06-27 23:17:16 +02:00
|
|
|
n = i.attrib["SpotNumber"]
|
|
|
|
if i.attrib["SpotNumber"] == "1":
|
2013-03-10 13:47:12 +01:00
|
|
|
subs += "%s\n%s --> %s\n" % (i.attrib["SpotNumber"], i.attrib["TimeIn"], i.attrib["TimeOut"])
|
|
|
|
else:
|
|
|
|
subs += "\n%s\n%s --> %s\n" % (i.attrib["SpotNumber"], i.attrib["TimeIn"], i.attrib["TimeOut"])
|
|
|
|
else:
|
2013-06-27 23:17:16 +02:00
|
|
|
if n > 0:
|
|
|
|
subs += "%s\n" % i.text
|
2013-03-10 13:47:12 +01:00
|
|
|
|
2013-05-05 12:57:42 +02:00
|
|
|
filename = re.search(r"(.*)\.[a-z0-9]{2,3}$", options.output)
|
2013-03-10 13:47:12 +01:00
|
|
|
if filename:
|
|
|
|
options.output = "%s.srt" % filename.group(1)
|
|
|
|
log.info("Subtitle: %s", options.output)
|
|
|
|
fd = open(options.output, "w")
|
2013-12-30 01:35:08 +01:00
|
|
|
if is_py2:
|
2013-06-27 23:03:14 +02:00
|
|
|
subs = subs.encode('utf8')
|
2013-03-10 13:47:12 +01:00
|
|
|
fd.write(subs)
|
|
|
|
fd.close()
|
|
|
|
|
|
|
|
def subtitle_smi(options, data):
|
|
|
|
recomp = re.compile(r'<SYNC Start=(\d+)>\s+<P Class=\w+>(.*)<br>\s+<SYNC Start=(\d+)>\s+<P Class=\w+>', re.M|re.I|re.U)
|
|
|
|
number = 1
|
|
|
|
subs = ""
|
2013-05-29 23:20:15 +02:00
|
|
|
for i in recomp.finditer(str(data)):
|
2013-03-10 13:47:12 +01:00
|
|
|
subs += "%s\n%s --> %s\n" % (number, timestr(i.group(1)), timestr(i.group(3)))
|
|
|
|
text = "%s\n\n" % i.group(2)
|
|
|
|
subs += text.replace("<br>", "\n")
|
|
|
|
number += 1
|
|
|
|
|
2013-05-05 12:57:42 +02:00
|
|
|
filename = re.search(r"(.*)\.[a-z0-9]{2,3}$", options.output)
|
2013-03-10 13:47:12 +01:00
|
|
|
if filename:
|
|
|
|
options.output = "%s.srt" % filename.group(1)
|
|
|
|
log.info("Subtitle: %s", options.output)
|
|
|
|
fd = open(options.output, "w")
|
|
|
|
fd.write(subs)
|
|
|
|
fd.close()
|
|
|
|
|
|
|
|
def subtitle_wsrt(options, data):
|
2013-05-05 12:57:42 +02:00
|
|
|
recomp = re.compile(r"(\d+)\r\n([\d:\.]+ --> [\d:\.]+)?([^\r\n]+)?\r\n([^\r\n]+)\r\n(([^\r\n]*)\r\n)?")
|
2013-03-10 13:47:12 +01:00
|
|
|
srt = ""
|
|
|
|
for i in recomp.finditer(data):
|
|
|
|
sub = "%s\n%s\n%s\n" % (i.group(1), i.group(2).replace(".", ","), i.group(4))
|
|
|
|
if len(i.group(6)) > 0:
|
|
|
|
sub += "%s\n" % i.group(6)
|
|
|
|
sub += "\n"
|
|
|
|
sub = re.sub('<[^>]*>', '', sub)
|
|
|
|
srt += sub
|
2013-05-05 12:57:42 +02:00
|
|
|
filename = re.search(r"(.*)\.[a-z0-9]{2,3}$", options.output)
|
2013-03-10 13:47:12 +01:00
|
|
|
if filename:
|
|
|
|
options.output = "%s.srt" % filename.group(1)
|
|
|
|
log.info("Subtitle: %s", options.output)
|
|
|
|
fd = open(options.output, "w")
|
|
|
|
fd.write(srt)
|
|
|
|
fd.close()
|
|
|
|
|
2013-01-17 00:21:47 +01:00
|
|
|
def select_quality(options, streams):
|
2013-03-23 18:26:48 +01:00
|
|
|
available = sorted(streams.keys(), key=int)
|
2013-01-17 00:21:47 +01:00
|
|
|
|
2013-03-25 19:27:26 +01:00
|
|
|
try:
|
|
|
|
optq = int(options.quality)
|
|
|
|
except ValueError:
|
|
|
|
log.error("Requested quality need to be a number")
|
|
|
|
sys.exit(4)
|
2013-03-23 18:26:48 +01:00
|
|
|
if optq:
|
2013-03-25 19:27:26 +01:00
|
|
|
try:
|
|
|
|
optf = int(options.flexibleq)
|
|
|
|
except ValueError:
|
|
|
|
log.error("Flexible-quality need to be a number")
|
|
|
|
sys.exit(4)
|
2013-03-23 18:26:48 +01:00
|
|
|
if not optf:
|
|
|
|
wanted = [optq]
|
|
|
|
else:
|
|
|
|
wanted = range(optq-optf, optq+optf+1)
|
2013-01-17 00:21:47 +01:00
|
|
|
else:
|
2013-03-23 18:26:48 +01:00
|
|
|
wanted = [available[-1]]
|
2013-01-17 00:21:47 +01:00
|
|
|
|
2013-03-23 18:26:48 +01:00
|
|
|
selected = None
|
|
|
|
for q in available:
|
|
|
|
if q in wanted:
|
|
|
|
selected = q
|
|
|
|
break
|
|
|
|
|
|
|
|
if not selected:
|
|
|
|
log.error("Can't find that quality. Try one of: %s (or try --flexible-quality)",
|
2013-04-27 13:20:15 +02:00
|
|
|
", ".join([str(elm) for elm in available]))
|
2013-01-17 00:21:47 +01:00
|
|
|
sys.exit(4)
|
|
|
|
|
2013-03-23 18:26:48 +01:00
|
|
|
return streams[selected]
|
2013-01-17 00:21:47 +01:00
|
|
|
|