summaryrefslogtreecommitdiff
path: root/plugin.video.vrt.nu/resources/lib/vrtplayer/urltostreamservice.py
blob: f295d65ff957326d45f0a37ba013f59d98488891 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import xbmcgui
import requests
import xbmc
import json
import cookielib
import urlparse
import os
from resources.lib.helperobjects import helperobjects


class UrlToStreamService:

    _API_KEY ="3_qhEcPa5JGFROVwu5SWKqJ4mVOIkwlFNMSKwzPDAh8QZOtHqu6L4nD5Q7lk0eXOOG"
    _BASE_GET_STREAM_URL_PATH = "https://mediazone.vrt.be/api/v1/vrtvideo/assets/"

    def __init__(self, vrt_base, vrtnu_base_url, addon):
        self._vrt_base = vrt_base
        self._vrtnu_base_url = vrtnu_base_url
        self._addon = addon
        self._session = requests.session()

    def get_stream_from_url(self, url):
        cred = helperobjects.Credentials(self._addon)
        if not cred.are_filled_in():
            self._addon.openSettings()
            cred.reload()
        url = urlparse.urljoin(self._vrt_base, url)
        r = self._session.post("https://accounts.eu1.gigya.com/accounts.login",
                               {'loginID': cred.username, 'password': cred.password, 'APIKey': self._API_KEY,
                                'targetEnv': 'jssdk',
                                'includeSSOToken': 'true',
                                'authMode': 'cookie'})

        logon_json = r.json()
        if logon_json['errorCode'] == 0:
            uid = logon_json['UID']
            sig = logon_json['UIDSignature']
            ts = logon_json['signatureTimestamp']

            headers = {'Content-Type': 'application/json', 'Referer': self._vrtnu_base_url}
            data = '{"uid": "%s", ' \
                   '"uidsig": "%s", ' \
                   '"ts": "%s", ' \
                   '"email": "%s"}' % (uid, sig, ts, cred.username)

            response = self._session.post("https://token.vrt.be", data=data, headers=headers)
            securevideo_url = "{0}.mssecurevideo.json".format(self.__cut_slash_if_present(url))
            securevideo_response = self._session.get(securevideo_url, cookies=response.cookies)
            json = securevideo_response.json()

            video_id = list(json
                        .values())[0]['videoid']
            final_url = urlparse.urljoin(self._BASE_GET_STREAM_URL_PATH, video_id)

            stream_response = self._session.get(final_url)
            hls = self.__get_hls(stream_response.json()['targetUrls']).replace("https", "http")
            subtitle = None
            if self._addon.getSetting("showsubtitles") == "true":
                subtitle = self.__get_subtitle(stream_response.json()['subtitleUrls'])
            return helperobjects.StreamURLS(hls, subtitle)
        else:
            xbmcgui.Dialog().ok(self._addon.getAddonInfo('name'),
                                self._addon.getLocalizedString(32051),
                                self._addon.getLocalizedString(32052))

    @staticmethod
    def __get_hls(dictionary):
        for item in dictionary:
            if item['type'] == 'HLS':
                return item['url']

    @staticmethod
    def __get_subtitle(dictionary):
        for item in dictionary:
            if item['type'] == 'CLOSED':
                return item['url']

    @staticmethod
    def __cut_slash_if_present(url):
        if url.endswith('/'):
            return url[:-1]
        else:
            return url