summaryrefslogtreecommitdiff
path: root/plugin.video.catchuptvandmore/resources/lib/channels/eu/arte.py
blob: 9fe6d0b95ec9549e9a7013b151b704eaffeec77e (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# -*- coding: utf-8 -*-
"""
    Catch-up TV & More
    Copyright (C) 2016  SylvainCecchetto

    This file is part of Catch-up TV & More.

    Catch-up TV & More is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Catch-up TV & More is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with Catch-up TV & More; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""

import json
from resources.lib import utils
from resources.lib import common

# Initialize GNU gettext emulation in addon
# This allows to use UI strings from addon’s English
# strings.po file instead of numeric codes
_ = common.ADDON.initialize_gettext()

# TO DO
#   List emissions
#   Most recent
#   Most viewed

URL_REPLAY = 'https://www.arte.tv/papi/tvguide/videos/' \
             'ARTE_PLUS_SEVEN/%s.json?includeLongRights=true'
# Langue, ...

URL_LIVE_ARTE = 'https://api.arte.tv/api/player/v1/livestream/%s'
# Langue, ...

def channel_entry(params):
    """Entry function of the module"""
    if 'root' in params.next:
        return root(params)
    elif 'list_shows' in params.next:
        return list_shows(params)
    elif 'list_videos' in params.next:
        return list_videos(params)
    elif 'live' in params.next:
        return list_live(params)
    elif 'play' in params.next:
        return get_video_url(params)
    return None

@common.PLUGIN.cached(common.CACHE_TIME)
def root(params):
    """Add Replay and Live in the listing"""
    modes = []

    # Add Replay
    modes.append({
        'label' : 'Replay',
        'url': common.PLUGIN.get_url(
            action='channel_entry',
            next='list_shows_1',
            category='%s Replay' % params.channel_name.upper(),
            window_title='%s Replay' % params.channel_name.upper()
        ),
    })

    modes.append({
        'label' : 'Live TV',
        'url': common.PLUGIN.get_url(
            action='channel_entry',
            next='live_cat',
            category='%s Live TV' % params.channel_name.upper(),
            window_title='%s Live TV' % params.channel_name.upper()
        ),
    })

    return common.PLUGIN.create_listing(
        modes,
        sort_methods=(
            common.sp.xbmcplugin.SORT_METHOD_UNSORTED,
            common.sp.xbmcplugin.SORT_METHOD_LABEL
        ),
    )

@common.PLUGIN.cached(common.CACHE_TIME)
def list_shows(params):
    """Build categories listing"""
    shows = []
    emissions_list = []
    categories = {}

    desired_language = common.PLUGIN.get_setting(
        params.channel_id + '.language')

    if desired_language == 'DE':
        desired_language = 'D'
    else:
        desired_language = 'F'

    file_path = utils.download_catalog(
        URL_REPLAY % desired_language,
        '%s_%s.json' % (params.channel_name, desired_language)
    )
    file_replay = open(file_path).read()
    json_parser = json.loads(file_replay)

    for emission in json_parser['paginatedCollectionWrapper']['collection']:
        emission_dict = {}
        emission_dict['duration'] = emission['videoDurationSeconds']
        emission_dict['video_url'] = emission['videoPlayerUrl'].encode('utf-8')
        emission_dict['image'] = emission['programImage'].encode('utf-8')
        try:
            emission_dict['genre'] = emission['genre'].encode('utf-8')
        except:
            emission_dict['genre'] = 'Unknown'
        try:
            emission_dict['director'] = emission['director'].encode('utf-8')
        except:
            emission_dict['director'] = ''
        emission_dict['production_year'] = emission['productionYear']
        emission_dict['program_title'] = emission['VTI'].encode('utf-8')
        try:
            emission_dict['emission_title'] = emission['VSU'].encode('utf-8')
        except:
            emission_dict['emission_title'] = ''

        emission_dict['category'] = emission['VCH'][0]['label'].encode('utf-8')
        categories[emission_dict['category']] = emission_dict['category']
        emission_dict['aired'] = emission['VDA'].encode('utf-8')
        emission_dict['playcount'] = emission['VVI']

        try:
            emission_dict['desc'] = emission['VDE'].encode('utf-8')
        except:
            emission_dict['desc'] = ''

        emissions_list.append(emission_dict)

    with common.PLUGIN.get_storage() as storage:
        storage['emissions_list'] = emissions_list

    for category in categories.keys():

        shows.append({
            'label': category,
            'url': common.PLUGIN.get_url(
                action='channel_entry',
                next='list_videos_cat',
                category=category,
                window_title=category
            ),
        })

    return common.PLUGIN.create_listing(
        shows,
        sort_methods=(
            common.sp.xbmcplugin.SORT_METHOD_UNSORTED,
            common.sp.xbmcplugin.SORT_METHOD_LABEL
        )
    )


@common.PLUGIN.cached(common.CACHE_TIME)
def list_videos(params):
    """Build videos listing"""
    videos = []
    with common.PLUGIN.get_storage() as storage:
        emissions_list = storage['emissions_list']

    if params.next == 'list_videos_cat':
        for emission in emissions_list:
            if emission['category'] == params.category:
                if emission['emission_title']:
                    title = emission['program_title'] + ' - [I]' + \
                        emission['emission_title'] + '[/I]'
                else:
                    title = emission['program_title']
                aired = emission['aired'].split(' ')[0]
                aired_splited = aired.split('/')
                day = aired_splited[0]
                mounth = aired_splited[1]
                year = aired_splited[2]
                # date : string (%d.%m.%Y / 01.01.2009)
                # aired : string (2008-12-07)
                date = '.'.join((day, mounth, year))
                aired = '-'.join((year, mounth, day))
                info = {
                    'video': {
                        'title': title,
                        'plot': emission['desc'],
                        'aired': aired,
                        'date': date,
                        'duration': emission['duration'],
                        'year': emission['production_year'],
                        'genre': emission['genre'],
                        'playcount': emission['playcount'],
                        'director': emission['director'],
                        'mediatype': 'tvshow'
                    }
                }

                # Nouveau pour ajouter le menu pour télécharger la vidéo
                context_menu = []
                download_video = (
                    _('Download'),
                    'XBMC.RunPlugin(' + common.PLUGIN.get_url(
                        action='download_video',
                        url=emission['video_url']) + ')'
                )
                context_menu.append(download_video)
                # Fin

                videos.append({
                    'label': title,
                    'thumb': emission['image'],
                    'url': common.PLUGIN.get_url(
                        action='channel_entry',
                        next='play_r',
                        url=emission['video_url'],
                    ),
                    'is_playable': True,
                    'info': info,
                    'context_menu': context_menu  #  A ne pas oublier pour ajouter le bouton "Download" à chaque vidéo
                })

        return common.PLUGIN.create_listing(
            videos,
            sort_methods=(
                common.sp.xbmcplugin.SORT_METHOD_DATE,
                common.sp.xbmcplugin.SORT_METHOD_DURATION,
                common.sp.xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE,
                common.sp.xbmcplugin.SORT_METHOD_GENRE,
                common.sp.xbmcplugin.SORT_METHOD_PLAYCOUNT,
                common.sp.xbmcplugin.SORT_METHOD_UNSORTED
            ),
            content='tvshows')

@common.PLUGIN.cached(common.CACHE_TIME)
def list_live(params):
    """Build live listing"""
    lives = []

    desired_language = common.PLUGIN.get_setting(
        params.channel_id + '.language')

    if desired_language == 'DE':
        desired_language = 'de'
    else:
        desired_language = 'fr'

    url_live = ''

    file_path = utils.download_catalog(
        URL_LIVE_ARTE % desired_language,
        '%s_%s_live.json' % (params.channel_name, desired_language)
    )
    file_live = open(file_path).read()
    json_parser = json.loads(file_live)

    title = json_parser["videoJsonPlayer"]["VTI"].encode('utf-8')
    img = json_parser["videoJsonPlayer"]["VTU"]["IUR"].encode('utf-8')
    plot = ''
    if 'V7T' in json_parser["videoJsonPlayer"]:
        plot = json_parser["videoJsonPlayer"]["V7T"].encode('utf-8')
    elif 'VDE' in json_parser["videoJsonPlayer"]:
        plot = json_parser["videoJsonPlayer"]["VDE"].encode('utf-8')
    duration = 0
    duration = json_parser["videoJsonPlayer"]["videoDurationSeconds"]
    url_live = json_parser["videoJsonPlayer"]["VSR"]["HLS_SQ_1"]["url"]

    info = {
        'video': {
            'title': title,
            'plot': plot,
            'duration': duration
        }
    }

    lives.append({
        'label': title,
        'fanart': img,
        'thumb': img,
        'url' : common.PLUGIN.get_url(
            action='channel_entry',
            next='play_l',
            url=url_live,
        ),
        'is_playable': True,
        'info': info
    })

    return common.PLUGIN.create_listing(
        lives,
        sort_methods=(
            common.sp.xbmcplugin.SORT_METHOD_UNSORTED,
            common.sp.xbmcplugin.SORT_METHOD_LABEL
        )
    )

@common.PLUGIN.cached(common.CACHE_TIME)
def get_video_url(params):
    """Get video URL and start video player"""
    if params.next == 'play_r' or params.next == 'download_video':
        file_medias = utils.get_webcontent(
            params.url)
        json_parser = json.loads(file_medias)

        url_selected = ''
        video_streams = json_parser['videoJsonPlayer']['VSR']

        desired_quality = common.PLUGIN.get_setting('quality')

        if desired_quality == "DIALOG":
            all_datas_videos = []

            for video in video_streams:
                if not video.find("HLS"):
                        datas = json_parser['videoJsonPlayer']['VSR'][video]
                        new_list_item = common.sp.xbmcgui.ListItem()
                        new_list_item.setLabel(datas['mediaType'] + " (" + datas['versionLibelle'] + ")")
                        new_list_item.setPath(datas['url'])
                        all_datas_videos.append(new_list_item)

            seleted_item = common.sp.xbmcgui.Dialog().select("Choose Stream", all_datas_videos)

            url_selected = all_datas_videos[seleted_item].getPath().encode('utf-8')

        elif desired_quality == "BEST":
            url_selected = video_streams['HTTP_MP4_SQ_1']['url'].encode('utf-8')
        else:
            url_selected = video_streams['HLS_SQ_1']['url'].encode('utf-8')

        return url_selected
    elif params.next == 'play_l':
        return params.url