From 9804d80302b94752a9ff966f63944bbc114cd48e Mon Sep 17 00:00:00 2001 From: Anders Waldenborg Date: Sat, 11 Jan 2014 23:02:47 +0100 Subject: [PATCH] Split subtitle getting to separate method --- lib/svtplay_dl/__init__.py | 4 ++++ lib/svtplay_dl/service/__init__.py | 3 +++ lib/svtplay_dl/service/kanal5.py | 25 +++++++++++++++---------- lib/svtplay_dl/service/nrk.py | 17 ++++++++++------- lib/svtplay_dl/service/svtplay.py | 27 ++++++++++++++++++--------- lib/svtplay_dl/service/tv4play.py | 16 +++++++++++----- lib/svtplay_dl/service/urplay.py | 16 +++++++++++----- lib/svtplay_dl/service/viaplay.py | 15 ++++++++++----- 8 files changed, 82 insertions(+), 41 deletions(-) diff --git a/lib/svtplay_dl/__init__.py b/lib/svtplay_dl/__init__.py index 20aaa5a..1bb7b13 100644 --- a/lib/svtplay_dl/__init__.py +++ b/lib/svtplay_dl/__init__.py @@ -71,6 +71,10 @@ def get_media(url, options): os.path.join(options.output, filenamify(title_tag)) stream.get(options) + if options.subtitle: + if options.output != "-": + stream.get_subtitle(options) + def setup_log(silent, verbose=False): fmt = logging.Formatter('%(levelname)s %(message)s') diff --git a/lib/svtplay_dl/service/__init__.py b/lib/svtplay_dl/service/__init__.py index 647ef4b..3e27b97 100644 --- a/lib/svtplay_dl/service/__init__.py +++ b/lib/svtplay_dl/service/__init__.py @@ -31,6 +31,9 @@ class Service(object): return False + def get_subtitle(self, options): + pass + from svtplay_dl.service.aftonbladet import Aftonbladet from svtplay_dl.service.dr import Dr from svtplay_dl.service.expressen import Expressen diff --git a/lib/svtplay_dl/service/kanal5.py b/lib/svtplay_dl/service/kanal5.py index 907c800..d967747 100644 --- a/lib/svtplay_dl/service/kanal5.py +++ b/lib/svtplay_dl/service/kanal5.py @@ -15,8 +15,12 @@ from svtplay_dl.fetcher.hls import download_hls class Kanal5(Service): supported_domains = ['kanal5play.se', 'kanal9play.se'] + def __init__(self, url): + Service.__init__(self, url) + self.cj = CookieJar() + self.subtitle = None + def get(self, options): - cj = CookieJar() match = re.search(r".*video/([0-9]+)", self.url) if not match: log.error("Can't find video file") @@ -26,9 +30,9 @@ class Kanal5(Service): if options.username and options.password: #bogus cc = Cookie(None, 'asdf', None, '80', '80', 'www.kanal5play.se', None, None, '/', None, False, False, 'TestCookie', None, None, None) - cj.set_cookie(cc) + self.cj.set_cookie(cc) #get session cookie - data = get_http_data("http://www.kanal5play.se/", cookiejar=cj) + data = get_http_data("http://www.kanal5play.se/", cookiejar=self.cj) authurl = "https://kanal5swe.appspot.com/api/user/login?callback=jQuery171029989&email=%s&password=%s&_=136250" % (options.username, options.password) data = get_http_data(authurl) match = re.search(r"({.*})\);", data) @@ -46,16 +50,16 @@ class Kanal5(Service): path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}) - cj.set_cookie(cc) + self.cj.set_cookie(cc) format_ = "FLASH" if options.hls: format_ = "IPHONE" url = "http://www.kanal5play.se/api/getVideo?format=%s&videoId=%s" % (format_, video_id) - data = json.loads(get_http_data(url, cookiejar=cj)) + data = json.loads(get_http_data(url, cookiejar=self.cj)) options.live = data["isLive"] if data["hasSubtitle"]: - subtitle = "http://www.kanal5play.se/api/subtitles/%s" % video_id + self.subtitle = "http://www.kanal5play.se/api/subtitles/%s" % video_id if options.hls: url = data["streams"][0]["source"] baseurl = url[0:url.rfind("/")] @@ -78,7 +82,8 @@ class Kanal5(Service): match = re.search(r"^(.*):", filename) options.other = "-W %s -y %s " % ("http://www.kanal5play.se/flash/K5StandardPlayer.swf", filename) download_rtmp(options, steambaseurl) - if options.subtitle: - if options.output != "-": - data = get_http_data(subtitle, cookiejar=cj) - subtitle_json(options, data) + + def get_subtitle(self, options): + if self.subtitle: + data = get_http_data(self.subtitle, cookiejar=self.cj) + subtitle_json(options, data) diff --git a/lib/svtplay_dl/service/nrk.py b/lib/svtplay_dl/service/nrk.py index 8f08348..a2241ff 100644 --- a/lib/svtplay_dl/service/nrk.py +++ b/lib/svtplay_dl/service/nrk.py @@ -22,11 +22,14 @@ class Nrk(Service): else: manifest_url = "%s?hdcore=2.8.0&g=hejsan" % manifest_url download_hds(options, manifest_url) - if options.subtitle: - match = re.search("data-subtitlesurl = \"(/.*)\"", data) - if match: - parse = urlparse(self.url) - subtitle = "%s://%s%s" % (parse.scheme, parse.netloc, match.group(1)) - data = get_http_data(subtitle) - subtitle_tt(options, data) + + + def get_subtitle(self, options): + data = get_http_data(self.url) + match = re.search("data-subtitlesurl = \"(/.*)\"", data) + if match: + parse = urlparse(self.url) + subtitle = "%s://%s%s" % (parse.scheme, parse.netloc, match.group(1)) + data = get_http_data(subtitle) + subtitle_tt(options, data) diff --git a/lib/svtplay_dl/service/svtplay.py b/lib/svtplay_dl/service/svtplay.py index 4547cdf..7b367c7 100644 --- a/lib/svtplay_dl/service/svtplay.py +++ b/lib/svtplay_dl/service/svtplay.py @@ -18,6 +18,10 @@ from svtplay_dl.log import log class Svtplay(Service): supported_domains = ['svtplay.se', 'svt.se', 'oppetarkiv.se', 'beta.svtplay.se'] + def __init__(self, url): + Service.__init__(self, url) + self.subtitle = None + def get(self, options): if re.findall("svt.se", self.url): data = get_http_data(self.url) @@ -41,6 +45,13 @@ class Svtplay(Service): options.live = data["video"]["live"] else: options.live = False + + try: + self.subtitle = data["video"]["subtitleReferences"][0]["url"] + except KeyError: + pass + + streams = {} streams2 = {} #hack.. for i in data["video"]["videoReferences"]: @@ -95,12 +106,10 @@ class Svtplay(Service): download_hds(options, manifest) else: download_http(options, test["url"]) - if options.subtitle: - try: - subtitle = data["video"]["subtitleReferences"][0]["url"] - except KeyError: - sys.exit(1) - if len(subtitle) > 0: - if options.output != "-": - data = get_http_data(subtitle) - subtitle_wsrt(options, data) + + + def get_subtitle(self, options): + if self.subtitle: + if options.output != "-": + data = get_http_data(self.subtitle) + subtitle_wsrt(options, data) diff --git a/lib/svtplay_dl/service/tv4play.py b/lib/svtplay_dl/service/tv4play.py index e37b0f2..9d79693 100644 --- a/lib/svtplay_dl/service/tv4play.py +++ b/lib/svtplay_dl/service/tv4play.py @@ -15,6 +15,10 @@ from svtplay_dl.fetcher.hds import download_hds class Tv4play(Service): supported_domains = ['tv4play.se', 'tv4.se'] + def __init__(self, url): + Service.__init__(self, url) + self.subtitle = None + def get(self, options): parse = urlparse(self.url) if "tv4play.se" in self.url: @@ -50,7 +54,6 @@ class Tv4play(Service): options.live = True streams = {} - subtitle = False for i in sa: if i.find("mediaFormat").text == "mp4": @@ -59,7 +62,8 @@ class Tv4play(Service): stream["path"] = i.find("url").text streams[int(i.find("bitrate").text)] = stream elif i.find("mediaFormat").text == "smi": - subtitle = i.find("url").text + self.subtitle = i.find("url").text + if len(streams) == 0: log.error("Can't find any streams") sys.exit(2) @@ -81,7 +85,9 @@ class Tv4play(Service): sys.exit(2) manifest = "%s?hdcore=2.8.0&g=hejsan" % test["path"] download_hds(options, manifest) - if options.subtitle and subtitle: - data = get_http_data(subtitle) - subtitle_smi(options, data) + + def get_subtitle(self, options): + if self.subtitle: + data = get_http_data(self.subtitle) + subtitle_smi(options, data) diff --git a/lib/svtplay_dl/service/urplay.py b/lib/svtplay_dl/service/urplay.py index aa07d4d..02008cb 100644 --- a/lib/svtplay_dl/service/urplay.py +++ b/lib/svtplay_dl/service/urplay.py @@ -14,6 +14,10 @@ from svtplay_dl.log import log class Urplay(Service): supported_domains = ['urplay.se', 'ur.se'] + def __init__(self, url): + Service.__init__(self, url) + self.subtitle = None + def get(self, options): data = get_http_data(self.url) match = re.search(r"urPlayer.init\((.*)\);", data) @@ -22,7 +26,7 @@ class Urplay(Service): sys.exit(2) data = match.group(1) jsondata = json.loads(data) - subtitle = jsondata["subtitles"].split(",")[0] + self.subtitle = jsondata["subtitles"].split(",")[0] basedomain = jsondata["streaming_config"]["streamer"]["redirect"] http = "http://%s/%s" % (basedomain, jsondata["file_html5"]) hd = None @@ -58,10 +62,6 @@ class Urplay(Service): download_hls(options, selected["hls"]["playlist"], selected["hls"]["http"]) else: download_rtmp(options, selected["rtmp"]["server"]) - if options.subtitle: - if options.output != "-": - data = get_http_data(subtitle) - subtitle_tt(options, data) def select_highest_quality(self, available): if 'hd' in available: @@ -70,3 +70,9 @@ class Urplay(Service): return available["sd"] else: raise KeyError() + + + def get_subtitle(self, options): + if self.subtitle: + data = get_http_data(self.subtitle) + subtitle_tt(options, data) diff --git a/lib/svtplay_dl/service/viaplay.py b/lib/svtplay_dl/service/viaplay.py index c101da6..22ea679 100644 --- a/lib/svtplay_dl/service/viaplay.py +++ b/lib/svtplay_dl/service/viaplay.py @@ -21,6 +21,10 @@ class Viaplay(Service): 'tv3play.no', 'tv3play.dk', 'tv6play.no', 'viasat4play.no', 'tv3play.ee', 'tv3play.lv', 'tv3play.lt'] + def __init__(self, url): + Service.__init__(self, url) + self.subtitle = None + def get(self, options): parse = urlparse(self.url) match = re.search(r'\/play\/(\d+)/?', parse.path) @@ -38,7 +42,7 @@ class Viaplay(Service): options.live = True filename = xml.find("Product").find("Videos").find("Video").find("Url").text - subtitle = xml.find("Product").find("SamiFile").text + self.subtitle = xml.find("Product").find("SamiFile").text if filename[:4] == "http": data = get_http_data(filename) @@ -54,7 +58,8 @@ class Viaplay(Service): path = "-y %s" % match.group(2) options.other = "-W http://flvplayer.viastream.viasat.tv/flvplayer/play/swf/player.swf %s" % path download_rtmp(options, filename) - if options.subtitle and subtitle: - if options.output != "-": - data = get_http_data(subtitle) - subtitle_sami(options, data) + + def get_subtitle(self, options): + if self.subtitle: + data = get_http_data(self.subtitle) + subtitle_sami(options, data)