def appletv_root_menu():
    trending_mode()
    addDirectoryItem(plugin.handle, plugin.url_for(appletv_featured),
                     make_list_item(get_string(30680)), True)
    addDirectoryItem(plugin.handle, plugin.url_for(appletv_sports),
                     make_list_item(get_string(30550)), True)
    addDirectoryItem(plugin.handle, plugin.url_for(appletv_channels),
                     make_list_item(get_string(30560)), True)
    endOfDirectory(plugin.handle)
def process_item_list(item_list):
    stashes = list()
    for item in item_list:
        stash_element = item.find('./stash/json')
        if item.get('id').startswith('loadMore'):
            method_info = util.parse_method_call(item.get('onSelect'))
            if method_info[0] == 'espn.page.loadMore':
                label = item.find('./label')
                label2 = item.find('./label2')
                menu_label = ''
                if label is not None:
                    menu_label = label.text
                if label2 is not None:
                    menu_label = menu_label + ' ' + label2.text
                if label is None and label2 is None:
                    menu_label = get_string(30570)
                url = method_info[3]
                nav_id = method_info[2]
                url = url + '&navigationItemId=' + nav_id
                logging.debug('Load more url %s' % url)
                addDirectoryItem(plugin.handle,
                                 plugin.url_for(appletv_showcase, url=url),
                                 make_list_item(menu_label), True)
        elif not item.get('id') == 'no-event':
            if stash_element is None:
                # Assume goes to another onPlay with a url
                name = item.get('accessibilityLabel')
                image = item.find('./image').get('src')
                url = util.parse_url_from_method(item.get('onPlay'))
                addDirectoryItem(plugin.handle,
                                 plugin.url_for(appletv_showcase, url=url),
                                 make_list_item(name, image), True)
            else:
                stash = stash_element.text.encode('utf-8')
                # Some of the json is baddly formatted
                stash = re.sub(r'\s+"', '"', stash)
                stash_json = json.loads(stash, 'utf-8')
                stash_json['internal_item'] = item
                stashes.append(stash_json)

    logging.debug('sorting %s items' % len(stashes))
    stashes.sort(key=functools.cmp_to_key(compare_appletv))
    for stash_json in stashes:
        if stash_json['type'] == 'upcoming':
            index_tv_shelf(stash_json, True)
        elif 'sessionUrl' in stash_json:
            index_tv_shelf(stash_json, False)
        else:
            index_item_shelf(stash_json)
def appletv_showcase():
    url = arg_as_string('url')
    selected_nav_id = arg_as_string('nav_id')
    et = util.get_url_as_xml_cache(get_url(url))
    navigation_items = et.findall('.//navigation/navigationItem')
    logging.debug('Found %s items' % len(navigation_items))
    if selected_nav_id == '' and len(navigation_items) > 0:
        for navigation_item in navigation_items:
            name = navigation_item.find('./title').text
            nav_id = navigation_item.get('id')
            menu_item = navigation_item.find('.//twoLineMenuItem')
            if menu_item is None:
                menu_item = navigation_item.find('.//twoLineEnhancedMenuItem')
            if menu_item is not None and not menu_item.get('id') == 'no-event':
                addDirectoryItem(
                    plugin.handle,
                    plugin.url_for(appletv_showcase, url=url, nav_id=nav_id),
                    make_list_item(name), True)
    elif len(navigation_items) > 0:
        for navigation_item in navigation_items:
            if str(navigation_item.get('id')) == selected_nav_id:
                logging.debug('Found nav item %s' % selected_nav_id)
                process_item_list(
                    navigation_item.findall('.//twoLineMenuItem'))
                process_item_list(
                    navigation_item.findall('.//twoLineEnhancedMenuItem'))
                setContent(plugin.handle, 'episodes')
    else:  # If there are no navigation items then just dump all of the menu entries
        logging.debug('Dumping all menu items')
        process_item_list(et.findall('.//twoLineMenuItem'))
        process_item_list(et.findall('.//twoLineEnhancedMenuItem'))
        setContent(plugin.handle, 'episodes')
    endOfDirectory(plugin.handle)
def appletv_featured():
    et = util.get_url_as_xml_cache(get_url(APPLE_TV_FEATURED))
    for showcase in et.findall('.//showcase/items/showcasePoster'):
        name = showcase.get('accessibilityLabel')
        image = showcase.find('./image').get('src')
        url = util.parse_url_from_method(showcase.get('onPlay'))
        addDirectoryItem(plugin.handle,
                         plugin.url_for(appletv_showcase, url=url),
                         make_list_item(name, image), True)
    collections = et.findall('.//collectionDivider')
    shelfs = et.findall('.//shelf')
    for i in range(0, len(collections)):
        collection_divider = collections[i]
        shelf = shelfs[i]
        title = collection_divider.find('title').text
        name = shelf.get('id')
        addDirectoryItem(plugin.handle,
                         plugin.url_for(appletv_shelf, shelf_id=name),
                         make_list_item(title), True)
    endOfDirectory(plugin.handle)
def appletv_channels():
    et = util.get_url_as_xml_cache(get_url(APPLE_TV_CHANNELS))
    for channel in et.findall('.//oneLineMenuItem'):
        name = channel.get('accessibilityLabel')
        image = channel.find('.//image').text
        url = util.parse_url_from_method(channel.get('onSelect'))
        addDirectoryItem(plugin.handle,
                         plugin.url_for(appletv_showcase, url=url),
                         make_list_item(name, image), True)
    setContent(plugin.handle, 'episodes')
    endOfDirectory(plugin.handle, updateListing=False)
def appletv_sports():
    et = util.get_url_as_xml_cache(get_url(APPLE_TV_SPORTS))
    images = et.findall('.//image')
    sports = et.findall('.//oneLineMenuItem')
    for i in range(0, min(len(images), len(sports))):
        sport = sports[i]
        image = images[i]
        name = sport.get('accessibilityLabel')
        image = image.text
        url = util.parse_url_from_method(sport.get('onSelect'))
        addDirectoryItem(plugin.handle,
                         plugin.url_for(appletv_showcase, url=url),
                         make_list_item(name, image), True)
    endOfDirectory(plugin.handle, updateListing=False)