def play(source, player=True):
    if player == 'remote':
        remote_play(source)
    else:
        if source['content_type'] == 'image':
            command = {
                'jsonrpc': '2.0',
                'id': 1,
                'method': 'Player.Open',
                'params': {
                    'item': {
                        'file': source['url']
                    }
                }
            }
            log_utils.log(
                'Play using jsonrpc method Player.Open: |{0!s}|'.format(
                    source['url']), log_utils.LOGDEBUG)
            response = kodi.execute_jsonrpc(command)
        else:
            playback_item = kodi.ListItem(label=source['info']['title'],
                                          path=source['url'])
            playback_item.setProperty('IsPlayable', 'true')
            playback_item.setArt(source['art'])
            playback_item.addStreamInfo(source['content_type'], {})
            if source['is_dash']:
                playback_item.setProperty('inputstreamaddon',
                                          'inputstream.adaptive')
                playback_item.setProperty('inputstream.adaptive.manifest_type',
                                          'mpd')
            elif (source['url'].startswith('rtmp')) and (inputstream_rtmp):
                if kodi.addon_enabled('inputstream.rtmp'):
                    playback_item.setProperty('inputstreamaddon',
                                              'inputstream.rtmp')
            elif (source['url'].endswith('m3u8')) and (inputstream_hls):
                if kodi.addon_enabled('inputstream.hls'):
                    playback_item.setProperty('inputstreamaddon',
                                              'inputstream.hls')
            playback_item.setInfo(source['content_type'], source['info'])
            if player:
                log_utils.log(
                    'Play using Player(): |{0!s}|'.format(source['url']),
                    log_utils.LOGDEBUG)
                kodi.Player().play(source['url'], playback_item)
            else:
                log_utils.log(
                    'Play using set_resolved_url: |{0!s}|'.format(
                        source['url']), log_utils.LOGDEBUG)
                kodi.set_resolved_url(playback_item)
def play_this(item, title='', thumbnail='', player=True, history=None):
    if history is None:
        history = kodi.get_setting('history-add-on-play') == "true"
    override_history = kodi.get_setting('history-add-on-play') == "true"
    stream_url = None
    headers = None
    content_type = 'video'
    override_content_type = None
    is_dash = False
    direct = [
        'rtmp:', 'rtmpe:', 'ftp:', 'ftps:', 'special:', 'plugin:', 'udp:',
        'upnp:'
    ]
    unresolved_source = None
    label = title
    source_label = label
    source_thumbnail = thumbnail
    history_item = None

    if item.startswith('http'):
        with kodi.ProgressDialog(
                '%s...' % kodi.i18n('resolving'),
                '%s:' % kodi.i18n('attempting_determine_type'),
                item) as progress_dialog:
            while not progress_dialog.is_canceled():
                url_override = __check_for_new_url(item)
                if item != url_override:
                    log_utils.log(
                        'Source |{0}| has been replaced by |{1}|'.format(
                            item, url_override), log_utils.LOGDEBUG)
                    progress_dialog.update(
                        5, '%s: %s' % (kodi.i18n('source'), item),
                        '%s: %s' % (kodi.i18n('replaced_with'), url_override),
                        ' ')
                    item = url_override
                result = __get_content_type_and_headers(item)
                content_type = result['content_type']
                headers = result['headers']
                url_override = result['url_override']
                if url_override:
                    log_utils.log(
                        'Source |{0}| has been replaced by |{1}|'.format(
                            item, url_override), log_utils.LOGDEBUG)
                    progress_dialog.update(
                        10, '%s: %s' % (kodi.i18n('source'), item),
                        '%s: %s' % (kodi.i18n('replaced_with'), url_override),
                        ' ')
                    item = url_override

                log_utils.log(
                    'Source |{0}| has media type |{1}|'.format(
                        item, content_type), log_utils.LOGDEBUG)
                progress_dialog.update(
                    20, '%s: %s' % (kodi.i18n('source'), item),
                    '%s: %s' % (kodi.i18n('using_media_type'), content_type),
                    ' ')
                if content_type == 'video' or content_type == 'audio' or content_type == 'image' \
                        or content_type == 'mpd' or content_type == 'smil':
                    source = item
                    if content_type == 'smil':
                        content_type = 'video'
                        smil_result = __get_html_and_headers(item, headers)
                        source = pick_source(
                            parse_smil_source_list(smil_result['contents']))
                    elif content_type == 'mpd':
                        content_type = 'video'
                        if not dash_supported:
                            source = None
                        else:
                            is_dash = True
                    if source:
                        stream_url = source

                elif content_type == 'text':
                    if progress_dialog.is_canceled():
                        sys.exit(0)
                    progress_dialog.update(
                        40, '%s: %s' % (kodi.i18n('source'), item),
                        '%s: URLResolver' % kodi.i18n('attempt_resolve_with'),
                        ' ')
                    content_type = 'video'
                    headers.update({'Referer': item})
                    source = resolve(item, title=title)
                    if source:
                        log_utils.log(
                            'Source |{0}| was |URLResolver supported|'.format(
                                source), log_utils.LOGDEBUG)
                        sd_result = __check_smil_dash(source, headers)
                        source = sd_result['url']
                        is_dash = sd_result['is_dash']
                        if source:
                            progress_dialog.update(
                                98, '%s: %s' % (kodi.i18n('source'), item),
                                '%s: URLResolver' %
                                kodi.i18n('attempt_resolve_with'), '%s: %s' %
                                (kodi.i18n('resolution_successful'), source))
                            stream_url = source

                    if not stream_url:
                        if progress_dialog.is_canceled():
                            sys.exit(0)
                        progress_dialog.update(
                            60, '%s: %s' % (kodi.i18n('source'), item),
                            '%s: youtube-dl' %
                            kodi.i18n('attempt_resolve_with'), ' ')
                        if ytdl_supported(item):
                            ytdl_result = resolve_youtube_dl(item)
                            if ytdl_result['resolved_url']:
                                headers = ytdl_result['headers']
                                label = ytdl_result['label'] if ytdl_result[
                                    'label'] is not None else label
                                source_thumbnail = ytdl_result[
                                    'thumbnail'] if ytdl_result[
                                        'thumbnail'] is not None else source_thumbnail
                                log_utils.log(
                                    'Source |{0}| found by |youtube-dl|'.
                                    format(ytdl_result['resolved_url']),
                                    log_utils.LOGDEBUG)
                                sd_result = __check_smil_dash(
                                    ytdl_result['resolved_url'], headers)
                                source = sd_result['url']
                                is_dash = sd_result['is_dash']
                                if source:
                                    progress_dialog.update(
                                        98,
                                        '%s: %s' % (kodi.i18n('source'), item),
                                        '%s: youtube-dl' %
                                        kodi.i18n('attempt_resolve_with'),
                                        '%s: %s' %
                                        (kodi.i18n('resolution_successful'),
                                         source))
                                    stream_url = source

                if not progress_dialog.is_canceled():
                    progress_dialog.update(100, ' ',
                                           kodi.i18n('resolution_completed'),
                                           ' ')
                    break
                else:
                    sys.exit(0)

        if not stream_url:
            content_type = 'executable'
            scrape_result = scrape(item)
            source = scrape_result['resolved_url']
            override_content_type = scrape_result['content_type']
            unresolved_source = scrape_result['unresolved_url']
            source_label = scrape_result['label']
            headers = scrape_result['headers']
            if scrape_result['thumbnail']:
                source_thumbnail = scrape_result['thumbnail']
            if scrape_result['title']:
                label = scrape_result['title']
            if source:
                log_utils.log(
                    'Source |{0}| found by |Scraping for supported|'.format(
                        source), log_utils.LOGDEBUG)
                if override_content_type == 'video':
                    sd_result = __check_smil_dash(source, headers)
                    source = sd_result['url']
                    is_dash = sd_result['is_dash']
                if source:
                    stream_url = source

        if stream_url:
            stream_url = stream_url.replace(r'\\', '')

    elif any(item.startswith(p) for p in direct):
        log_utils.log('Source |{0}| may be supported'.format(item),
                      log_utils.LOGDEBUG)
        stream_url = item

    if is_dash and (not dash_supported
                    or not kodi.addon_enabled('inputstream.adaptive')):
        stream_url = None

    if stream_url and (content_type == 'video' or content_type == 'audio'
                       or content_type == 'image'
                       or content_type == 'executable'):
        working_dialog = kodi.WorkingDialog()
        with working_dialog:
            play_history = utils.PlayHistory()
            working_dialog.update(20)
            if history or player == 'history':
                history_item = item.split('|')[0]
                if '%' not in history_item:
                    history_item = urllib2.quote(history_item)
                log_utils.log(
                    'Adding source |{0}| to history with content_type |{1}|'.
                    format(item, content_type), log_utils.LOGDEBUG)
                play_history.add(history_item, content_type,
                                 label if label else item,
                                 urllib2.quote(thumbnail))
            working_dialog.update(40)
            if override_content_type and override_history:
                history_item = stream_url
                if history_item.startswith('plugin://') or unresolved_source:
                    history_item = unresolved_source
                history_item = history_item.split('|')[0]
                if '%' not in history_item:
                    history_item = urllib2.quote(history_item)
                log_utils.log(
                    'Adding source |{0}| to history with content_type |{1}|'.
                    format(unresolved_source,
                           override_content_type), log_utils.LOGDEBUG)
                play_history.add(history_item, override_content_type,
                                 source_label, urllib2.quote(source_thumbnail))
            if player == 'history':
                return
            if history_item:
                kodi.refresh_container()
            working_dialog.update(60)
            if (not stream_url.startswith('plugin://')) and (headers
                                                             is not None):
                stream_url = get_url_with_headers(stream_url, headers)

            working_dialog.update(80)
            if any(plugin_id in stream_url
                   for plugin_id in RUNPLUGIN_EXCEPTIONS):
                log_utils.log('Running plugin: |{0!s}|'.format(stream_url),
                              log_utils.LOGDEBUG)
                kodi.execute_builtin('RunPlugin(%s)' % stream_url)
            else:
                if override_content_type:
                    content_type = override_content_type

                source = {
                    'content_type': content_type,
                    'url': stream_url,
                    'is_dash': is_dash,
                    'info': {
                        'title': source_label
                    },
                    'art': {
                        'icon': source_thumbnail,
                        'thumb': source_thumbnail
                    }
                }

                play(source, player)

    else:
        log_utils.log('Found no potential sources: |{0!s}|'.format(item),
                      log_utils.LOGDEBUG)