1
0
mirror of https://github.com/spaam/svtplay-dl.git synced 2024-12-01 07:34:16 +01:00
svtplay-dl/lib/svtplay_dl/service/svtplay.py

117 lines
4.5 KiB
Python
Raw Normal View History

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 -*-
from __future__ import absolute_import
import sys
import re
import json
import xml.etree.ElementTree as ET
from svtplay_dl.service import Service, OpenGraphThumbMixin
2014-04-21 19:53:18 +02:00
from svtplay_dl.utils import get_http_data
from svtplay_dl.utils.urllib import urlparse
2014-04-27 13:19:34 +02:00
from svtplay_dl.fetcher.hds import HDS, hdsparse
2014-04-21 21:55:39 +02:00
from svtplay_dl.fetcher.hls import HLS, hlsparse
from svtplay_dl.fetcher.rtmp import RTMP
from svtplay_dl.fetcher.http import HTTP
2014-04-21 19:53:18 +02:00
from svtplay_dl.subtitle import subtitle_wsrt
from svtplay_dl.log import log
class Svtplay(Service, OpenGraphThumbMixin):
2014-04-27 09:04:56 +02:00
supported_domains = ['svtplay.se', 'svt.se', 'oppetarkiv.se', 'beta.svtplay.se', 'svtflow.se']
def __init__(self, url):
Service.__init__(self, url)
self.subtitle = None
2014-01-06 23:14:06 +01:00
def get(self, options):
if re.findall("svt.se", self.url):
match = re.search(r"data-json-href=\"(.*)\"", self.get_urldata())
if match:
filename = match.group(1).replace("&", "&").replace("&format=json", "")
url = "http://www.svt.se%s" % filename
else:
log.error("Can't find video file")
sys.exit(2)
2014-01-06 23:14:06 +01:00
else:
url = self.url
pos = url.find("?")
if pos < 0:
dataurl = "%s?&output=json&format=json" % url
else:
dataurl = "%s&output=json&format=json" % url
data = json.loads(get_http_data(dataurl))
2013-04-16 13:18:40 +02:00
if "live" in data["video"]:
options.live = data["video"]["live"]
else:
options.live = False
if data["video"]["subtitleReferences"]:
try:
subtitle = data["video"]["subtitleReferences"][0]["url"]
except KeyError:
pass
if len(subtitle) > 0:
yield subtitle_wsrt(subtitle)
for i in data["video"]["videoReferences"]:
parse = urlparse(i["url"])
if parse.path.find("m3u8") > 0:
2014-04-21 21:55:39 +02:00
streams = hlsparse(i["url"])
for n in list(streams.keys()):
yield HLS(options, streams[n], n)
2014-04-21 19:12:21 +02:00
elif parse.path.find("f4m") > 0:
match = re.search(r"\/se\/secure\/", i["url"])
if not match:
manifest = "%s?hdcore=2.8.0&g=hejsan" % i["url"]
2014-04-27 13:19:34 +02:00
streams = hdsparse(options, manifest)
for n in list(streams.keys()):
yield streams[n]
2014-04-21 19:12:21 +02:00
elif parse.scheme == "rtmp":
embedurl = "%s?type=embed" % url
data = get_http_data(embedurl)
match = re.search(r"value=\"(/(public)?(statiskt)?/swf(/video)?/svtplayer-[0-9\.a-f]+swf)\"", data)
swf = "http://www.svtplay.se%s" % match.group(1)
options.other = "-W %s" % swf
yield RTMP(options, i["url"], i["bitrate"])
else:
yield HTTP(options, i["url"], "0")
def find_all_episodes(self, options):
parse = urlparse(self.url)
if parse.netloc == "www.oppetarkiv.se":
page = 1
match = re.search(r'"http://www.oppetarkiv.se/etikett/titel/([^"/]+)', self.get_urldata())
if match is None:
match = re.search(r'"http://www.oppetarkiv.se/etikett/titel/([^"/]+)', self.url)
if match is None:
log.error("Couldn't find title")
sys.exit(2)
program = match.group(1)
more = True
episodes = []
while more:
url = "http://www.oppetarkiv.se/etikett/titel/%s/?sida=%s&sort=tid_stigande&embed=true" % (program, page)
data = get_http_data(url)
visa = re.search(r'svtXColorDarkLightGrey', data)
if not visa:
more = False
regex = re.compile(r'(http://www.oppetarkiv.se/video/[^"]+)')
for match in regex.finditer(data):
episodes.append(match.group(1))
page += 1
return episodes
else:
match = re.search(r'<link rel="alternate" type="application/rss\+xml" [^>]*href="([^"]+)"',
self.get_urldata())
if match is None:
log.error("Couldn't retrieve episode list")
sys.exit(2)
xml = ET.XML(get_http_data(match.group(1)))
return sorted(x.text for x in xml.findall(".//item/link"))