summaryrefslogtreecommitdiff
path: root/plugin.video.catchuptvandmore/addon.py
blob: 229d7d2839f3e4605cce6facca1237db5478b9d5 (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
# -*- 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 imp
from resources.lib import skeleton
from resources.lib import common

# Useful path
lib_path = common.sp.xbmc.translatePath(
    common.sp.os.path.join(
        common.addon.path,
        "resources",
        "lib"))

media_path = (
    common.sp.xbmc.translatePath(
        common.sp.os.path.join(
            common.addon.path,
            "resources",
            "media"
        )))

# 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()


@common.plugin.action()
def root(params):
    """
    Build the addon main menu
    with all not hidden categories
    """
    listing = []
    last_category_id = ''
    for category_id, string_id in skeleton.categories.iteritems():
        if common.plugin.get_setting(category_id):
            last_category_id = category_id
            last_window_title = _(string_id)
            context_menu = []
            hide = (
                _('Hide'),
                'XBMC.RunPlugin(' + common.plugin.get_url(
                    action='hide',
                    item_id=category_id) + ')'
            )
            context_menu.append(hide)
            listing.append({
                'label': _(string_id),
                'url': common.plugin.get_url(
                    action='list_channels',
                    category_id=category_id,
                    window_title=_(string_id)
                ),
                'context_menu': context_menu
            })

    # If only one category is present, directly open this category
    if len(listing) == 1:
        params['category_id'] = last_category_id
        params['window_title'] = last_window_title
        return list_channels(params)

    return common.plugin.create_listing(
        listing,
        sort_methods=(
            common.sp.xbmcplugin.SORT_METHOD_UNSORTED,
            common.sp.xbmcplugin.SORT_METHOD_LABEL)
    )


@common.plugin.action()
def list_channels(params):
    """
    Build the channels list
    of the desired category
    """

    # First, we sort channels by order
    channels_dict = skeleton.channels[params.category_id]
    channels = []
    for channel_id, title in channels_dict.iteritems():
        # If channel isn't disable
        if common.plugin.get_setting(channel_id):
            channel_order = common.plugin.get_setting(channel_id + '.order')
            channel = (channel_order, channel_id, title)
            channels.append(channel)

    channels = sorted(channels, key=lambda x: x[0])

    # Secondly, we build channels list in Kodi
    listing = []
    for index, (order, channel_id, title) in enumerate(channels):
        # channel_id = channels.fr.6play.w9
        [
            channel_type,  # channels
            channel_country,  # fr
            channel_file,  # 6play
            channel_name  # w9
        ] = channel_id.split('.')

        # channel_module = channels.fr.6play
        channel_module = '.'.join((
            channel_type,
            channel_country,
            channel_file))

        media_channel_path = common.sp.xbmc.translatePath(
            common.sp.os.path.join(
                media_path,
                channel_type,
                channel_country,
                channel_name
            ))

        # Build context menu (Move up, move down, ...)
        context_menu = []

        item_down = (
            _('Move down'),
            'XBMC.RunPlugin(' + common.plugin.get_url(
                action='move',
                direction='down',
                channel_id_order=channel_id + '.order',
                displayed_channels=channels) + ')'
        )
        item_up = (
            _('Move up'),
            'XBMC.RunPlugin(' + common.plugin.get_url(
                action='move',
                direction='up',
                channel_id_order=channel_id + '.order',
                displayed_channels=channels) + ')'
        )

        if index == 0:
            context_menu.append(item_down)
        elif index == len(channels) - 1:
            context_menu.append(item_up)
        else:
            context_menu.append(item_up)
            context_menu.append(item_down)

        hide = (
            _('Hide'),
            'XBMC.RunPlugin(' + common.plugin.get_url(
                action='hide',
                item_id=channel_id) + ')'
        )
        context_menu.append(hide)

        icon = media_channel_path + '.png'
        fanart = media_channel_path + '_fanart.png'

        listing.append({
            'icon': icon,
            'fanart': fanart,
            'label': title,
            'url': common.plugin.get_url(
                action='channel_entry',
                next='list_shows_1',
                channel_name=channel_name,
                channel_module=channel_module,
                channel_id=channel_id,
                channel_country=channel_country,
                window_title=title
            ),
            'context_menu': context_menu
        })

    return common.plugin.create_listing(
        listing,
        sort_methods=(
            common.sp.xbmcplugin.SORT_METHOD_UNSORTED,)
    )


@common.plugin.action()
def channel_entry(params):
    """
    Last plugin action function in addon.py.
    Now we are going into the channel python file.
    The channel file can return folder or not item ; playable or not item
    """
    if 'channel_name' in params and \
            'channel_module' in params and \
            'channel_id' in params and \
            'channel_country' in params:
        channel_name = params.channel_name
        channel_module = params.channel_module
        channel_id = params.channel_id
        channel_country = params.channel_country
        with common.plugin.get_storage() as storage:
            storage['last_channel_name'] = channel_name
            storage['last_channel_module'] = channel_module
            storage['last_channel_id'] = channel_id
            storage['last_channel_country'] = channel_country
    else:
        with common.plugin.get_storage() as storage:
            channel_name = storage['last_channel_name']
            channel_module = storage['last_channel_module']
            channel_id = storage['last_channel_id']
            channel_country = storage['last_channel_country']

    params['channel_name'] = channel_name
    params['channel_id'] = channel_id
    params['channel_country'] = channel_country

    channel_path = common.sp.xbmc.translatePath(
        common.sp.os.path.join(
            lib_path,
            channel_module.replace('.', '/') + '.py'))

    channel = imp.load_source(
        channel_name,
        channel_path)

    # Let's go to the channel file ...
    return channel.channel_entry(params)


@common.plugin.action()
def move(params):
    if params.direction == 'down':
        offset = + 1
    elif params.direction == 'up':
        offset = - 1

    for k in range(0, len(params.displayed_channels)):
        channel = eval(params.displayed_channels[k])
        channel_order = channel[0]
        channel_id = channel[1]
        if channel_id + '.order' == params.channel_id_order:
            channel_swaped = eval(params.displayed_channels[k + offset])
            channel_swaped_order = channel_swaped[0]
            channel_swaped_id = channel_swaped[1]
            common.plugin.set_setting(
                params.channel_id_order,
                channel_swaped_order)
            common.plugin.set_setting(
                channel_swaped_id + '.order',
                channel_order)
            common.sp.xbmc.executebuiltin('XBMC.Container.Refresh()')
            return None


@common.plugin.action()
def hide(params):
    if common.plugin.get_setting('show_hidden_items_information'):
        common.sp.xbmcgui.Dialog().ok(
            _('Information'),
            _('To re-enable hidden items go to the plugin settings'))
        common.plugin.set_setting('show_hidden_items_information', False)

    common.plugin.set_setting(params.item_id, False)
    common.sp.xbmc.executebuiltin('XBMC.Container.Refresh()')
    return None


if __name__ == '__main__':
    window_title = common.get_window_title()
    common.plugin.run(window_title)