summaryrefslogtreecommitdiff
path: root/plugin.audio.pbsfm/addon.py
blob: 3b7d7a022331474b612bd57eef8fccbbd96368e1 (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
from kodiswift import Plugin, xbmcgui
import urllib2
import json

plugin = Plugin()
pbsapi = 'http://pulse.emit.com/api/stations/pbs'

@plugin.cached_route('/')
def main_menu():
    items = [
        {'label': plugin.get_string(30000),'path': "http://eno.emit.com:8000/pbsfm_live_64.mp3",'is_playable': True, 'info':{'title': plugin.get_string(30000)}},
        {'label': plugin.get_string(30001), 'path': plugin.url_for('shows')},
        ]

    return items


@plugin.cached_route('/shows/')
def shows():

    shows = fetch_shows()
    
    items = []
    for show in shows:
        items.append({
                    'label': show['name'], 
                    'path': plugin.url_for('list_episodes', slug=show['slug']) 
                    })
    return items


@plugin.cached_route('/episodes/<slug>/')
def list_episodes(slug):

    episodes = fetch_episodes(slug)
    items = []
    if episodes:
        for episode in episodes:
            if episode['streams']:
                items.append({'label': episode['name'], 'path': episode['streams'][0]['href'], 'is_playable': True, 'info':{'title': episode['name']}})
    else:
        last = fetch_last(slug)
        if last['lastEpisode']:
            items.append({'label': last['lastEpisode']['name'], 'path': last['lastEpisode']['streams'][0]['href'], 'is_playable': True, 'info':{'title': episode['name']}})
        else:
            items.append({'label': 'Nothing Found!', 'path': None})
    return items

@plugin.cached(ttl=60*24*7)
def fetch_shows():
    response = urllib2.urlopen(pbsapi)
    home = json.load(response)
    shows = json.load(urllib2.urlopen(home['$shows']))
    return shows

@plugin.cached(ttl=60*24)
def fetch_episodes(slug):
    episode_list = pbsapi+'/shows/'+slug+'/episodes'
    episodes = json.load(urllib2.urlopen(episode_list))

    return episodes

@plugin.cached(ttl=60*24)
def fetch_last(slug):
    show_url = pbsapi+'/shows/'+slug
    show = json.load(urllib2.urlopen(show_url))
    return show


if __name__ == '__main__':
    plugin.run()