def mode_episode(pid):
    programme = requests.get((('https://www.bbc.co.uk/programmes/' + pid) + '.json'))
    programme_json = programme.json()['programme']
    picked_url = None
    for version in programme_json['versions']:
        playlist = requests.get((('https://open.live.bbc.co.uk/mediaselector/6/select/version/2.0/mediaset/iptv-all/vpid/' + version['pid']) + '/format/json'))
        playlist_json = playlist.json()
        if ('media' not in playlist_json):
            continue
        audio_items = [item for item in playlist_json['media'] if (item['kind'] == 'audio')]
        audio_items.sort(key=(lambda x: x['bitrate']), reverse=True)
        xbmc.log('Found {0} audio items for the programme version {1}'.format(len(audio_items), version['pid']), level=xbmc.LOGNOTICE)
        picked_stream = audio_items[0]
        picked_url = picked_stream['connection'][1]['href']
        xbmc.log('Picked the {0} stream with the bitrate {1}'.format(picked_stream['encoding'], picked_stream['bitrate']), level=xbmc.LOGNOTICE)
        play_item = xbmcgui.ListItem(path=picked_url)
        play_item.setArt({'thumb': (('https://ichef.bbci.co.uk/images/ic/480xn/' + programme_json['image']['pid']) + '.jpg'), 'icon': (('https://ichef.bbci.co.uk/images/ic/480xn/' + programme_json['image']['pid']) + '.jpg')})
        play_item.setInfo('music', {'title': programme_json['display_title']['title'], 'artist': programme_json['display_title']['subtitle'], 'album': programme_json['ownership']['service']['title'], 'comment': programme_json['short_synopsis']})
        xbmcplugin.setResolvedUrl(addon_handle, True, listitem=play_item)
    if (picked_url is None):
        xbmcgui.Dialog().notification(__addonname__, 'Episode not available to stream', icon=xbmcgui.NOTIFICATION_ERROR)
def mode_episode(pid):
    programme = requests.get((('https://www.bbc.co.uk/programmes/' + pid) + '.json'))
    programme_json = programme.json()['programme']
    picked_url = None
    for version in programme_json['versions']:
        playlist = requests.get((('https://open.live.bbc.co.uk/mediaselector/6/select/version/2.0/mediaset/iptv-all/vpid/' + version['pid']) + '/format/json'))
        playlist_json = playlist.json()
        if ('media' not in playlist_json):
            continue
        audio_items = [item for item in playlist_json['media'] if (item['kind'] == 'audio')]
        audio_items.sort(key=(lambda x: x['bitrate']), reverse=True)
        xbmc.log('Found {0} audio items for the programme version {1}'.format(len(audio_items), version['pid']), level=xbmc.LOGNOTICE)
        picked_stream = audio_items[0]
        picked_url = picked_stream['connection'][1]['href']
        xbmc.log('Picked the {0} stream with the bitrate {1}'.format(picked_stream['encoding'], picked_stream['bitrate']), level=xbmc.LOGNOTICE)
        play_item = xbmcgui.ListItem(path=picked_url)
        play_item.setArt({'thumb': (('https://ichef.bbci.co.uk/images/ic/480xn/' + programme_json['image']['pid']) + '.jpg'), 'icon': (('https://ichef.bbci.co.uk/images/ic/480xn/' + programme_json['image']['pid']) + '.jpg')})
        play_item.setInfo('music', {'title': programme_json['display_title']['title'], 'artist': programme_json['display_title']['subtitle'], 'album': programme_json['ownership']['service']['title'], 'comment': programme_json['short_synopsis']})
        xbmcplugin.setResolvedUrl(addon_handle, True, listitem=play_item)
    if (picked_url is None):
        xbmcgui.Dialog().notification(__addonname__, 'Episode not available to stream', icon=xbmcgui.NOTIFICATION_ERROR)