summaryrefslogtreecommitdiff
path: root/plugin.video.catchuptvandmore/resources/lib/channels/fr/bfmtv.py
blob: f88615b14ef4e263a36cae5fe70734056da0202b (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
# -*- coding: utf-8 -*-
'''
    Catch-up TV & More
    Copyright (C) 2017  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

url_token = 'http://api.nextradiotv.com/bfmtv-applications/'

url_menu = 'http://www.bfmtv.com/static/static-mobile/bfmtv/' \
           'ios-smartphone/v0/configuration.json'

url_replay = 'http://api.nextradiotv.com/bfmtv-applications/%s/' \
             'getPage?pagename=replay'
# token

url_show = 'http://api.nextradiotv.com/bfmtv-applications/%s/' \
           'getVideosList?category=%s&count=100&page=%s'
# token, category, page_number

url_video = 'http://api.nextradiotv.com/bfmtv-applications/%s/' \
            'getVideo?idVideo=%s'
# token, video_id


@common.plugin.cached(common.cache_time)
def get_token():
    file_token = utils.get_webcontent(url_token)
    token_json = json.loads(file_token)
    return token_json['session']['token'].encode('utf-8')


def channel_entry(params):
    if 'list_shows' in params.next:
        return list_shows(params)
    elif 'list_videos' in params.next:
        return list_videos(params)
    elif 'play' in params.next:
        return get_video_URL(params)


@common.plugin.cached(common.cache_time)
def list_shows(params):
    # Create categories list
    shows = []

    if params.next == 'list_shows_1':
        file_path = utils.download_catalog(
            url_replay % get_token(),
            '%s.json' % (params.channel_name))
        file_categories = open(file_path).read()
        json_categories = json.loads(file_categories)
        json_categories = json_categories['page']['contents'][0]
        json_categories = json_categories['elements'][0]['items']

        for categories in json_categories:
            title = categories['title'].encode('utf-8')
            image_url = categories['image_url'].encode('utf-8')
            category = categories['categories'].encode('utf-8')

            shows.append({
                'label': title,
                'thumb': image_url,
                'url': common.plugin.get_url(
                    action='channel_entry',
                    category=category,
                    next='list_videos_1',
                    title=title,
                    page='1'
                )
            })

        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):
    videos = []
    if params.next == 'list_videos_1':
        file_path = utils.download_catalog(
            url_show % (
                get_token(),
                params.category,
                params.page),
            '%s_%s_%s.json' % (
                params.channel_name,
                params.category,
                params.page))
        file_show = open(file_path).read()
        json_show = json.loads(file_show)

        for video in json_show['videos']:
            video_id = video['video'].encode('utf-8')
            video_id_ext = video['id_ext'].encode('utf-8')
            category = video['category'].encode('utf-8')
            title = video['title'].encode('utf-8')
            description = video['description'].encode('utf-8')
            begin_date = video['begin_date'] # 1486725600,
            image = video['image'].encode('utf-8')
            duration = video['video_duration_ms'] / 1000

            info = {
                'video': {
                    'title': title,
                    'plot': description,
                    #'aired': aired,
                    #'date': date,
                    'duration': duration,
                    #'year': year,
                    'genre': category,
                    'mediatype': 'tvshow'
                }
            }

            videos.append({
                'label': title,
                'thumb': image,
                'url': common.plugin.get_url(
                    action='channel_entry',
                    next='play',
                    video_id=video_id,
                    video_id_ext=video_id_ext
                ),
                'is_playable': True,
                'info': info
            })

        # More videos...
        videos.append({
            'label': common.addon.get_localized_string(30100),
            'url': common.plugin.get_url(
                action='channel_entry',
                category=params.category,
                next='list_videos_1',
                title=title,
                page=str(int(params.page) + 1)
            )

        })

        return common.plugin.create_listing(
            videos,
            sort_methods=(
                common.sp.xbmcplugin.SORT_METHOD_UNSORTED,
                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_UNSORTED
            ),
            content='tvshows')


@common.plugin.cached(common.cache_time)
def get_video_URL(params):
    file_medias = utils.get_webcontent(
        url_video % (get_token(), params.video_id))
    json_parser = json.loads(file_medias)

    url_hd_plus = ''
    url_hd = ''
    url_sd = ''
    url_sd_minus = ''
    url_default = ''

    for media in json_parser['video']['medias']:
        if media['frame_height'] == 270:
            url_sd_minus = media['video_url'].encode('utf-8')
        elif media['frame_height'] == 360:
            url_sd = media['video_url'].encode('utf-8')
        elif media['frame_height'] == 720:
            url_hd = media['video_url'].encode('utf-8')
        elif media['frame_height'] == 1080:
            url_hd_plus = media['video_url'].encode('utf-8')
        url_default = media['video_url'].encode('utf-8')

    desired_quality = common.plugin.get_setting(
        params.channel_id + '.quality')

    if desired_quality == 'HD+' and url_hd_plus:
        return url_hd_plus
    elif url_hd:
        return url_hd

    if desired_quality == 'HD' and url_hd:
        return url_hd
    elif url_hd_plus:
        return url_hd_plus

    if desired_quality == 'SD' and url_sd:
        return url_sd
    elif url_sd_minus:
        return url_sd_minus

    if desired_quality == 'SD-' and url_sd_minus:
        return url_sd_minus
    elif url_sd:
        return url_sd

    return url_default