summaryrefslogtreecommitdiff
path: root/plugin.video.vrt.nu/resources/lib/vrtplayer/vrtplayer.py
blob: 565597d96365f527c1b72bd8876c51d1f4876ecf (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import sys
import xbmc
import os
import xbmcgui
import xbmcplugin
import xbmcaddon
import requests
import re
import  time
from urlparse import parse_qsl
from urlparse import urljoin
from urllib import urlencode
from bs4 import BeautifulSoup
from bs4 import SoupStrainer
from resources.lib.vrtplayer import urltostreamservice
from resources.lib.helperobjects import helperobjects
from resources.lib.vrtplayer import metadatacollector
from resources.lib.vrtplayer import statichelper
from resources.lib.vrtplayer import actions
from resources.lib.vrtplayer import metadatacreator


class VRTPlayer:

    _VRT_LIVESTREAM_URL = "https://live-w.lwc.vrtcdn.be/groupc/live/d05012c2-6a5d-49ff-a711-79b32684615b/live.isml/.m3u8"
    _CANVAS_LIVESTREAM_ = "https://live-w.lwc.vrtcdn.be/groupc/live/905b0602-9719-4d14-ae2a-a9b459630653/live.isml/.m3u8"
    _KETNET_VRT = "https://live-w.lwc.vrtcdn.be/groupc/live/8b898c7d-adf7-4d44-ab82-b5bb3a069989/live.isml/.m3u8"

    _VRT_BASE = "https://www.vrt.be/"
    _VRTNU_BASE_URL = urljoin(_VRT_BASE, "/vrtnu/")
    _VRTNU_SEARCH_URL = "https://search.vrt.be/suggest?facets[categories]="

    def __init__(self, handle, url):
        self._handle = handle
        self._url = url
        self.metadata_collector = metadatacollector.MetadataCollector()
        self._addon = xbmcaddon.Addon()
        self._addon_path = self._addon.getAddonInfo("path")

    def show_listing(self, list_items):
        listing = []
        for title_item in list_items:
            list_item = xbmcgui.ListItem(label=title_item.title)
            url = self._url + '?' + urlencode(title_item.url_dictionary)
            list_item.setProperty('IsPlayable', str(title_item.is_playable))
            list_item.setArt({'thumb': title_item.logo})
            list_item.setInfo('video', title_item.video_dictionary)
            listing.append((url, list_item, not title_item.is_playable))
        xbmcplugin.addDirectoryItems(self._handle, listing, len(listing))
        xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE)
        xbmcplugin.endOfDirectory(self._handle)

    def get_az_menu_items(self):
        url = urljoin(self._VRTNU_BASE_URL, "./a-z/")
        response = requests.get(url)
        tiles = SoupStrainer('a', {"class": "tile"})
        soup = BeautifulSoup(response.content, "html.parser", parse_only=tiles)
        listing = []
        for tile in soup.find_all(class_="tile"):
            link_to_video = tile["href"]
            video_dictionary = self.metadata_collector.get_az_metadata(tile)
            thumbnail, title = self.__get_thumbnail_and_title(tile)
            item = helperobjects.TitleItem(title, {'action': actions.GET_EPISODES, 'video': link_to_video}, False
                                           , thumbnail,
                                           video_dictionary)
            listing.append(item)
        return listing

    def get_category_menu_items(self):
        joined_url = urljoin(self._VRTNU_BASE_URL, "./categorieen/")
        response = requests.get(joined_url)
        tiles = SoupStrainer('a', {"class": "tile tile--category"})
        soup = BeautifulSoup(response.content, "html.parser", parse_only=tiles)
        listing = []
        for tile in soup.find_all(class_="tile"):
            link_to_video = tile["href"]
            thumbnail, title = self.__get_thumbnail_and_title(tile)
            item = helperobjects.TitleItem(title, {'action': actions.GET_CATEGORY_EPISODES, 'video': link_to_video},
            False, thumbnail)
            listing.append(item)
        return listing

    def get_video_category_episodes(self, path):
        category = path.split('/')[-2]
        joined_url = self._VRTNU_SEARCH_URL + category
        response = requests.get(joined_url)
        programs = response.json()
        listing = []
        for program in programs:
            title = program["title"]
            plot = BeautifulSoup(program["description"], "html.parser").text
            thumbnail = statichelper.replace_double_slashes_with_https(program["thumbnail"])

            metadata_creator = metadatacreator.MetadataCreator()
            metadata_creator.plot = plot
            video_dictionary = metadata_creator.get_video_dictionary()
            #cut vrtbase url off since it will be added again when searching for episodes (with a-z we dont have the
            #  full url)
            link_to_video = statichelper.replace_double_slashes_with_https(program["targetUrl"]).replace(self._VRT_BASE,
                                                                                                         "")
            item = helperobjects.TitleItem(title, {'action': actions.GET_EPISODES, 'video': link_to_video},
            False, thumbnail, video_dictionary)
            listing.append(item)
        return listing
    

    def get_main_menu_items(self):
        return {helperobjects.TitleItem(self._addon.getLocalizedString(32091), {'action': actions.LISTING_AZ}, False,
                                        None),
                helperobjects.TitleItem(self._addon.getLocalizedString(32092), {'action': actions.LISTING_CATEGORIES},
                                        False, None),
                helperobjects.TitleItem(self._addon.getLocalizedString(32100), {'action': actions.LISTING_LIVE}, False,
                                        None)}

    def get_livestream_items(self):
        return {helperobjects.TitleItem(self._addon.getLocalizedString(32101),
                                        {'action': actions.PLAY_LIVE, 'video': self._VRT_LIVESTREAM_URL},
                                        True, self.__get_media("een.png")),
                helperobjects.TitleItem(self._addon.getLocalizedString(32102),
                                        {'action': actions.PLAY_LIVE, 'video': self._CANVAS_LIVESTREAM_},
                                        True, self.__get_media("canvas.png")),
                helperobjects.TitleItem(self._addon.getLocalizedString(32103),
                                        {'action': actions.PLAY_LIVE, 'video': self._KETNET_VRT},
                                        True, self.__get_media("ketnet.png"))}

    def get_video_episodes(self, path):
        url = urljoin(self._VRT_BASE, path)
        #xbmc.log(url, xbmc.LOGWARNING)
        # go to url.relevant gets redirected and go on with this url
        relevant_path = requests.get(url)
        response = requests.get(relevant_path.url)
        soup = BeautifulSoup(response.content, "html.parser")
        listing = []
        episodes = soup.find_all(class_="tile")
        if len(episodes) != 0:
            listing.extend(self.get_multiple_videos(soup))
        else:
            li, url = self.get_single_video(relevant_path.url, soup)
            listing.append((url, li, False))

        xbmcplugin.addDirectoryItems(self._handle, listing, len(listing))
        xbmcplugin.endOfDirectory(self._handle)

    def get_multiple_videos(self, soup):
        items = []
        episode_list = soup.find("div", {"id": "episodelist__slider"})

        for tile in episode_list.find_all(class_="tile"):
            li = self.__get_item(tile, "true")
            if li is not None:
                link_to_video = tile["href"]
                video_dictionary = self.metadata_collector.get_multiple_layout_episode_metadata(tile)
                li.setInfo('video', video_dictionary)
                url = '{0}?action=play&video={1}'.format(self._url, link_to_video)
                items.append((url, li, False))
        return items

    def get_single_video(self, path, soup):
        vrt_video = soup.find(class_="vrtvideo")
        thumbnail = VRTPlayer.format_image_url(vrt_video)
        li = xbmcgui.ListItem(soup.find(class_="content__title").text)
        li.setProperty('IsPlayable', 'true')

        video_dictionary = self.metadata_collector.get_single_layout_episode_metadata(soup)

        li.setInfo('video', video_dictionary)
        li.setArt({'thumb': thumbnail})
        url = '{0}?action=play&video={1}'.format(self._url, path)
        return li, url

    def play_video(self, path):
        stream_service = urltostreamservice.UrlToStreamService(self._VRT_BASE,
                                                               self._VRTNU_BASE_URL,
                                                               self._addon)
        stream = stream_service.get_stream_from_url(path)
        if stream is not None:
            play_item = xbmcgui.ListItem(path=stream.stream_url)
            play_item.setMimeType('application/x-mpegURL')
            if stream.subtitle_url is not None:
                play_item.setSubtitles([stream.subtitle_url])
            xbmcplugin.setResolvedUrl(self._handle, True, listitem=play_item)

    def play_livestream(self, path):
        play_item = xbmcgui.ListItem(path=path)
        xbmcplugin.setResolvedUrl(self._handle, True, listitem=play_item)

    def __get_media(self, file_name):
        return os.path.join(self._addon_path, 'resources', 'media', file_name)

    @staticmethod
    def format_image_url(element):
        raw_thumbnail = element.find("img")['srcset'].split('1x,')[0]
        return statichelper.replace_double_slashes_with_https(raw_thumbnail)

    @staticmethod
    def __get_thumbnail_and_title(element):
        thumbnail = VRTPlayer.format_image_url(element)
        found_element = element.find(class_="tile__title")
        title = ""
        if found_element is not None:
            title = statichelper.replace_newlines_and_strip(found_element.contents[0])
        return thumbnail, title

    @staticmethod
    def __get_item(element, is_playable):
        thumbnail = VRTPlayer.format_image_url(element)
        found_element = element.find(class_="tile__title")
        li = None
        if found_element is not None:
            stripped = statichelper.replace_newlines_and_strip(found_element.contents[0])
            li = xbmcgui.ListItem(stripped)
            li.setProperty('IsPlayable', is_playable)
            li.setArt({'thumb': thumbnail})
        return li