mirror of
https://github.com/spaam/svtplay-dl.git
synced 2024-11-28 06:04:17 +01:00
subtitle: refactor so we can reuse the try-except-thing
This commit is contained in:
parent
524fe2ce0c
commit
dcc1367250
@ -129,7 +129,7 @@ def get_one_media(stream, options):
|
||||
|
||||
if options.subtitle and options.output != "-":
|
||||
if subs:
|
||||
subs[0].download(copy.copy(options))
|
||||
subs[0].download()
|
||||
if options.force_subtitle:
|
||||
return
|
||||
|
||||
|
@ -12,7 +12,7 @@ from svtplay_dl.utils import get_http_data, filenamify
|
||||
from svtplay_dl.log import log
|
||||
from svtplay_dl.fetcher.rtmp import RTMP
|
||||
from svtplay_dl.fetcher.hls import HLS, hlsparse
|
||||
from svtplay_dl.subtitle import subtitle_json
|
||||
from svtplay_dl.subtitle import subtitle
|
||||
|
||||
class Kanal5(Service):
|
||||
supported_domains = ['kanal5play.se', 'kanal9play.se', 'kanal11play.se']
|
||||
@ -58,7 +58,7 @@ class Kanal5(Service):
|
||||
if not options.live:
|
||||
options.live = data["isLive"]
|
||||
if data["hasSubtitle"]:
|
||||
yield subtitle_json("http://www.kanal5play.se/api/subtitles/%s" % video_id)
|
||||
yield subtitle(copy.copy(options), "json", "http://www.kanal5play.se/api/subtitles/%s" % video_id)
|
||||
|
||||
if options.output_auto:
|
||||
directory = os.path.dirname(options.output)
|
||||
|
@ -11,7 +11,7 @@ from svtplay_dl.utils import get_http_data
|
||||
from svtplay_dl.utils.urllib import urlparse
|
||||
from svtplay_dl.fetcher.hds import hdsparse
|
||||
from svtplay_dl.fetcher.hls import HLS, hlsparse
|
||||
from svtplay_dl.subtitle import subtitle_tt
|
||||
from svtplay_dl.subtitle import subtitle
|
||||
from svtplay_dl.log import log
|
||||
|
||||
class Nrk(Service, OpenGraphThumbMixin):
|
||||
@ -22,8 +22,8 @@ class Nrk(Service, OpenGraphThumbMixin):
|
||||
match = re.search("data-subtitlesurl = \"(/.*)\"", data)
|
||||
if match:
|
||||
parse = urlparse(self.url)
|
||||
subtitle = "%s://%s%s" % (parse.scheme, parse.netloc, match.group(1))
|
||||
yield subtitle_tt(subtitle)
|
||||
suburl = "%s://%s%s" % (parse.scheme, parse.netloc, match.group(1))
|
||||
yield subtitle(copy.copy(options), "tt", suburl)
|
||||
|
||||
if options.force_subtitle:
|
||||
return
|
||||
|
@ -14,7 +14,7 @@ from svtplay_dl.fetcher.hds import hdsparse
|
||||
from svtplay_dl.fetcher.hls import HLS, hlsparse
|
||||
from svtplay_dl.fetcher.rtmp import RTMP
|
||||
from svtplay_dl.fetcher.http import HTTP
|
||||
from svtplay_dl.subtitle import subtitle_wsrt
|
||||
from svtplay_dl.subtitle import subtitle
|
||||
from svtplay_dl.log import log
|
||||
|
||||
class Svtplay(Service, OpenGraphThumbMixin):
|
||||
@ -50,11 +50,11 @@ class Svtplay(Service, OpenGraphThumbMixin):
|
||||
if data["video"]["subtitleReferences"]:
|
||||
subtitle = None
|
||||
try:
|
||||
subtitle = data["video"]["subtitleReferences"][0]["url"]
|
||||
suburl = data["video"]["subtitleReferences"][0]["url"]
|
||||
except KeyError:
|
||||
pass
|
||||
if subtitle and len(subtitle) > 0:
|
||||
yield subtitle_wsrt(subtitle)
|
||||
yield subtitle(copy.copy(options), "wrst", suburl)
|
||||
|
||||
if options.output_auto:
|
||||
directory = os.path.dirname(options.output)
|
||||
|
@ -15,7 +15,7 @@ from svtplay_dl.log import log
|
||||
from svtplay_dl.fetcher.hls import hlsparse, HLS
|
||||
from svtplay_dl.fetcher.rtmp import RTMP
|
||||
from svtplay_dl.fetcher.hds import hdsparse
|
||||
from svtplay_dl.subtitle import subtitle_smi
|
||||
from svtplay_dl.subtitle import subtitle
|
||||
|
||||
class Tv4play(Service, OpenGraphThumbMixin):
|
||||
supported_domains = ['tv4play.se', 'tv4.se']
|
||||
@ -88,7 +88,7 @@ class Tv4play(Service, OpenGraphThumbMixin):
|
||||
for n in list(streams.keys()):
|
||||
yield streams[n]
|
||||
elif i.find("mediaFormat").text == "smi":
|
||||
yield subtitle_smi(i.find("url").text)
|
||||
yield subtitle(copy.copy(options), "smi", i.find("url").text)
|
||||
|
||||
url = "http://premium.tv4play.se/api/web/asset/%s/play?protocol=hls" % vid
|
||||
data = get_http_data(url)
|
||||
|
@ -12,7 +12,7 @@ from svtplay_dl.utils import get_http_data
|
||||
from svtplay_dl.fetcher.rtmp import RTMP
|
||||
from svtplay_dl.fetcher.hls import HLS, hlsparse
|
||||
from svtplay_dl.log import log
|
||||
from svtplay_dl.subtitle import subtitle_tt
|
||||
from svtplay_dl.subtitle import subtitle
|
||||
|
||||
class Urplay(Service, OpenGraphThumbMixin):
|
||||
supported_domains = ['urplay.se', 'ur.se']
|
||||
@ -28,7 +28,7 @@ class Urplay(Service, OpenGraphThumbMixin):
|
||||
sys.exit(2)
|
||||
data = match.group(1)
|
||||
jsondata = json.loads(data)
|
||||
yield subtitle_tt(jsondata["subtitles"].split(",")[0])
|
||||
yield subtitle(copy.copy(options), "tt", jsondata["subtitles"].split(",")[0])
|
||||
basedomain = jsondata["streaming_config"]["streamer"]["redirect"]
|
||||
http = "http://%s/%s" % (basedomain, jsondata["file_html5"])
|
||||
hd = None
|
||||
|
@ -17,7 +17,7 @@ from svtplay_dl.log import log
|
||||
from svtplay_dl.fetcher.rtmp import RTMP
|
||||
from svtplay_dl.fetcher.hds import hdsparse
|
||||
from svtplay_dl.fetcher.hls import HLS, hlsparse
|
||||
from svtplay_dl.subtitle import subtitle_sami
|
||||
from svtplay_dl.subtitle import subtitle
|
||||
|
||||
class Viaplay(Service, OpenGraphThumbMixin):
|
||||
supported_domains = [
|
||||
@ -70,7 +70,7 @@ class Viaplay(Service, OpenGraphThumbMixin):
|
||||
options.live = True
|
||||
|
||||
if dataj["sami_path"]:
|
||||
yield subtitle_sami(dataj["sami_path"])
|
||||
yield subtitle(copy.copy(options), "sami", dataj["sami_path"])
|
||||
|
||||
streams = get_http_data("http://playapi.mtgx.tv/v3/videos/stream/%s" % vid)
|
||||
streamj = json.loads(streams)
|
||||
|
@ -6,9 +6,11 @@ from svtplay_dl.log import log
|
||||
from svtplay_dl.utils import is_py2, is_py3, get_http_data, HTTPError
|
||||
|
||||
class subtitle(object):
|
||||
def __init__(self, url):
|
||||
def __init__(self, options, subtype, url):
|
||||
self.url = url
|
||||
self.subtitle = None
|
||||
self.options = options
|
||||
self.subtype = subtype
|
||||
|
||||
def get_subdata(self):
|
||||
if self.subtitle is None:
|
||||
@ -18,12 +20,27 @@ class subtitle(object):
|
||||
return None
|
||||
return self.subtitle
|
||||
|
||||
class subtitle_tt(subtitle):
|
||||
def download(self, options):
|
||||
def download(self):
|
||||
subdata = self.get_subdata()
|
||||
if subdata is None:
|
||||
log.error("Can't download subtitle.")
|
||||
return
|
||||
|
||||
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)
|
||||
|
||||
save(self.options, data)
|
||||
|
||||
def tt(self, subdata):
|
||||
i = 1
|
||||
data = ""
|
||||
tree = ET.ElementTree(ET.fromstring(subdata))
|
||||
@ -51,14 +68,9 @@ class subtitle_tt(subtitle):
|
||||
|
||||
if is_py2:
|
||||
data = data.encode('utf8')
|
||||
save(options, data)
|
||||
return data
|
||||
|
||||
class subtitle_json(subtitle):
|
||||
def download(self, options):
|
||||
subdata = self.get_subdata()
|
||||
if subdata is None:
|
||||
log.error("Can't download subtitle.")
|
||||
return
|
||||
def json(self, subdata):
|
||||
data = json.loads(subdata)
|
||||
number = 1
|
||||
subs = ""
|
||||
@ -70,14 +82,9 @@ class subtitle_json(subtitle):
|
||||
subs += "%s\n\n" % i["text"]
|
||||
number += 1
|
||||
|
||||
save(options, subs)
|
||||
return subs
|
||||
|
||||
class subtitle_sami(subtitle):
|
||||
def download(self, options):
|
||||
subdata = self.get_subdata()
|
||||
if subdata is None:
|
||||
log.error("Can't download subtitle.")
|
||||
return
|
||||
def sami(self, subdata):
|
||||
tree = ET.XML(subdata)
|
||||
subt = tree.find("Font")
|
||||
subs = ""
|
||||
@ -95,22 +102,17 @@ class subtitle_sami(subtitle):
|
||||
|
||||
if is_py2:
|
||||
subs = subs.encode('utf8')
|
||||
save(options, subs)
|
||||
return subs
|
||||
|
||||
class subtitle_smi(subtitle):
|
||||
def download(self, options):
|
||||
subdata = self.get_subdata()
|
||||
if subdata is None:
|
||||
log.error("Can't download subtitle.")
|
||||
return
|
||||
def smi(self, subdata):
|
||||
if is_py3:
|
||||
self.subtitle = self.subtitle.decode("latin1")
|
||||
subdata = subdata.decode("latin1")
|
||||
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 = ""
|
||||
TAG_RE = re.compile(r'<[^>]+>')
|
||||
bad_char = re.compile(r'\x96')
|
||||
for i in recomp.finditer(self.subtitle):
|
||||
for i in recomp.finditer(subdata):
|
||||
subs += "%s\n%s --> %s\n" % (number, timestr(i.group(1)), timestr(i.group(6)))
|
||||
text = "%s\n\n" % TAG_RE.sub('', i.group(3).replace("<br>", "\n"))
|
||||
if text[0] == "\x0a":
|
||||
@ -119,14 +121,9 @@ class subtitle_smi(subtitle):
|
||||
number += 1
|
||||
recomp = re.compile(r'\r')
|
||||
text = bad_char.sub('-', recomp.sub('', subs)).replace('"', '"')
|
||||
save(options, text)
|
||||
return text
|
||||
|
||||
class subtitle_wsrt(subtitle):
|
||||
def download(self, options):
|
||||
subdata = self.get_subdata()
|
||||
if subdata is None:
|
||||
log.error("Can't download subtitle.")
|
||||
return
|
||||
def wrst(self, subdata):
|
||||
recomp = re.compile(r"(\d+)\r\n([\d:\.]+ --> [\d:\.]+)?([^\r\n]+)?\r\n([^\r\n]+)\r\n(([^\r\n]*)\r\n)?")
|
||||
srt = ""
|
||||
subtract = False
|
||||
@ -149,7 +146,7 @@ class subtitle_wsrt(subtitle):
|
||||
sub = re.sub('<[^>]*>', '', sub)
|
||||
srt += sub
|
||||
|
||||
save(options, srt)
|
||||
return srt
|
||||
|
||||
def save(options, data):
|
||||
filename = re.search(r"(.*)\.[a-z0-9]{2,3}$", options.output)
|
||||
|
Loading…
Reference in New Issue
Block a user