def html_listings_user_and_all (listings, room_id=False, device_id=False, user=False, chan_current=False, package=False):
    #
    html_tvguide = '<p style="text-align: right">Last updated {timestamp}</p>'.format(timestamp=datetime.now().strftime('%d/%m/%Y %H:%M:%S'))
    #
    if not listings:
        html_tvguide += _html_no_listings(room_id=room_id, device_id=device_id)
        return html_tvguide
    #
    if room_id and device_id:
        html_tvguide += '<script>setTimeout(function () {getChannel(\'/command/' + str(room_id) + \
                        '/' + str(device_id) + \
                        '?command=getchannel\', true);}, 10000);</script>'
    #
    categories = listings['categories']
    all_count = 0
    html_nav_user = ''
    html_nav_all = ''
    html_content = ''
    #
    # Build HTML code for 'user' TV channels
    #
    user_channels = get_userchannels(user)
    #
    if listings and user_channels:
        #
        all_count += 1
        active = 'active' if all_count == 1 else ''
        #
        html_nav_user += urlopen('web/html/pills_nav.html').read().encode('utf-8').format(active=active,
                                                                                    category=str(user).lower(),
                                                                                    title=str(user)+'\'s favourites')
        #
        temp_listings=[]
        for cat in categories:
            for l in listings['channels'][cat]:
                if l.name() in user_channels:
                    temp_listings.append(l)
        #
        body = _html_listings(temp_listings,
                              room_id=room_id,
                              device_id=device_id,
                              user=user,
                              chan_current=chan_current,
                              package=package)
        #
        html_content += urlopen('web/html/pills_contents.html').read().encode('utf-8').format(active=active,
                                                                                         category=str(user).lower(),
                                                                                         body=body)
        #
        #chan_current = chan_current,
    #
    # Build HTML code for 'all' TV channels
    #
    for cat in categories:
        all_count += 1
        active = 'active' if all_count == 1 else ''
        #
        html_nav_all += urlopen('web/html/pills_nav.html').read().encode('utf-8').format(active=active,
                                                                                    category=cat.lower(),
                                                                                    title=cat)
        #
        body = _html_listings(listings['channels'][cat],
                              room_id=room_id,
                              device_id=device_id,
                              chan_current=chan_current,
                              package=package)
        #
        html_content += urlopen('web/html/pills_contents.html').read().encode('utf-8').format(active=active,
                                                                                         category=cat.lower(),
                                                                                         body=body)
        #
    #
    # If user channels available, change categories into dropdown menu
    if user_channels:
        html_nav_all = urlopen('web/html/pills_nav_dropdown.html').read().encode('utf-8').format(title='All Channels',
                                                                                            dropdowns=html_nav_all)
    #
    # Combine pills for 'user' and 'all' channel listings
    html_nav = html_nav_user + html_nav_all
    #
    html_tvguide += urlopen('web/html/pills_parent.html').read().encode('utf-8').format(nav=html_nav,
                                                                                   content=html_content)
    #
    return html_tvguide
def html_channels_user_and_all (room_id=False, device_id=False, user=False, chan_current=False, package=False):
    #
    if not read_list_channels():
        return _html_no_channels()
    #
    categories = get_channel_categories()
    user_channels = get_userchannels(user)
    #
    html_channels = ''
    #
    if room_id and device_id:
        html_channels += '<script>setTimeout(function () {getChannel(\'/command/device/' + str(room_id) + \
                         '/' + str(device_id) + \
                         '?command=getchannel\', true);}, 10000);</script>'
    #
    all_count = 0
    html_nav_user = ''
    html_nav_all = ''
    html_content = ''
    #
    # Build HTML code for 'user' TV channels
    #
    if user_channels:
        #
        all_count += 1
        active = 'active' if all_count == 1 else ''
        #
        html_nav_user += urlopen('web/html/pills_nav.html').read().encode('utf-8').format(active=active,
                                                                                    category=str(user).lower(),
                                                                                    title=str(user)+'\'s favourites')
        #
        temp_cats=[]
        temp_channels=[]
        for cat in categories:
            chan = get_channel_cat_list(cat)
            for c in chan:
                if c['name'] in user_channels:
                    temp_cats.append(cat)
                    temp_channels.append(c)
        #
        body = _html_channels(temp_cats,
                              temp_channels,
                              room_id=room_id,
                              device_id=device_id,
                              user=user,
                              chan_current=chan_current,
                              package=package)
        #
        html_content += urlopen('web/html/pills_contents.html').read().encode('utf-8').format(active=active,
                                                                                         category=str(user).lower(),
                                                                                         body=body)
        #
        #chan_current = chan_current,
    #
    # Build HTML code for 'all' TV channels
    #
    for cat in categories:
        all_count += 1
        active = 'active' if all_count == 1 else ''
        #
        html_nav_all += urlopen('web/html/pills_nav.html').read().encode('utf-8').format(active=active,
                                                                                    category=cat.lower(),
                                                                                    title=cat)
        #
        body = _html_channels(cat,
                              get_channel_cat_list(cat),
                              room_id=room_id,
                              device_id=device_id,
                              chan_current=chan_current,
                              package=package)
        #
        html_content += urlopen('web/html/pills_contents.html').read().encode('utf-8').format(active=active,
                                                                                         category=cat.lower(),
                                                                                         body=body)
        #
    #
    # If user channels available, change categories into dropdown menu
    if user_channels:
        html_nav_all = urlopen('web/html/pills_nav_dropdown.html').read().encode('utf-8').format(title='All Channels',
                                                                                            dropdowns=html_nav_all)
    #
    # Combine pills for 'user' and 'all' channel listings
    html_nav = html_nav_user + html_nav_all
    #
    html_channels += urlopen('web/html/pills_parent.html').read().encode('utf-8').format(nav=html_nav,
                                                                                   content=html_content)
    #
    return html_channels