def add_link_outline(link):
    log_debug('add_link_outline', 2)
    log_debug('link: %s' % link, 3)

    if __tunein__.is_custom_url_id(utils.get_value(link, 'guide_id')):
        add_custom_url(link)
        return

    params = utils.get_params(link['URL'].split('?')[1])
    category = utils.get_value(params, 'c')
    offset = utils.get_value(params, 'offset')
    filter = utils.get_value(params, 'filter')
    pivot = utils.get_value(params, 'pivot')
    id = utils.get_value(params, 'id')
    name = utils.get_value(link, 'text')
    logo = ''

    if 'image' in link:
        logo = utils.get_value(link, 'image')
    label = ''
    if 'subtext' in link:
        label = utils.get_value(link, 'subtext')

    path = 'browse'
    if __tunein__.is_show_id(id):
        path = 'tune-show'
        contextmenu = [(__settings__.get_string(1009), 'XBMC.RunPlugin(%s?path=%s&id=%s)' %
                    (__settings__.get_argv(0), 'add', id, ))]
    else:
        contextmenu = []

    url = utils.add_params(__settings__.get_argv(0), {
                           'path': path, 'id': id, 'c': category, 'name': name, 'filter': filter, 'offset': offset, 'pivot': pivot})

    add_directory_item(name, url, label=label, logo=logo, contextmenu=contextmenu)
def add_topic_outline(topic):
    log_debug('add_topic_outline', 2)
    log_debug('topic: %s' % topic, 3)

    id = utils.get_value(topic, 'guide_id')
    name = utils.get_value(topic, 'text')
    subtext = utils.get_value(topic, 'subtext')
    stream_type = utils.get_value(topic, 'stream_type')

    params = utils.get_params(topic['URL'].split('?')[1])
    show_id = utils.get_value(params, 'sid')

    if not __tunein__.is_topic_id(id):
        return

    if not __enabledownloads__ and stream_type == 'download':
        return

    artist = ''
    comment = ''
    genre = ''
    album = ''
    for show in __showscache__.get():
        if 'guide_id' in show and show['guide_id'] == show_id:
            artist = utils.get_value(show, 'text')
            if len(artist) == 0:
                artist = utils.get_value(show, 'hosts')
            album = utils.get_value(show, 'title')
            comment = utils.get_value(show, 'subtext')
            if len(comment) == 0:
                comment = utils.get_value(show, 'description')
            genre = get_genre_name(utils.get_value(show, 'genre_id'))
            break

    url = utils.add_params(
        __settings__.get_argv(0), {'path': 'tune', 'id': id, 'name': name})

    logo = None
    contextmenu = None
    if stream_type == 'download':
        contextmenu = [(__settings__.get_string(1014), 'XBMC.RunPlugin(%s?path=%s&id=%s)' % (
            __settings__.get_argv(0), 'download', id))]
        logo = __settings__.get_path('%s%s%s' % (
            'resources/images/', get_logo_colour(), '/downloads-256.png'))

    add_directory_item(name, url, label=subtext, artist=artist, album=album,
                       comment=comment, genre=genre, logo=logo, isfolder=False, contextmenu=contextmenu)
def add_show_outline(show):
    log_debug('add_show_outline', 2)
    log_debug('show: %s' % show, 3)

    id = utils.get_value(show, 'guide_id')
    name = utils.get_value(show, 'text')
    logo = utils.get_value(show, 'image')
    label = utils.get_value(show, 'subtext')
    album = utils.get_value(show, 'current_track')
    is_preset = utils.get_value(show, 'is_preset')
    preset_number = utils.get_value(show, 'preset_number')

    params = utils.get_params(show['URL'].split('?')[1])
    category = utils.get_value(params, 'c')
    filter = utils.get_value(params, 'filter')

    if not __tunein__.is_show_id(id):
        return

    __showscache__.add(show)

    url = utils.add_params(__settings__.get_argv(0), {
                           'path': 'tune-show', 'id': id, 'name': name, 'logo': logo, 'c': category, 'filter': filter})

    if is_preset == 'true':
        contextmenu = [(
            __settings__.get_string(
                1012), 'XBMC.RunPlugin(%s?path=%s&id=%s&num=%s)' % (__settings__.get_argv(0), 'up', id, preset_number, )),
            (__settings__.get_string(1013), 'XBMC.RunPlugin(%s?path=%s&id=%s&num=%s)' %
             (__settings__.get_argv(0), 'down', id, preset_number,)),
            (__settings__.get_string(1010), 'XBMC.RunPlugin(%s?path=%s&id=%s)' % (__settings__.get_argv(0), 'remove', id, ))]
    else:
        contextmenu = [(__settings__.get_string(
            1009), 'XBMC.RunPlugin(%s?path=%s&id=%s)' % (__settings__.get_argv(0), 'add', id, ))]

    add_directory_item(name, url, label=label, album=album, logo=logo,
                       contextmenu=contextmenu)
# Initialise settings.
__settings__ = settings.Settings(__addonid__, sys.argv)

# Get addon information.
__addonname__ = __settings__.get_name()
__version__ = __settings__.get_version()

# Get addon settings values.
__email__ = __settings__.get('email')
__password__ = __settings__.get('password')
__prompt__ = __settings__.get('prompt') == "true"
__debuglevel__ = __settings__.get('debuglevel')

# Initialise parameters.
__params__ = utils.get_params(__settings__.get_argv(2))
__path__ = utils.get_value(__params__, 'path')
__url__ = utils.get_value(__params__, 'url')


def log_debug(msg, dbglvl):
    if __debuglevel__ >= int(dbglvl):
        print '%s: DEBUG: %s' % (__addonname__, utils.normalize_unicode(msg))


def log_error(msg):
    print '%s: ERROR: %s' % (__addonname__, utils.normalize_unicode(msg))


log_debug('Addon: %s' % __addonname__, 1)
log_debug('Version: %s' % __version__, 1)
def add_directory_item(name, url, label='', artist='', album='', genre='', comment='', logo=None, isfolder=True, contextmenu=None):
    log_debug('add_directory_item', 2)
    log_debug('name: %s' % name, 3)
    log_debug('url: %s' % url, 3)
    log_debug('label: %s' % label, 3)
    log_debug('artist: %s' % artist, 3)
    log_debug('album: %s' % album, 3)
    log_debug('genre: %s' % genre, 3)
    log_debug('comment: %s' % comment, 3)
    log_debug('logo: %s' % logo, 3)
    log_debug('isfolder: %s' % isfolder, 3)
    log_debug('contextmenu: %s' % contextmenu, 3)

    # If logo argument not set use default directory/stream image.
    if (logo is None or len(logo) == 0) and isfolder:
        iconImage = __settings__.get_path('%s%s%s' % (
            'resources/images/', get_logo_colour(), '/folder-32.png'))
        thumbnailImage = __settings__.get_path('%s%s%s' % (
            'resources/images/', get_logo_colour(), '/folder-256.png'))

        # Add custom logos for Browse category
        # pattern = re.compile('(.*)c=(\w+)')
        # result = pattern.match(url)
        params = utils.get_params(url.split('?')[1])
        category = utils.get_value(params, 'c')
        if (category):
            # if result.group(2) == "local":
            if category == "local":
                iconImage = __settings__.get_path('%s%s%s' % (
                    'resources/images/', get_logo_colour(), '/local-32.png'))
                thumbnailImage = __settings__.get_path('%s%s%s' % (
                    'resources/images/', get_logo_colour(), '/local-256.png'))
            elif category == "music":
                iconImage = __settings__.get_path('%s%s%s' % (
                    'resources/images/', get_logo_colour(), '/music-32.png'))
                thumbnailImage = __settings__.get_path('%s%s%s' % (
                    'resources/images/', get_logo_colour(), '/music-256.png'))
            elif category == "talk":
                iconImage = __settings__.get_path('%s%s%s' % (
                    'resources/images/', get_logo_colour(), '/talk-32.png'))
                thumbnailImage = __settings__.get_path('%s%s%s' % (
                    'resources/images/', get_logo_colour(), '/talk-256.png'))
            elif category == "sports":
                iconImage = __settings__.get_path('%s%s%s' % (
                    'resources/images/', get_logo_colour(), '/sports-32.png'))
                thumbnailImage = __settings__.get_path('%s%s%s' % (
                    'resources/images/', get_logo_colour(), '/sports-256.png'))
            elif category == "lang":
                iconImage = __settings__.get_path('%s%s%s' % (
                    'resources/images/', get_logo_colour(), '/by_language-32.png'))
                thumbnailImage = __settings__.get_path('%s%s%s' % (
                    'resources/images/', get_logo_colour(), '/by_language-256.png'))
            elif category == "podcast":
                iconImage = __settings__.get_path('%s%s%s' % (
                    'resources/images/', get_logo_colour(), '/podcasts-32.png'))
                thumbnailImage = __settings__.get_path('%s%s%s' % (
                    'resources/images/', get_logo_colour(), '/podcasts-256.png'))
            elif category == "video":
                iconImage = __settings__.get_path('%s%s%s' % (
                    'resources/images/', get_logo_colour(), '/livetv-32.png'))
                thumbnailImage = __settings__.get_path('%s%s%s' % (
                    'resources/images/', get_logo_colour(), '/livetv-256.png'))

        # Add custom logo for location
        pattern = re.compile('(.*)id=r(.*)')
        result = pattern.match(url)
        if (result):
            iconImage = __settings__.get_path('%s%s%s' % (
                'resources/images/', get_logo_colour(), '/by_location-32.png'))
            thumbnailImage = __settings__.get_path('%s%s%s' % (
                'resources/images/', get_logo_colour(), '/by_location-256.png'))

    elif (logo is None or len(logo) == 0) and not isfolder:
        iconImage = __settings__.get_path('%s%s%s' % (
            'resources/images/', get_logo_colour(), '/stream-32.png'))
        thumbnailImage = __settings__.get_path('%s%s%s' % (
            'resources/images/', get_logo_colour(), '/stream-256.png'))
    else:
        iconImage = logo
        thumbnailImage = logo

    # Add addons settings context menu.
    if contextmenu is None:
        contextmenu = [(__settings__.get_string(
            1011), 'XBMC.RunPlugin(%s?path=refresh)' % __settings__.get_argv(0))]
    else:
        contextmenu.append((__settings__.get_string(
            1011), 'XBMC.RunPlugin(%s?path=refresh)' % __settings__.get_argv(0)))

    liz = xbmcgui.ListItem(name, label, iconImage=iconImage, thumbnailImage=thumbnailImage)
    liz.addContextMenuItems(items=contextmenu, replaceItems=True)
    liz.setInfo('Music', {'Title': name, 'Artist': artist, 'Album':
                album, 'Genre': genre, 'Comment': comment})
    if not isfolder:
        liz.setProperty('IsPlayable', 'true')

    if __settings__.get('fanart') == "true":
        liz.setProperty('fanart_image', __fanart__)
    xbmcplugin.addDirectoryItem(handle=int(
        __settings__.get_argv(1)), url=url, listitem=liz, isFolder=isfolder)
# Get addon settings values.
__username__ = __settings__.get('username')
__password__ = __settings__.get('password')
__latlon__ = __settings__.get('latlon')
__locale__ = __settings__.get('locale')
__iconcolour__ = __settings__.get('iconcolour')
__prompt__ = __settings__.get('prompt') == "true"
__featured__ = __settings__.get('featured') == "true"
__local__ = __settings__.get('local') == "true"
__debuglevel__ = __settings__.get('debuglevel')
__enabledownloads__ = __settings__.get('enabledownloads') == "true"
__onlyfavourites__ = __settings__.get('onlyfavourites') == "true"

# Initialise parameters.
__params__ = utils.get_params(__settings__.get_argv(2))
__path__ = utils.get_value(__params__, 'path')
__id__ = utils.get_value(__params__, 'id')
__fanart__ = os.path.join(__settings__.get_path(), 'fanart.jpg')
if __settings__.get('fanart') == "true" and int(__settings__.get_argv(1)) != -1:
    xbmcplugin.setPluginFanart(int(__settings__.get_argv(1)), __fanart__)

modtime = __formatscache__.lastupdate()
if modtime is None or modtime > (__settings__.get('cacheupdate') * 60 * 60) or len(__formatscache__.get()) == 0:
    __formatscache__.clear()
    __formatscache__.add(tunein.TuneIn(__partnerid__).describe_formats())
__formats__ = get_formats()

# Get mac address to be used as serial.
# Note: function seems to occasionally return 'Busy', so added workaround.
mac_address = utils.mac_address()