summaryrefslogtreecommitdiff
path: root/plugin.video.btsportvideo/addon.py
blob: d671ea64880aca18e2d1172d1d707d8bbc28b234 (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
###############################################################################
#
# MIT License
#
# Copyright (c) 2017 Lee Smith
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
###############################################################################

import traceback
# Workaround for 'Failed to import _strptime because the import lock is held by another thread.'
import _strptime  # pylint: disable=unused-import

from kodiswift import Plugin
import rollbar.kodi

from resources.lib import api


PAGE_SIZE = 9

plugin = Plugin(addon_id='plugin.video.btsportvideo')


def categories():
    yield {'label': '[B]{}[/B]'.format(plugin.get_string(30002)),
           'path': plugin.url_for('search')}
    yield {'label': '[B]{}[/B]'.format(plugin.get_string(30001)),
           'path': plugin.url_for('show_videos_by_category_first_page', category='all')}
    for category in api.categories():
        yield {'label': category,
               'path': plugin.url_for('show_videos_by_category_first_page',
                                      category=category)}


def items(func, route, page, **kwargs):
    videos, npages = func(page=page, page_size=PAGE_SIZE, **kwargs)

    if page > 1:
        yield {
            'label': u'[B]<< {} ({})[/B]'.format(plugin.get_string(30003), page - 1),
            'path': plugin.url_for(route, page=page - 1, **kwargs)
        }
    if page < npages:
        yield {
            'label': u'[B]{} ({}) >> [/B]'.format(plugin.get_string(30004), page + 1),
            'path': plugin.url_for(route, page=page + 1, **kwargs)
        }

    for video in videos:
        yield {
            'label': video.title,
            'thumbnail': video.thumbnail,
            'path': video.url,
            'info': {
                'date': video.date.strftime('%d.%m.%Y'),
                'duration': video.duration
            },
            'is_playable': True
        }


def show_videos(func, route, page, update_listing, **kwargs):
    return plugin.finish(
        items(func, route, page, **kwargs),
        sort_methods=['playlist_order', 'date', 'title', 'duration'],
        update_listing=update_listing
    )


@plugin.cached_route('/')
def index():
    return list(categories())


@plugin.route('/videos/<category>', name='show_videos_by_category_first_page',
              options={'update_listing': False})
@plugin.route('/videos/<category>/<page>')
def show_videos_by_category(category, page='1', update_listing=True):
    return show_videos(api.videos, 'show_videos_by_category', int(page),
                       update_listing, category=category)


@plugin.route('/search/<term>', name='show_search_results_first_page',
              options={'update_listing': False})
@plugin.route('/search/<term>/<page>')
def show_search_results(term, page='1', update_listing=True):
    return show_videos(api.search_results, 'show_search_results', int(page),
                       update_listing, term=term)


@plugin.route('/search')
def search():
    term = plugin.keyboard(heading=plugin.get_string(30002))
    if term:
        url = plugin.url_for('show_search_results_first_page', term=term)
        plugin.redirect(url)


if __name__ == '__main__':
    try:
        plugin.run()
    except Exception as exc:
        if rollbar.kodi.error_report_requested(exc):
            rollbar.kodi.report_error(
                access_token='cc56a52c62c5418ebc87f97b1aa44669',
                version=plugin.addon.getAddonInfo('version'),
                url=plugin.request.url
            )
        plugin.log.error(traceback.format_exc())