Esempio n. 1
0
def list_network(network):
    items = []

    aa = addict.AudioAddict.get(PROFILE_DIR, network)
    xbmcplugin.setPluginCategory(HANDLE, aa.name)
    xbmcplugin.setContent(HANDLE, 'files')

    # Channels
    items.append((utils.build_path('channels', network),
                  xbmcgui.ListItem(utils.translate(30321)), True))

    # Shows
    if aa.network['has_shows']:
        items.append((utils.build_path('shows', network),
                      xbmcgui.ListItem(utils.translate(30322)), True))

        items.append((utils.build_path('shows', network, 'schedule'),
                      xbmcgui.ListItem(utils.translate(30332)), True))

    # Playlists
    if aa.network['has_playlists']:
        items.append((utils.build_path('playlists', network),
                      xbmcgui.ListItem(utils.translate(30338)), True))

    # Search
    items.append((utils.build_path('search', network),
                  xbmcgui.ListItem(utils.translate(30323)), True))

    utils.list_items(items)
Esempio n. 2
0
def search(network, filter_=None, query=None, page=1):
    aa = addict.AudioAddict.get(PROFILE_DIR, network)
    xbmcplugin.setPluginCategory(HANDLE, aa.name)

    per_page = ADDON.getSettingInt('aa.shows_per_page')

    if not query:
        k = xbmc.Keyboard(heading=utils.translate(30323))
        k.doModal()
        if not k.isConfirmed():
            return False
        query = k.getText()

    if not aa.network['has_shows']:
        filter_ = 'channels'

    items = []
    if not filter_:
        # Channels
        items.append((utils.build_path('search', network, 'channels', query),
                      xbmcgui.ListItem(utils.translate(30321)), True))

        # Shows
        items.append((utils.build_path('search', network, 'shows', query),
                      xbmcgui.ListItem(utils.translate(30322)), True))

    else:
        if filter_ == 'channels':
            channels = aa.search_channels(query, page=page)
            if channels:
                items = list_channels(network,
                                      channels=channels,
                                      do_list=False)

        elif filter_ == 'shows':
            for show in aa.search_shows(query, page).get('results', []):
                item = utils.build_show_item(network, show)
                items.append((item.getPath(), item, True))

        if filter_ and len(items) >= per_page:
            items.append((
                utils.build_path('search',
                                 network,
                                 query,
                                 filter_,
                                 page=page + 1),
                xbmcgui.ListItem(utils.translate(30318)),
                True,
            ))

    utils.list_items(items)
Esempio n. 3
0
def list_episodes(network, slug, page=1):
    aa = addict.AudioAddict.get(PROFILE_DIR, network)
    xbmcplugin.setContent(HANDLE, 'songs')
    xbmcplugin.setPluginCategory(HANDLE, aa.name)

    per_page = ADDON.getSettingInt('aa.shows_per_page')

    items = []
    for ep in aa.get_show_episodes(slug, page, per_page):
        tracks = ep.get('tracks', [])
        if not tracks:
            continue

        item = utils.build_track_item(tracks[0])
        item = utils.add_aa_art(item, ep.get('show'))

        items.append((item.getPath(), item, False))

    if len(items) >= per_page:
        items.append((
            utils.build_path('episodes', network, slug, page=page + 1),
            xbmcgui.ListItem(utils.translate(30318)),
            True,
        ))

    utils.list_items(items)
Esempio n. 4
0
def play_playlist(network, playlist_id, playlist_name=''):
    utils.logd('Fetching tracklist from server...')
    aa = addict.AudioAddict.get(PROFILE_DIR, network)

    with utils.busy_dialog():
        track = aa.next_playlist_track(playlist_id, pop=False)
        item_url = utils.build_path('playlist',
                                    'track',
                                    network,
                                    playlist_id,
                                    track.get('id'),
                                    playlist_name=playlist_name)

        item = utils.build_track_item(track, item_url)

        utils.logd('Managing playlist')
        playlist = xbmc.PlayList(xbmc.PLAYLIST_MUSIC)
        playlist.clear()
        playlist.add(item.getPath(), item)
        playlist.getposition

        xbmcplugin.setResolvedUrl(HANDLE, True, item)

        # Activated through UI, needs explicit play
        if HANDLE == -1:
            utils.logd('Triggering explicit play')
            xbmc.Player().play()
Esempio n. 5
0
def list_networks():
    items = []

    for key, data in addict.NETWORKS.items():
        item = xbmcgui.ListItem(data['name'])
        item.setArt({
            'thumb':
            os.path.join(ADDON_DIR, 'resources', 'assets', key + '.png'),
        })

        item.addContextMenuItems([
            (utils.translate(30307), 'RunPlugin({})'.format(
                utils.build_path('refresh', key))),
        ], True)

        items.append((utils.build_path('networks', key), item, True))

    utils.list_items(items)
Esempio n. 6
0
def list_playlist_menu(network):
    aa = addict.AudioAddict.get(PROFILE_DIR, network)
    xbmcplugin.setPluginCategory(HANDLE, aa.name)

    items = []

    # Popular Playlists
    items.append((utils.build_path('playlists', network, 'popular'),
                  xbmcgui.ListItem(utils.translate(30339)), True))

    # Newest Playlists
    items.append((utils.build_path('playlists', network, 'newest'),
                  xbmcgui.ListItem(utils.translate(30340)), True))

    # Followed Playlists
    items.append((utils.build_path('playlists', network, 'followed'),
                  xbmcgui.ListItem(utils.translate(30341)), True))

    utils.list_items(items)
Esempio n. 7
0
def list_shows_schedule(network, page=1):
    aa = addict.AudioAddict.get(PROFILE_DIR, network)
    xbmcplugin.setPluginCategory(HANDLE, aa.name)

    shows = aa.get_upcoming()
    shows = sorted(shows, key=lambda s: s['start_at'])

    # Shows for "get_upcoming" have "following" always set to False
    # Have to work around this for now :/
    followed_shows = aa.get_shows_followed()
    followed_slugs = [s.get('slug') for s in followed_shows]

    now = addict.datetime_now()
    active = utils.get_playing() or {}
    utils.log('active item', active)
    items = []
    for show in shows:
        end_at = addict.parse_datetime(show.get('end_at'))

        if end_at < now:
            continue

        start_at = addict.parse_datetime(show.get('start_at'))

        show = show.get('show', {})
        channel = show.get('channels', [])[0]

        item = utils.build_show_item(network, show, followed_slugs)
        item.setPath(
            utils.build_path('play',
                             'channel',
                             network,
                             channel.get('key'),
                             live=show.get('now_playing', False)))

        if show.get('now_playing', False):
            label_prefix = utils.translate(30333)  # Live now

            item.setProperty('IsPlayable', 'false')

            if (active.get('is_live', False)
                    and active.get('channel') == channel.get('key')):
                item.select(True)
        else:
            label_prefix = '{} - {}'.format(start_at.strftime('%H:%M'),
                                            end_at.strftime('%H:%M'))

        item.setLabel('[B]{}[/B] - {} [I]({})[/I]'.format(
            label_prefix, _enc(show.get('name')), _enc(channel.get('name'))))

        items.append((item.getPath(), item, False))

    utils.list_items(items)
Esempio n. 8
0
def list_shows_menu(network):
    aa = addict.AudioAddict.get(PROFILE_DIR, network)
    xbmcplugin.setPluginCategory(HANDLE, aa.name)

    items = []

    # Followed Shows
    items.append((utils.build_path('shows', network, 'followed'),
                  xbmcgui.ListItem(utils.translate(30324)), True))

    # By Style
    items.append((utils.build_path('shows', network, 'fields',
                                   'channel_filter_name'),
                  xbmcgui.ListItem(utils.translate(30325)), True))

    # By Channel
    items.append((utils.build_path('shows', network, 'fields', 'channel_name'),
                  xbmcgui.ListItem(utils.translate(30316)), True))

    # Schedule
    items.append((utils.build_path('shows', network, 'schedule'),
                  xbmcgui.ListItem(utils.translate(30332)), True))

    utils.list_items(items)
Esempio n. 9
0
def list_shows_styles(network, field):
    aa = addict.AudioAddict.get(PROFILE_DIR, network)
    xbmcplugin.setPluginCategory(HANDLE, aa.name)

    facets = aa.get_show_facets()
    facets = sorted(facets, key=lambda f: f['name'])

    items = []
    for facet in facets:
        if facet.get('field') != field:
            continue

        item_url = utils.build_path('shows', network, 'facets',
                                    facet.get('name'))
        items.append(
            (item_url, xbmcgui.ListItem(_enc(facet.get('label'))), True))

    utils.list_items(items)
Esempio n. 10
0
def list_shows(network, facet='All', page=1):
    aa = addict.AudioAddict.get(PROFILE_DIR, network)
    xbmcplugin.setPluginCategory(HANDLE, aa.name)

    per_page = ADDON.getSettingInt('aa.shows_per_page')
    shows = aa.get_shows(facet, page=page, per_page=per_page)

    items = []
    for show in shows:
        item = utils.build_show_item(network, show)
        items.append((item.getPath(), item, True))

    if len(items) >= per_page:
        items.append((
            utils.build_path('shows', network, 'followed', page=page + 1),
            xbmcgui.ListItem(utils.translate(30318)),
            True,
        ))

    utils.list_items(items)
Esempio n. 11
0
def resolve_playlist_track(network, playlist_id, track_id, playlist_name=''):
    utils.logd('Resolving track:', track_id)
    aa = addict.AudioAddict.get(PROFILE_DIR, network)

    track = aa.next_playlist_track(playlist_id, refresh=False, pop=True)

    utils.logd('Resolved track: {}'.format(track.get('id')))

    if track_id != track.get('id'):
        utils.logw('Got unexpected track from cache! '
                   'Expected {} but got {}'.format(track_id, track.get('id')))

    album = '{} / {}'.format(aa.name, _enc(playlist_name))
    item = utils.build_track_item(track, album=album)

    xbmcplugin.setResolvedUrl(HANDLE, True, item)

    # Queue another track if it's the last one playing
    playlist = xbmc.PlayList(xbmc.PLAYLIST_MUSIC)
    if playlist.getposition() + 2 < playlist.size():
        return

    # e.g "Yatse" clears the playlist, removing this entry.
    # As such we wait a little before queuing another track
    time.sleep(1)
    utils.logd('Adding another track to the playlist...')
    track = aa.next_playlist_track(playlist_id, refresh=False, pop=False)

    item = utils.build_track_item(track,
                                  utils.build_path(
                                      'playlist',
                                      'track',
                                      network,
                                      playlist_id,
                                      track.get('id'),
                                      playlist_name=playlist_name),
                                  album=album)

    utils.logd('Queuing track: {}'.format(track.get('id')))
    playlist.add(item.getPath(), item)
Esempio n. 12
0
def play_channel(network, channel, live=False):
    utils.logd('Fetching tracklist from server...')
    aa = addict.AudioAddict.get(PROFILE_DIR, network)

    with utils.busy_dialog():
        is_live, track = aa.next_channel_track(channel,
                                               tune_in=True,
                                               refresh=True,
                                               pop=False,
                                               live=live)

        utils.logd('Activating first track: {}, is-live: {}'.format(
            track.get('id'), is_live))
        item_url = utils.build_path('channel',
                                    'track',
                                    network,
                                    channel,
                                    track.get('id'),
                                    is_live=is_live)

        item = utils.build_track_item(track, item_url)

        utils.logd('Managing playlist')
        playlist = xbmc.PlayList(xbmc.PLAYLIST_MUSIC)
        playlist.clear()
        playlist.add(item.getPath(), item)

        xbmcplugin.setResolvedUrl(HANDLE, True, item)

        # Activated through UI, needs explicit play
        if HANDLE == -1:
            utils.logd('Triggering explicit play')
            xbmc.Player().play()

        # TODO: Set listitem active if possible
        xbmc.executebuiltin('Container.Refresh')
Esempio n. 13
0
def list_playlists(network, sort, page=1):
    aa = addict.AudioAddict.get(PROFILE_DIR, network)
    xbmcplugin.setContent(HANDLE, 'songs')
    xbmcplugin.setPluginCategory(HANDLE, aa.name)

    # ToDo shows/playlists per page
    per_page = ADDON.getSettingInt('aa.shows_per_page')

    if sort == 'popular':
        callback = aa.get_playlists_popular
    elif sort == 'newest':
        callback = aa.get_playlists_newest
    elif sort == 'followed':
        callback = aa.get_playlists_followed
    else:
        return

    playlists = callback(page=page, per_page=per_page).get('results', [])

    items = []
    active = utils.get_playing() or {}
    for pl in playlists:
        item = utils.build_playlist_item(network, pl)
        items.append((item.getPath(), item, False))

        if active.get('playlist_id') == pl.get('id'):
            item.select(True)

    if len(items) >= per_page:
        items.append((
            utils.build_path('playlists', network, sort, page=page + 1),
            xbmcgui.ListItem(utils.translate(30318)),
            True,
        ))

    utils.list_items(items)
Esempio n. 14
0
def list_styles(network):
    aa = addict.AudioAddict.get(PROFILE_DIR, network)
    xbmcplugin.setPluginCategory(HANDLE, aa.name)
    xbmcplugin.setContent(HANDLE, 'albums')

    items = []

    filters = aa.get_channel_filters()
    favorites = aa.get_favorite_channels()
    if favorites:
        filters.insert(
            0, {
                'key': 'favorites',
                'name': utils.translate(30308),
                'channels': favorites,
            })

    for style in filters:
        item = xbmcgui.ListItem('{} ({})'.format(
            _enc(style.get('name')), len(style.get('channels', []))))
        items.append((utils.build_path('channels', network,
                                       style.get('key')), item, True))

    utils.list_items(items)
Esempio n. 15
0
def list_channels(network, style=None, channels=None, do_list=True):
    aa = addict.AudioAddict.get(PROFILE_DIR, network)
    xbmcplugin.setPluginCategory(HANDLE, aa.name)
    xbmcplugin.setContent(HANDLE, 'songs')

    items = []

    if not channels:
        if style == 'favorites':
            channels = aa.get_favorite_channels()
        else:
            channels = aa.get_channels(style)

    favorites = [f['channel_id'] for f in aa.get_favorites()]
    # If I ever manage to get label2 to show, that's what we're going to
    # put there...
    # playing = {
    #     p['channel_id']: '{} - {}'.format(_enc(p['track']['display_artist']),
    #                                       _enc(p['track']['display_title']))
    #     for p in aa.get_currently_playing()
    # }

    active = utils.get_playing() or {}
    for channel in channels:
        item_url = utils.build_path('play', 'channel', network,
                                    channel.get('key'))

        item = xbmcgui.ListItem(_enc(channel.get('name')))
        item.setPath(item_url)
        # item.setLabel2(playing[channel.get('id')])
        item.setProperty('IsPlayable', 'false')
        item = utils.add_aa_art(item, channel, 'default', 'compact')

        if active.get('channel') == channel.get('key'):
            item.select(True)

        cmenu = []
        if channel.get('id') not in favorites:
            # Add to favorites
            cmenu.append((utils.translate(30326), 'RunPlugin({})'.format(
                utils.build_path('favorite',
                                 network,
                                 channel.get('key'),
                                 channel_name=_enc(channel.get('name'))))))
        else:
            # Remove from favorites
            cmenu.append((utils.translate(30327), 'RunPlugin({})'.format(
                utils.build_path('unfavorite',
                                 network,
                                 channel.get('key'),
                                 channel_name=_enc(channel.get('name'))))))

        cmenu.append(
            (utils.translate(30330), 'Container.Update({}, return)'.format(
                utils.build_path('listen_history', network,
                                 channel.get('key')))))

        item.addContextMenuItems(cmenu, True)
        items.append((item_url, item, False))

    if not do_list:
        return items
    utils.list_items(items)
Esempio n. 16
0
def monitor_live(skip_shows=None):
    if not skip_shows:
        skip_shows = []

    now = addict.datetime_now()
    addon = xbmcaddon.Addon()

    for network in addict.NETWORKS.keys():
        aa = addict.AudioAddict.get(PROFILE_DIR, network)

        if not aa.is_active or not aa.network['has_shows']:
            continue

        followed = [s.get('slug') for s in aa.get_shows_followed()]

        shows = aa.get_live_shows()
        live_show_ids = [s.get('id') for s in shows]

        # Remove shows which are not live anymore
        skip_shows = [i for i in skip_shows if i in live_show_ids]

        for show in shows:
            if show.get('id') in skip_shows:
                continue

            end_at = addict.parse_datetime(show.get('end_at'))
            if end_at < now:
                continue

            skip_shows.append(show.get('id'))

            _show = show.get('show')
            if _show.get('slug') in followed and addon.getSettingBool(
                    'addon.notify_live'):
                utils.notify('[B]{}[/B] is live!'.format(_show.get('name')))

            if addon.getSettingBool('addon.tune_in_live'):
                filename = xbmc.getInfoLabel('Player.Filenameandpath')
                if not filename:
                    continue

                playing = utils.get_playing()
                if not playing:
                    continue

                chan = _show.get('channels', [])[0]
                if (playing['network'] != network
                        or playing['channel'] != chan.get('key')):
                    utils.logd(
                        'Different network/channel playing, not tuning in.')
                    continue

                if playing['live']:
                    utils.logd('Live stream already playing.')
                    break

                time_left = (end_at - now).seconds
                if time_left < 2:
                    utils.log('Less than 2 minutes left, not tuning in.')
                    break

                utils.log('Tuning in to live stream...')
                xbmc.executebuiltin('RunPlugin({})'.format(
                    utils.build_path('play',
                                     network,
                                     playing['channel'],
                                     live=True)))

    return skip_shows
Esempio n. 17
0
def resolve_channel_track(network, channel, track_id, is_live=False):
    utils.logd('Resolving track:', track_id)
    aa = addict.AudioAddict.get(PROFILE_DIR, network)

    current_is_live, track = aa.next_channel_track(channel,
                                                   tune_in=False,
                                                   refresh=False,
                                                   pop=True,
                                                   live=is_live)

    utils.logd('Resolved track: {}, is-live: {}'.format(
        track.get('id'), current_is_live))

    if track_id != track.get('id'):
        utils.logw('Got unexpected track from cache! '
                   'Expected {} but got {}'.format(track_id, track.get('id')))

    album = '{} / {}'.format(aa.name, _enc(aa.get_channel_name(channel)))
    item = utils.build_track_item(track, album=album)

    xbmcplugin.setResolvedUrl(HANDLE, True, item)

    offset = track.get('content', {}).get('offset', 0)
    if ADDON.getSettingBool('addon.seek_offset') and offset:
        # Have at least 30 sec. left to prevent the track ending before
        # the next one has been queued
        length = track.get('length')
        offset = min(length - 30, offset)

        utils.logd('Seeking to:', offset)
        if not utils.seek_offset(offset):
            utils.logd('Seeking failed!')

    aa.add_listen_history(channel, track_id)

    # Queue another track if it's the last one playing
    playlist = xbmc.PlayList(xbmc.PLAYLIST_MUSIC)
    if playlist.getposition() + 2 < playlist.size():
        return

    # e.g "Yatse" clears the playlist, removing this entry.
    # As such we wait a little before queuing another track
    time.sleep(1)
    utils.logd('Adding another track to the playlist...')
    is_live, track = aa.next_channel_track(channel,
                                           tune_in=False,
                                           refresh=False,
                                           pop=False,
                                           live=not current_is_live)

    item = utils.build_track_item(track,
                                  utils.build_path('channel',
                                                   'track',
                                                   network,
                                                   channel,
                                                   track.get('id'),
                                                   is_live=is_live),
                                  album=album)

    utils.logd('Queuing track: {}, is-live: {}'.format(track.get('id'),
                                                       is_live))
    playlist.add(item.getPath(), item)