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-02-12 19:43:37 +01:00
|
|
|
import re
|
2014-08-27 22:58:37 +02:00
|
|
|
import os
|
2013-02-12 19:43:37 +01:00
|
|
|
import xml.etree.ElementTree as ET
|
2014-04-03 19:52:51 +02:00
|
|
|
import json
|
2014-06-07 20:43:40 +02:00
|
|
|
import copy
|
2013-02-12 19:43:37 +01:00
|
|
|
|
2015-08-31 21:54:10 +02:00
|
|
|
from svtplay_dl.utils.urllib import urlparse, parse_qs, quote_plus
|
2014-01-19 14:26:48 +01:00
|
|
|
from svtplay_dl.service import Service, OpenGraphThumbMixin
|
2015-08-31 21:54:10 +02:00
|
|
|
from svtplay_dl.utils import is_py2_old, filenamify
|
2013-03-17 19:55:19 +01:00
|
|
|
from svtplay_dl.log import log
|
2015-10-04 14:37:16 +02:00
|
|
|
from svtplay_dl.fetcher.hls import hlsparse
|
2014-04-21 18:24:01 +02:00
|
|
|
from svtplay_dl.fetcher.rtmp import RTMP
|
2014-04-27 13:24:53 +02:00
|
|
|
from svtplay_dl.fetcher.hds import hdsparse
|
2014-08-31 01:20:36 +02:00
|
|
|
from svtplay_dl.subtitle import subtitle
|
2015-09-06 14:19:10 +02:00
|
|
|
from svtplay_dl.error import ServiceError
|
2013-02-12 19:43:37 +01:00
|
|
|
|
2015-09-15 20:10:32 +02:00
|
|
|
|
2014-01-19 14:26:48 +01:00
|
|
|
class Tv4play(Service, OpenGraphThumbMixin):
|
2014-01-01 14:57:17 +01:00
|
|
|
supported_domains = ['tv4play.se', 'tv4.se']
|
2013-01-17 00:21:47 +01:00
|
|
|
|
2014-01-11 23:02:47 +01:00
|
|
|
def __init__(self, url):
|
|
|
|
Service.__init__(self, url)
|
|
|
|
self.subtitle = None
|
2015-08-30 00:06:20 +02:00
|
|
|
self.cookies = {}
|
2014-01-11 23:02:47 +01:00
|
|
|
|
2014-01-06 23:14:06 +01:00
|
|
|
def get(self, options):
|
2015-08-30 00:06:20 +02:00
|
|
|
data = self.get_urldata()
|
2015-01-20 13:10:35 +01:00
|
|
|
|
|
|
|
vid = findvid(self.url, data)
|
|
|
|
if vid is None:
|
2015-09-06 14:19:10 +02:00
|
|
|
yield ServiceError("Can't find video id for %s" % self.url)
|
2015-01-20 13:10:35 +01:00
|
|
|
return
|
2013-01-17 00:21:47 +01:00
|
|
|
|
2014-12-15 00:32:41 +01:00
|
|
|
if options.username and options.password:
|
2015-10-05 19:43:57 +02:00
|
|
|
work = self._login(options.username, options.password)
|
|
|
|
if isinstance(work, Exception):
|
|
|
|
yield work
|
2014-12-15 00:32:41 +01:00
|
|
|
return
|
2015-10-05 19:43:57 +02:00
|
|
|
|
2013-01-17 00:21:47 +01:00
|
|
|
url = "http://premium.tv4play.se/api/web/asset/%s/play" % vid
|
2015-08-31 19:45:15 +02:00
|
|
|
data = self.http.request("get", url, cookies=self.cookies)
|
2015-08-30 10:20:47 +02:00
|
|
|
if data.status_code == 401:
|
2015-08-30 00:06:20 +02:00
|
|
|
xml = ET.XML(data.content)
|
2014-09-07 22:51:44 +02:00
|
|
|
code = xml.find("code").text
|
|
|
|
if code == "SESSION_NOT_AUTHENTICATED":
|
2015-09-06 14:19:10 +02:00
|
|
|
yield ServiceError("Can't access premium content")
|
2014-09-28 19:34:56 +02:00
|
|
|
elif code == "ASSET_PLAYBACK_INVALID_GEO_LOCATION":
|
2015-11-23 00:01:53 +01:00
|
|
|
yield ServiceError("Can't download this video because of geoblock.")
|
2014-09-07 22:51:44 +02:00
|
|
|
else:
|
2015-09-06 14:19:10 +02:00
|
|
|
yield ServiceError("Can't find any info for that video")
|
2014-09-07 22:51:44 +02:00
|
|
|
return
|
2015-08-30 11:27:31 +02:00
|
|
|
if data.status_code == 404:
|
2015-09-06 14:19:10 +02:00
|
|
|
yield ServiceError("Can't find the video api")
|
2015-08-30 11:27:31 +02:00
|
|
|
return
|
2015-08-30 00:06:20 +02:00
|
|
|
xml = ET.XML(data.content)
|
2013-01-17 00:21:47 +01:00
|
|
|
ss = xml.find("items")
|
2013-12-30 01:35:08 +01:00
|
|
|
if is_py2_old:
|
2013-01-17 00:21:47 +01:00
|
|
|
sa = list(ss.getiterator("item"))
|
|
|
|
else:
|
|
|
|
sa = list(ss.iter("item"))
|
|
|
|
|
|
|
|
if xml.find("live").text:
|
|
|
|
if xml.find("live").text != "false":
|
|
|
|
options.live = True
|
2014-06-03 16:25:31 +02:00
|
|
|
if xml.find("drmProtected").text == "true":
|
2015-09-06 14:19:10 +02:00
|
|
|
yield ServiceError("We cant download DRM protected content from this site.")
|
2014-11-25 21:46:33 +01:00
|
|
|
return
|
2014-08-27 22:58:37 +02:00
|
|
|
|
|
|
|
if options.output_auto:
|
|
|
|
directory = os.path.dirname(options.output)
|
|
|
|
options.service = "tv4play"
|
2015-10-07 19:52:37 +02:00
|
|
|
basename = self._autoname(vid)
|
|
|
|
if basename is None:
|
|
|
|
yield ServiceError("Cant find vid id for autonaming")
|
|
|
|
return
|
2015-10-05 20:27:45 +02:00
|
|
|
title = "%s-%s-%s" % (basename, vid, options.service)
|
2014-08-27 22:58:37 +02:00
|
|
|
title = filenamify(title)
|
|
|
|
if len(directory):
|
2015-08-24 23:02:18 +02:00
|
|
|
options.output = os.path.join(directory, title)
|
2014-08-27 22:58:37 +02:00
|
|
|
else:
|
|
|
|
options.output = title
|
|
|
|
|
2014-12-22 17:41:40 +01:00
|
|
|
if self.exclude(options):
|
2015-09-06 23:04:48 +02:00
|
|
|
yield ServiceError("Excluding video")
|
2014-12-22 17:41:40 +01:00
|
|
|
return
|
|
|
|
|
2013-01-17 00:21:47 +01:00
|
|
|
for i in sa:
|
2013-11-17 13:32:33 +01:00
|
|
|
if i.find("mediaFormat").text == "mp4":
|
2014-06-23 21:22:26 +02:00
|
|
|
base = urlparse(i.find("base").text)
|
|
|
|
parse = urlparse(i.find("url").text)
|
2014-12-20 23:00:06 +01:00
|
|
|
if "rtmp" in base.scheme:
|
2014-04-21 18:24:01 +02:00
|
|
|
swf = "http://www.tv4play.se/flash/tv4playflashlets.swf"
|
2014-04-21 21:57:51 +02:00
|
|
|
options.other = "-W %s -y %s" % (swf, i.find("url").text)
|
2014-06-07 20:43:40 +02:00
|
|
|
yield RTMP(copy.copy(options), i.find("base").text, i.find("bitrate").text)
|
2014-06-23 21:22:26 +02:00
|
|
|
elif parse.path[len(parse.path)-3:len(parse.path)] == "f4m":
|
2015-10-04 14:37:16 +02:00
|
|
|
streams = hdsparse(options, self.http.request("get", i.find("url").text, params={"hdcore": "3.7.0"}), i.find("url").text)
|
2014-10-12 23:31:02 +02:00
|
|
|
if streams:
|
|
|
|
for n in list(streams.keys()):
|
|
|
|
yield streams[n]
|
2013-03-10 13:55:34 +01:00
|
|
|
elif i.find("mediaFormat").text == "smi":
|
2014-08-31 01:20:36 +02:00
|
|
|
yield subtitle(copy.copy(options), "smi", i.find("url").text)
|
2014-04-03 19:52:51 +02:00
|
|
|
|
2014-07-13 22:48:34 +02:00
|
|
|
url = "http://premium.tv4play.se/api/web/asset/%s/play?protocol=hls" % vid
|
2015-08-31 19:45:15 +02:00
|
|
|
data = self.http.request("get", url, cookies=self.cookies).content
|
2014-07-13 22:48:34 +02:00
|
|
|
xml = ET.XML(data)
|
|
|
|
ss = xml.find("items")
|
|
|
|
if is_py2_old:
|
|
|
|
sa = list(ss.getiterator("item"))
|
|
|
|
else:
|
|
|
|
sa = list(ss.iter("item"))
|
|
|
|
for i in sa:
|
2014-08-18 22:22:03 +02:00
|
|
|
if i.find("mediaFormat").text == "mp4":
|
|
|
|
parse = urlparse(i.find("url").text)
|
|
|
|
if parse.path.endswith("m3u8"):
|
2015-10-04 14:37:16 +02:00
|
|
|
streams = hlsparse(options, self.http.request("get", i.find("url").text), i.find("url").text)
|
2014-08-18 22:22:03 +02:00
|
|
|
for n in list(streams.keys()):
|
2015-10-04 14:37:16 +02:00
|
|
|
yield streams[n]
|
2014-07-13 22:48:34 +02:00
|
|
|
|
2015-10-07 19:52:37 +02:00
|
|
|
def _get_show_info(self):
|
|
|
|
parse = urlparse(self.url)
|
|
|
|
show = parse.path[parse.path.find("/", 1)+1:]
|
|
|
|
if not re.search("%", show):
|
|
|
|
show = quote_plus(show)
|
|
|
|
data = self.http.request("get", "http://webapi.tv4play.se/play/video_assets?type=episode&is_live=false&platform=web&node_nids=%s&per_page=99999" % show).text
|
|
|
|
jsondata = json.loads(data)
|
|
|
|
return jsondata
|
|
|
|
|
|
|
|
def _autoname(self, vid):
|
|
|
|
jsondata = self._get_show_info()
|
|
|
|
for i in jsondata["results"]:
|
|
|
|
if vid == i["id"]:
|
|
|
|
return i["title"]
|
|
|
|
return None
|
|
|
|
|
2014-04-03 19:52:51 +02:00
|
|
|
def find_all_episodes(self, options):
|
2015-10-05 19:43:57 +02:00
|
|
|
premium = False
|
|
|
|
if options.username and options.password:
|
|
|
|
premium = self._login(options.username, options.password)
|
|
|
|
if isinstance(premium, Exception):
|
|
|
|
log.error(premium.message)
|
|
|
|
return None
|
|
|
|
|
2015-10-07 19:52:37 +02:00
|
|
|
jsondata = self._get_show_info()
|
|
|
|
|
2014-04-03 19:52:51 +02:00
|
|
|
episodes = []
|
2014-12-21 13:01:28 +01:00
|
|
|
n = 1
|
2014-04-03 19:52:51 +02:00
|
|
|
for i in jsondata["results"]:
|
2015-10-05 19:43:57 +02:00
|
|
|
if premium:
|
|
|
|
text = "availability_group_premium"
|
|
|
|
else:
|
|
|
|
text = "availability_group_free"
|
|
|
|
|
2014-04-03 19:52:51 +02:00
|
|
|
try:
|
2015-10-05 19:43:57 +02:00
|
|
|
days = int(i["availability"][text])
|
2014-12-18 14:12:10 +01:00
|
|
|
except (ValueError, TypeError):
|
2014-04-03 19:52:51 +02:00
|
|
|
days = 999
|
2014-11-25 21:48:08 +01:00
|
|
|
if days > 0:
|
2014-07-22 10:13:49 +02:00
|
|
|
video_id = i["id"]
|
|
|
|
url = "http://www.tv4play.se/program/%s?video_id=%s" % (
|
2015-10-19 10:37:03 +02:00
|
|
|
i["program"]["nid"], video_id)
|
2014-04-03 19:52:51 +02:00
|
|
|
episodes.append(url)
|
2014-12-21 13:01:28 +01:00
|
|
|
if n == options.all_last:
|
|
|
|
break
|
|
|
|
n += 1
|
|
|
|
|
2014-12-21 12:27:16 +01:00
|
|
|
return episodes
|
2015-01-20 13:10:35 +01:00
|
|
|
|
2015-10-05 19:43:57 +02:00
|
|
|
def _login(self, username, password):
|
|
|
|
data = self.http.request("get", "https://www.tv4play.se/session/new?https=")
|
|
|
|
auth_token = re.search('name="authenticity_token" ([a-z]+="[^"]+" )?value="([^"]+)"', data.text)
|
|
|
|
if not auth_token:
|
|
|
|
return ServiceError("Can't find authenticity_token needed for user / password")
|
|
|
|
url = "https://www.tv4play.se/session"
|
|
|
|
postdata = {"user_name" : username, "password": password, "authenticity_token":auth_token.group(2), "https": ""}
|
|
|
|
data = self.http.request("post", url, data=postdata, cookies=self.cookies)
|
|
|
|
self.cookies = data.cookies
|
|
|
|
fail = re.search("<p class='failed-login'>([^<]+)</p>", data.text)
|
|
|
|
if fail:
|
|
|
|
return ServiceError(fail.group(1))
|
|
|
|
return True
|
|
|
|
|
2015-09-15 20:10:32 +02:00
|
|
|
|
2015-01-20 13:10:35 +01:00
|
|
|
def findvid(url, data):
|
|
|
|
parse = urlparse(url)
|
|
|
|
if "tv4play.se" in url:
|
|
|
|
try:
|
|
|
|
vid = parse_qs(parse.query)["video_id"][0]
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
match = re.search(r"\"vid\":\"(\d+)\",", data)
|
|
|
|
if match:
|
|
|
|
vid = match.group(1)
|
|
|
|
else:
|
|
|
|
match = re.search(r"-(\d+)$", url)
|
|
|
|
if match:
|
|
|
|
vid = match.group(1)
|
|
|
|
else:
|
2015-01-20 13:12:47 +01:00
|
|
|
match = re.search(r"meta content='([^']+)' property='og:video'", data)
|
|
|
|
if match:
|
|
|
|
match = re.search(r"vid=(\d+)&", match.group(1))
|
|
|
|
if match:
|
|
|
|
vid = match.group(1)
|
|
|
|
else:
|
|
|
|
log.error("Can't find video id for %s", url)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
return None
|
2015-01-20 13:10:35 +01:00
|
|
|
return vid
|