2014-04-21 19:52:09 +02:00
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
import json
|
|
|
|
import re
|
|
|
|
from svtplay_dl.log import log
|
2015-09-20 15:15:50 +02:00
|
|
|
from svtplay_dl.utils import is_py2, is_py3, decode_html_entities
|
2015-03-01 21:44:55 +01:00
|
|
|
from svtplay_dl.utils.io import StringIO
|
2014-12-30 21:20:03 +01:00
|
|
|
from svtplay_dl.output import output
|
2015-08-30 00:06:20 +02:00
|
|
|
from requests import Session
|
2015-09-20 15:15:50 +02:00
|
|
|
from requests import __build__ as requests_version
|
2014-04-21 19:52:09 +02:00
|
|
|
|
2015-01-25 14:41:29 +01:00
|
|
|
|
2014-07-28 15:53:23 +02:00
|
|
|
class subtitle(object):
|
2014-08-31 01:20:36 +02:00
|
|
|
def __init__(self, options, subtype, url):
|
2014-04-21 19:52:09 +02:00
|
|
|
self.url = url
|
|
|
|
self.subtitle = None
|
2014-08-31 01:20:36 +02:00
|
|
|
self.options = options
|
|
|
|
self.subtype = subtype
|
2015-08-30 00:06:20 +02:00
|
|
|
self.http = Session()
|
2014-04-21 19:52:09 +02:00
|
|
|
|
2014-08-31 01:20:36 +02:00
|
|
|
def download(self):
|
2015-09-20 15:15:50 +02:00
|
|
|
subdata = self.http.request("get", self.url, cookies=self.options.cookies)
|
2014-08-31 01:20:36 +02:00
|
|
|
|
|
|
|
data = None
|
|
|
|
if self.subtype == "tt":
|
|
|
|
data = self.tt(subdata)
|
|
|
|
if self.subtype == "json":
|
|
|
|
data = self.json(subdata)
|
|
|
|
if self.subtype == "sami":
|
|
|
|
data = self.sami(subdata)
|
|
|
|
if self.subtype == "smi":
|
|
|
|
data = self.smi(subdata)
|
|
|
|
if self.subtype == "wrst":
|
|
|
|
data = self.wrst(subdata)
|
|
|
|
|
2015-08-31 22:46:45 +02:00
|
|
|
file_d = output(self.options, "srt", mode="wt")
|
2014-12-30 21:20:03 +01:00
|
|
|
if hasattr(file_d, "read") is False:
|
|
|
|
return
|
|
|
|
file_d.write(data)
|
|
|
|
file_d.close()
|
2014-08-31 01:20:36 +02:00
|
|
|
|
|
|
|
def tt(self, subdata):
|
2014-04-21 19:52:09 +02:00
|
|
|
i = 1
|
|
|
|
data = ""
|
2015-10-10 16:31:42 +02:00
|
|
|
if is_py3:
|
|
|
|
subs = subdata.text
|
|
|
|
else:
|
|
|
|
subs = subdata.text.encode("utf8")
|
|
|
|
subdata = re.sub(' xmlns="[^"]+"', '', subs, count=1)
|
|
|
|
tree = ET.XML(subdata)
|
|
|
|
xml = tree.find("body").find("div")
|
|
|
|
plist = list(xml.findall("p"))
|
2014-07-09 18:39:18 +02:00
|
|
|
for node in plist:
|
2014-04-21 19:52:09 +02:00
|
|
|
tag = norm(node.tag)
|
2014-07-09 18:39:18 +02:00
|
|
|
if tag == "p" or tag == "span":
|
2014-04-21 19:52:09 +02:00
|
|
|
begin = node.attrib["begin"]
|
|
|
|
if not ("dur" in node.attrib):
|
|
|
|
duration = node.attrib["duration"]
|
|
|
|
else:
|
|
|
|
duration = node.attrib["dur"]
|
|
|
|
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)
|
|
|
|
else:
|
|
|
|
end = node.attrib["end"]
|
2014-12-26 02:04:29 +01:00
|
|
|
data += '%s\n%s --> %s\n' % (i, begin.replace(".", ","), end.replace(".", ","))
|
2014-07-09 18:39:18 +02:00
|
|
|
data = tt_text(node, data)
|
|
|
|
data += "\n"
|
2014-04-21 19:52:09 +02:00
|
|
|
i += 1
|
2015-08-31 23:19:01 +02:00
|
|
|
if is_py2:
|
|
|
|
data = data.encode("utf8")
|
2014-08-31 01:20:36 +02:00
|
|
|
return data
|
2014-04-21 19:52:09 +02:00
|
|
|
|
2014-08-31 01:20:36 +02:00
|
|
|
def json(self, subdata):
|
2015-09-20 15:15:50 +02:00
|
|
|
data = json.loads(subdata.text)
|
2014-04-21 19:52:09 +02:00
|
|
|
number = 1
|
|
|
|
subs = ""
|
|
|
|
for i in data:
|
|
|
|
subs += "%s\n%s --> %s\n" % (number, timestr(int(i["startMillis"])), timestr(int(i["endMillis"])))
|
2014-11-25 19:02:50 +01:00
|
|
|
if is_py2:
|
|
|
|
subs += "%s\n\n" % i["text"].encode("utf-8")
|
|
|
|
else:
|
|
|
|
subs += "%s\n\n" % i["text"]
|
2014-04-21 19:52:09 +02:00
|
|
|
number += 1
|
|
|
|
|
2014-08-31 01:20:36 +02:00
|
|
|
return subs
|
2014-04-21 19:52:09 +02:00
|
|
|
|
2014-08-31 01:20:36 +02:00
|
|
|
def sami(self, subdata):
|
2015-09-20 15:15:50 +02:00
|
|
|
tree = ET.XML(subdata.text.encode("utf8"))
|
2014-04-21 19:52:09 +02:00
|
|
|
subt = tree.find("Font")
|
|
|
|
subs = ""
|
|
|
|
n = 0
|
|
|
|
for i in subt.getiterator():
|
|
|
|
if i.tag == "Subtitle":
|
|
|
|
n = i.attrib["SpotNumber"]
|
2014-12-15 22:19:58 +01:00
|
|
|
|
2014-04-21 19:52:09 +02:00
|
|
|
if i.attrib["SpotNumber"] == "1":
|
2014-12-15 22:19:58 +01:00
|
|
|
subs += "%s\n%s --> %s\n" % (i.attrib["SpotNumber"], timecolon(i.attrib["TimeIn"]), timecolon(i.attrib["TimeOut"]))
|
2014-04-21 19:52:09 +02:00
|
|
|
else:
|
2014-12-15 22:19:58 +01:00
|
|
|
subs += "\n%s\n%s --> %s\n" % (i.attrib["SpotNumber"], timecolon(i.attrib["TimeIn"]), timecolon(i.attrib["TimeOut"]))
|
2014-04-21 19:52:09 +02:00
|
|
|
else:
|
|
|
|
if int(n) > 0:
|
|
|
|
subs += "%s\n" % i.text
|
|
|
|
|
2015-08-31 23:19:01 +02:00
|
|
|
if is_py2:
|
|
|
|
subs = subs.encode('utf8')
|
2014-08-31 01:20:36 +02:00
|
|
|
return subs
|
2014-04-21 19:52:09 +02:00
|
|
|
|
2014-08-31 01:20:36 +02:00
|
|
|
def smi(self, subdata):
|
2015-09-20 15:15:50 +02:00
|
|
|
if requests_version < 0x20300:
|
|
|
|
subdata = subdata.content
|
|
|
|
if is_py3:
|
|
|
|
subdata = subdata.decode("latin")
|
|
|
|
else:
|
|
|
|
subdata = subdata.text
|
2015-04-28 23:00:24 +02:00
|
|
|
ssubdata = StringIO(subdata)
|
|
|
|
timea = 0
|
2014-04-21 19:52:09 +02:00
|
|
|
number = 1
|
2015-04-28 23:00:24 +02:00
|
|
|
data = None
|
2014-04-21 19:52:09 +02:00
|
|
|
subs = ""
|
2014-11-23 13:02:14 +01:00
|
|
|
TAG_RE = re.compile(r'<[^>]+>')
|
|
|
|
bad_char = re.compile(r'\x96')
|
2015-04-28 23:00:24 +02:00
|
|
|
for i in ssubdata.readlines():
|
2015-05-01 22:34:02 +02:00
|
|
|
i = i.rstrip()
|
2015-04-28 23:16:44 +02:00
|
|
|
sync = re.search(r"<SYNC Start=(\d+)>", i)
|
2015-04-28 23:00:24 +02:00
|
|
|
if sync:
|
|
|
|
if int(sync.group(1)) != int(timea):
|
2015-05-01 22:34:02 +02:00
|
|
|
if data and data != " ":
|
2015-04-28 23:00:24 +02:00
|
|
|
subs += "%s\n%s --> %s\n" % (number, timestr(timea), timestr(sync.group(1)))
|
|
|
|
text = "%s\n" % TAG_RE.sub('', data.replace("<br>", "\n"))
|
2015-05-01 22:34:02 +02:00
|
|
|
if text[len(text)-2] != "\n":
|
|
|
|
text += "\n"
|
2015-04-28 23:00:24 +02:00
|
|
|
subs += text
|
|
|
|
number += 1
|
|
|
|
timea = sync.group(1)
|
|
|
|
text = re.search("<P Class=SVCC>(.*)", i)
|
|
|
|
if text:
|
|
|
|
data = text.group(1)
|
2014-11-23 13:02:14 +01:00
|
|
|
recomp = re.compile(r'\r')
|
|
|
|
text = bad_char.sub('-', recomp.sub('', subs)).replace('"', '"')
|
2015-09-20 16:03:07 +02:00
|
|
|
if is_py2 and isinstance(text, unicode):
|
|
|
|
return text.encode("utf-8")
|
2014-08-31 01:20:36 +02:00
|
|
|
return text
|
2014-04-21 19:52:09 +02:00
|
|
|
|
2014-08-31 01:20:36 +02:00
|
|
|
def wrst(self, subdata):
|
2015-09-20 15:15:50 +02:00
|
|
|
ssubdata = StringIO(subdata.text)
|
2014-04-21 19:52:09 +02:00
|
|
|
srt = ""
|
2014-06-07 18:50:51 +02:00
|
|
|
subtract = False
|
2015-03-01 21:44:55 +01:00
|
|
|
number_b = 1
|
|
|
|
number = 0
|
|
|
|
block = 0
|
|
|
|
subnr = False
|
|
|
|
for i in ssubdata.readlines():
|
2015-04-28 23:16:44 +02:00
|
|
|
match = re.search(r"^[\r\n]+", i)
|
|
|
|
match2 = re.search(r"([\d:\.]+ --> [\d:\.]+)", i)
|
|
|
|
match3 = re.search(r"^(\d+)\s", i)
|
2015-03-01 21:44:55 +01:00
|
|
|
if i[:6] == "WEBVTT":
|
|
|
|
pass
|
|
|
|
elif match and number_b > 1:
|
|
|
|
block = 0
|
|
|
|
srt += "\n"
|
|
|
|
elif match2:
|
|
|
|
if not subnr:
|
|
|
|
srt += "%s\n" % number_b
|
|
|
|
matchx = re.search(r'(\d+):(\d+)[.:]([\d\.]+) --> (\d+):(\d+)[.:]([\d\.]+)', i)
|
|
|
|
hour1 = int(matchx.group(1))
|
|
|
|
hour2 = int(matchx.group(4))
|
2015-06-07 20:10:36 +02:00
|
|
|
if int(number) == 1:
|
2015-03-01 21:44:55 +01:00
|
|
|
if hour1 > 9:
|
|
|
|
subtract = True
|
|
|
|
if subtract:
|
|
|
|
hour1 -= 10
|
|
|
|
hour2 -= 10
|
|
|
|
time = "%s:%s:%s --> %s:%s:%s\n" % (hour1, matchx.group(2), matchx.group(3).replace(".", ","), hour2, matchx.group(5), matchx.group(6).replace(".", ","))
|
|
|
|
srt += time
|
|
|
|
block = 1
|
|
|
|
subnr = False
|
|
|
|
number_b += 1
|
|
|
|
|
|
|
|
elif match3 and block == 0:
|
|
|
|
number = match3.group(1)
|
|
|
|
srt += "%s\n" % number
|
|
|
|
subnr = True
|
|
|
|
else:
|
|
|
|
sub = re.sub('<[^>]*>', '', i)
|
|
|
|
srt += sub.lstrip()
|
2015-09-01 22:54:16 +02:00
|
|
|
srt = decode_html_entities(srt)
|
2015-08-31 23:19:01 +02:00
|
|
|
if is_py2:
|
2015-04-28 22:59:07 +02:00
|
|
|
return srt.encode("utf-8")
|
|
|
|
return srt
|
2014-04-27 15:33:05 +02:00
|
|
|
|
2015-09-15 20:10:32 +02:00
|
|
|
|
2014-04-21 19:52:09 +02: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)
|
|
|
|
return output.replace(".", ",")
|
|
|
|
|
2015-09-15 20:10:32 +02:00
|
|
|
|
2014-12-15 22:19:58 +01:00
|
|
|
def timecolon(data):
|
2014-12-22 10:20:37 +01:00
|
|
|
match = re.search(r"(\d+:\d+:\d+):(\d+)", data)
|
2014-12-15 22:19:58 +01:00
|
|
|
return "%s,%s" % (match.group(1), match.group(2))
|
|
|
|
|
2015-09-15 20:10:32 +02:00
|
|
|
|
2014-04-21 19:52:09 +02:00
|
|
|
def norm(name):
|
|
|
|
if name[0] == "{":
|
|
|
|
_, tag = name[1:].split("}")
|
|
|
|
return tag
|
|
|
|
else:
|
|
|
|
return name
|
2014-07-09 18:39:18 +02:00
|
|
|
|
2015-09-15 20:10:32 +02:00
|
|
|
|
2014-07-09 18:39:18 +02:00
|
|
|
def tt_text(node, data):
|
|
|
|
if node.text:
|
|
|
|
data += "%s\n" % node.text.strip(' \t\n\r')
|
|
|
|
for i in node:
|
|
|
|
if i.text:
|
|
|
|
data += "%s\n" % i.text.strip(' \t\n\r')
|
|
|
|
if i.tail:
|
|
|
|
text = i.tail.strip(' \t\n\r')
|
|
|
|
if text:
|
|
|
|
data += "%s\n" % text
|
2015-01-05 21:52:34 +01:00
|
|
|
return data
|