1
0
mirror of https://github.com/spaam/svtplay-dl.git synced 2024-11-25 04:35:45 +01:00
svtplay-dl/lib/svtplay_dl/service/npo.py

86 lines
2.5 KiB
Python
Raw Normal View History

2018-05-02 00:12:36 +02: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
2019-08-25 00:40:39 +02:00
2018-05-02 00:12:36 +02:00
import json
import re
2018-05-22 20:19:16 +02:00
from urllib.parse import urlparse
2018-05-02 00:12:36 +02:00
from svtplay_dl.error import ServiceError
from svtplay_dl.fetcher.hls import hlsparse
2019-08-25 00:40:39 +02:00
from svtplay_dl.service import Service
2018-05-02 00:12:36 +02:00
class Npo(Service):
supported_domains = ["npo.nl", "ntr.nl", "omroepwnl.nl", "zapp.nl", "npo3.nl"]
def get(self):
# Get video id
parse = urlparse(self.url)
video_id = parse.path.split("/")[-1]
if "__" in video_id:
video_id = video_id.split("__")[-1]
if not video_id:
yield ServiceError("Can't find video info")
return
# Get prid
2019-09-06 22:31:52 +02:00
prid_src = self.http.request(
"get", "http://e.omroep.nl/metadata/{}".format(video_id)
)
2019-08-25 00:27:31 +02:00
prid_raw = prid_src.text.split("(", 1)[-1].split(")", 1)[0]
2018-05-02 00:12:36 +02:00
try:
janson = json.loads(prid_raw)
prid = janson["prid"]
if "titel" in janson:
2018-05-22 00:02:20 +02:00
self.output["title"] = janson["titel"]
2018-05-02 00:12:36 +02:00
except json.decoder.JSONDecodeError:
2019-08-25 00:33:51 +02:00
yield ServiceError("Can't decode prid request: {}".format(prid_raw))
2018-05-02 00:12:36 +02:00
return
# Get token
2019-09-06 22:31:52 +02:00
token_src = self.http.request(
"get", "http://ida.omroep.nl/app.php/auth/{}".format(prid)
)
2018-05-02 00:12:36 +02:00
try:
janson = json.loads(token_src.text)
token = janson["token"]
except json.decoder.JSONDecodeError:
2019-08-25 00:33:51 +02:00
yield ServiceError("Can't decode token request: {}".format(token_src.text))
2018-05-02 00:12:36 +02:00
return
# Get super api
2019-09-06 22:31:52 +02:00
api_url = self.http.request(
"get", "http://ida.omroep.nl/app.php/{}?token={}".format(prid, token)
)
2018-05-02 00:12:36 +02:00
try:
janson = json.loads(api_url.text)
except json.decoder.JSONDecodeError:
2019-08-25 00:33:51 +02:00
yield ServiceError("Can't decode api request: {}".format(api_url.text))
2018-05-02 00:12:36 +02:00
return
# Get sub api and streams
for item in janson["items"][0]:
stream = None
if item["format"] == "hls":
api = self.http.request("get", item["url"]).text
raw_url = re.search(r'"url":"(.+?)"', api).group(1)
2019-08-25 00:33:51 +02:00
url = json.loads('"{}"'.format(raw_url))
2018-05-02 00:12:36 +02:00
2019-09-06 22:31:52 +02:00
stream = hlsparse(
self.config, self.http.request("get", url), url, output=self.output
)
2018-05-02 00:12:36 +02:00
if stream:
for key in list(stream.keys()):
yield stream[key]