Esempio n. 1
0
def favourites(plugin, start=0, **kwargs):
    """Build 'favourites' menu of the addon ('favourites' menu callback function)

    Args:
        plugin (codequick.script.Script)
        start (int): Index of the menu starting item (multiple pages support)
        **kwargs: Arbitrary keyword arguments
    Returns:
        Iterator[codequick.listing.Listitem]: Kodi 'favourites' menu
    """

    # Get sorted items
    sorted_menu = []
    fav_dict = fav.get_fav_dict_from_json()
    menu = []
    for item_hash, item_dict in list(fav_dict['items'].items()):
        item = (item_dict['params']['order'], item_hash, item_dict)
        menu.append(item)

    # We sort the menu according to the item_order values
    sorted_menu = sorted(menu, key=lambda x: x[0])

    # Notify the user if there is no item in favourites
    if len(sorted_menu) == 0:
        Script.notify(Script.localize(30033), Script.localize(30806), display_time=7000)
        yield False

    # Add each item in the listing
    cnt = 0

    for index, (item_order, item_hash, item_dict) in enumerate(sorted_menu):
        if index < start:
            continue

        # If more thant 30 items add a new page
        if cnt == 30:
            yield Listitem().next_page(start=index)
            break

        cnt += 1

        item_dict['params']['from_fav'] = True
        item_dict['params']['item_hash'] = item_hash

        try:
            # Build item from dict
            item = Listitem.from_dict(**item_dict)

            # Generate a valid callback
            url = build_kodi_url(item_dict['callback'], item_dict['params'])
            item.set_callback(url, is_folder=item_dict['params']['is_folder'], is_playbale=item_dict['params']['is_playable'])
Esempio n. 2
0
def favourites(plugin, start=0, **kwargs):
    """Build 'favourites' menu of the addon ('favourites' menu callback function)

    Args:
        plugin (codequick.script.Script)
        start (int): Index of the menu starting item (multiple pages support)
        **kwargs: Arbitrary keyword arguments
    Returns:
        Iterator[codequick.listing.Listitem]: Kodi 'favourites' menu
    """

    # Get sorted items
    sorted_menu = []
    fav_dict = fav.get_fav_dict_from_json()
    menu = []
    for item_hash, item_dict in list(fav_dict.items()):
        item = (item_dict['params']['order'], item_hash, item_dict)
        menu.append(item)

    # We sort the menu according to the item_order values
    sorted_menu = sorted(menu, key=lambda x: x[0])

    # Notify the user if there is not item in favourites
    if len(sorted_menu) == 0:
        Script.notify(Script.localize(30033), Script.localize(30806), display_time=7000)
        yield False

    # Add each item in the listing
    cnt = 0

    for index, (item_order, item_hash, item_dict) in enumerate(sorted_menu):
        if index < start:
            continue

        # If more thant 30 items add a new page
        if cnt == 30:
            yield Listitem().next_page(start=index)
            break

        cnt += 1
        # Listitem.from_dict fails with subtitles
        # See https://github.com/willforde/script.module.codequick/issues/30
        if 'subtitles' in item_dict:
            item_dict.pop('subtitles')

        # Listitem.from_dict only works if context is a list
        if 'context' in item_dict and not isinstance(item_dict['context'], list):
            item_dict.pop('context')

        # Remove original move and hide contexts:
        if 'context' in item_dict:
            new_context = []
            for context in item_dict['context']:
                if 'move_item' not in context[1] and 'hide' not in context[1]:
                    new_context.append(context)
            item_dict['context'] = new_context

        item_dict['params']['from_fav'] = True
        item_dict['params']['item_hash'] = item_hash

        item = Listitem.from_dict(**item_dict)
        url = build_kodi_url(item_dict['callback'], item_dict['params'])

        item.set_callback(url)

        item.is_folder = item_dict['params']['is_folder']
        item.is_playbale = item_dict['params']['is_playable']

        # Rename
        item.context.script(fav.rename_favourite_item,
                            plugin.localize(LABELS['Rename']),
                            item_hash=item_hash)

        # Remove
        item.context.script(fav.remove_favourite_item,
                            plugin.localize(LABELS['Remove']),
                            item_hash=item_hash)

        # Move up
        if item_dict['params']['order'] > 0:
            item.context.script(fav.move_favourite_item,
                                plugin.localize(LABELS['Move up']),
                                direction='up',
                                item_hash=item_hash)

        # Move down
        if item_dict['params']['order'] < len(fav_dict) - 1:
            item.context.script(fav.move_favourite_item,
                                plugin.localize(LABELS['Move down']),
                                direction='down',
                                item_hash=item_hash)

        yield item