summaryrefslogtreecommitdiff
path: root/kodi-mediabrowser/main.py
blob: ad705063462e97e93ecc62324e72e8fe108694a6 (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
#!/usr/bin/env python3
#  -*- coding: utf-8 -*-
# Module: default
import sys
from urlparse import parse_qsl
import xbmcgui
import xbmcplugin
import xbmcaddon
import requests
from urllib import quote
import logging

logging.basicConfig(level=logging.INFO)

# Get the plugin url in plugin:// notation.
_url = sys.argv[0]
# Get the plugin handle as an integer number.
_handle = int(sys.argv[1])

addon = xbmcaddon.Addon()

endpoint = addon.getSetting('endpoint')


def list_files(path):
    resp = requests.get(endpoint + path)
    data = resp.json()
    files = data['files']

    listing = []
    for file in files:
        list_item = xbmcgui.ListItem(label=file['name'])

        if file['type'] == 'directory':
            list_item.setInfo('video', {'title': file['name'] + '/'})
            url = '{0}?action=list_files&path={1}'.format(_url, quote(file['path']))
            listing.append((url, list_item, True))
        else:
            list_item.setInfo('video', {'title': file['name']})
            list_item.setArt({'thumb': endpoint + file['poster']})
            list_item.setProperty('IsPlayable', 'true')
            url = endpoint + file['m3u8']
            listing.append((url, list_item, False))
    xbmcplugin.addDirectoryItems(_handle, listing, len(listing))
    xbmcplugin.addSortMethod(_handle, xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE)
    xbmcplugin.endOfDirectory(_handle)


def router(paramstring):
    params = dict(parse_qsl(paramstring))
    if params:
        if params['action'] == 'list_files':
            # Display the list of videos in a provided category.
            list_videos(params['path'])
        else:
            raise Exception('Invalid params')
    else:
        list_files('/json/')


if __name__ == '__main__':
    router(sys.argv[2][1:])