summaryrefslogtreecommitdiff
path: root/addon.py
blob: 17b316e397d937fb74a5a9bcb605e490433021d1 (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
# -*- coding: UTF-8 -*-
""" Kan Video plugin """

from bs4 import BeautifulSoup
import sys
import urllib
#import urllib2
import requests
import urlparse
import xbmc
import xbmcgui
import xbmcplugin


KAN_URL = 'http://www.kan.org.il'
USER_AGENT = 'Kodi'   # I'm optimistic


def build_url(base_url, query):
    return base_url + '?' + urllib.urlencode(query)


def read_url(url):
    headers = {'user-agent': 'Kodi/plugin.video.kan'}
    response = requests.get(url, headers=headers)
    if response.status_code != 200:
        raise IOError("Invalid URL {}".format(url))
    return response.content


def placeholder_folder(foldername):
    url = 'http://localhost/some_video.mkv'
    li = xbmcgui.ListItem(foldername + 'Not Implemented',
                          iconImage='DefaultVideo.png')
    xbmcplugin.addDirectoryItem(handle=addon_handle, url=url, listitem=li)
    xbmcplugin.endOfDirectory(addon_handle)


def tvshows_menu():
    tvshows_url = 'http://www.kan.org.il/video/programs.aspx'
    main_page = read_url(tvshows_url)
    parsed = BeautifulSoup(main_page, "lxml")
    anchors = parsed.find_all('a',
                              class_="program_category_link w-inline-block")
    for a in anchors:
        path = a.get('href')
        li = xbmcgui.ListItem(foldername + 'Show' + path)
        xbmcplugin.addDirectoryItem(handle=addon_handle, listitem=li)
    xbmcplugin.endOfDirectory(addon_handle)


def main():
    base_url = sys.argv[0]
    addon_handle = int(sys.argv[1])
    args = urlparse.parse_qs(sys.argv[2][1:])
    mode = args.get('mode', None)

    xbmc.log("Tzafrir: base_url: {}, addon_handle: {}, mode: {}".format(base_url, addon_handle, mode), xbmc.LOGINFO)
    if mode is None:
        url = build_url(base_url, {'mode': 'folder', 'foldername': 'tv-shows'})
        li = xbmcgui.ListItem(u'תוכניות טלוויזיה')
        xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
                                    listitem=li, isFolder=True)

        url = build_url(base_url, {'mode': 'folder', 'foldername': 'net-shows'})
        li = xbmcgui.ListItem(u'תוכניות רשת')
        xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
                                    listitem=li, isFolder=True)

        url = build_url(base_url, {'mode': 'folder', 'foldername': 'new-items'})
        li = xbmcgui.ListItem(u'קטעים חדשים')
        xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
                                    listitem=li, isFolder=True)

        xbmcplugin.endOfDirectory(addon_handle)

    elif mode[0] == 'folder':
        foldername = args['foldername'][0]
        if foldername == 'tv-shows':
            tvshows_menu()
        else:
            placeholder_folder(foldername)


if __name__ == '__main__':
    main()