예제 #1
0
def auth_trakt():
    start = time.time()
    use_https = kodi.get_setting('use_https') == 'true'
    trakt_timeout = int(kodi.get_setting('trakt_timeout'))
    trakt_api = Trakt_API(use_https=use_https, timeout=trakt_timeout)
    result = trakt_api.get_code()
    code, expires, interval = result['device_code'], result[
        'expires_in'], result['interval']
    time_left = expires - int(time.time() - start)
    line1 = i18n('verification_url') % (result['verification_url'])
    line2 = i18n('prompt_code') % (result['user_code'])
    line3 = i18n('code_expires') % (time_left)
    with kodi.ProgressDialog(i18n('trakt_acct_auth'),
                             line1=line1,
                             line2=line2,
                             line3=line3) as pd:
        pd.update(100)
        while time_left:
            for _ in range(INTERVALS):
                kodi.sleep(interval * 1000 / INTERVALS)
                if pd.is_canceled(): return

            try:
                result = trakt_api.get_device_token(code)
                break
            except urllib2.URLError as e:
                # authorization is pending; too fast
                if e.code in [400, 429]:
                    pass
                elif e.code == 418:
                    kodi.notify(msg=i18n('user_reject_auth'), duration=3000)
                    return
                elif e.code == 410:
                    break
                else:
                    raise

            time_left = expires - int(time.time() - start)
            progress = time_left * 100 / expires
            pd.update(progress, line3=i18n('code_expires') % (time_left))

    try:
        kodi.set_setting('trakt_oauth_token', result['access_token'])
        kodi.set_setting('trakt_refresh_token', result['refresh_token'])
        trakt_api = Trakt_API(result['access_token'],
                              use_https=use_https,
                              timeout=trakt_timeout)
        profile = trakt_api.get_user_profile(cached=False)
        kodi.set_setting('trakt_user',
                         '%s (%s)' % (profile['username'], profile['name']))
        kodi.notify(msg=i18n('trakt_auth_complete'), duration=3000)
    except Exception as e:
        log_utils.log('Trakt Authorization Failed: %s' % (e),
                      log_utils.LOGDEBUG)
예제 #2
0
def download_media(url, path, file_name):
    try:
        progress = int(kodi.get_setting('down_progress'))
        active = not progress == PROGRESS.OFF
        background = progress == PROGRESS.BACKGROUND
        with kodi.ProgressDialog('Premiumize Cloud',
                                 i18n('downloading') % (file_name),
                                 background=background,
                                 active=active) as pd:
            request = urllib2.Request(url)
            request.add_header('User-Agent', USER_AGENT)
            request.add_unredirected_header('Host', request.get_host())
            response = urllib2.urlopen(request)
            content_length = 0
            if 'Content-Length' in response.info():
                content_length = int(response.info()['Content-Length'])

            file_name = file_name.replace('.strm',
                                          get_extension(url, response))
            full_path = os.path.join(path, file_name)
            log_utils.log('Downloading: %s -> %s' % (url, full_path),
                          log_utils.LOGDEBUG)

            path = xbmc.makeLegalFilename(path)
            try:
                try:
                    xbmcvfs.mkdirs(path)
                except:
                    os.makedirs(path)
            except Exception as e:
                log_utils.log('Dir Create Failed: %s' % (e),
                              log_utils.LOGDEBUG)

            if not xbmcvfs.exists(path):
                raise Exception(i18n('failed_create_dir'))

            file_desc = xbmcvfs.File(full_path, 'w')
            total_len = 0
            cancel = False
            while True:
                data = response.read(CHUNK_SIZE)
                if not data:
                    break

                if pd.is_canceled():
                    cancel = True
                    break

                total_len += len(data)
                if not file_desc.write(data):
                    raise Exception(i18n('failed_write_file'))

                percent_progress = (
                    total_len
                ) * 100 / content_length if content_length > 0 else 0
                log_utils.log(
                    'Position : %s / %s = %s%%' %
                    (total_len, content_length, percent_progress),
                    log_utils.LOGDEBUG)
                pd.update(percent_progress)

            file_desc.close()

        if not cancel:
            kodi.notify(msg=i18n('download_complete') % (file_name),
                        duration=5000)
            log_utils.log('Download Complete: %s -> %s' % (url, full_path),
                          log_utils.LOGDEBUG)

    except Exception as e:
        log_utils.log(
            'Error (%s) during download: %s -> %s' % (str(e), url, file_name),
            log_utils.LOGERROR)
        kodi.notify(msg=i18n('download_error') % (str(e), file_name),
                    duration=5000)
예제 #3
0
def download_media(url, path, file_name, translations, progress=None):
    try:
        if progress is None:
            progress = int(kodi.get_setting('down_progress'))
            
        i18n = translations.i18n
        active = not progress == PROGRESS.OFF
        background = progress == PROGRESS.BACKGROUND
            
        with kodi.ProgressDialog(kodi.get_name(), i18n('downloading') % (file_name), background=background, active=active) as pd:
            try:
                headers = dict([item.split('=') for item in (url.split('|')[1]).split('&')])
                for key in headers: headers[key] = urllib.unquote(headers[key])
            except:
                headers = {}
            if 'User-Agent' not in headers: headers['User-Agent'] = BROWSER_UA
            request = urllib2.Request(url.split('|')[0], headers=headers)
            response = urllib2.urlopen(request)
            if 'Content-Length' in response.info():
                content_length = int(response.info()['Content-Length'])
            else:
                content_length = 0
    
            file_name += '.' + get_extension(url, response)
            full_path = os.path.join(path, file_name)
            logger.log('Downloading: %s -> %s' % (url, full_path), log_utils.LOGDEBUG)
    
            path = kodi.translate_path(xbmc.makeLegalFilename(path))
            try:
                try: xbmcvfs.mkdirs(path)
                except: os.makedirs(path)
            except Exception as e:
                logger.log('Path Create Failed: %s (%s)' % (e, path), log_utils.LOGDEBUG)
    
            if not path.endswith(os.sep): path += os.sep
            if not xbmcvfs.exists(path):
                raise Exception(i18n('failed_create_dir'))
            
            file_desc = xbmcvfs.File(full_path, 'w')
            total_len = 0
            cancel = False
            while True:
                data = response.read(CHUNK_SIZE)
                if not data:
                    break
    
                if pd.is_canceled():
                    cancel = True
                    break
    
                total_len += len(data)
                if not file_desc.write(data):
                    raise Exception(i18n('failed_write_file'))
    
                percent_progress = (total_len) * 100 / content_length if content_length > 0 else 0
                logger.log('Position : %s / %s = %s%%' % (total_len, content_length, percent_progress), log_utils.LOGDEBUG)
                pd.update(percent_progress)
            
            file_desc.close()

        if not cancel:
            kodi.notify(msg=i18n('download_complete') % (file_name), duration=5000)
            logger.log('Download Complete: %s -> %s' % (url, full_path), log_utils.LOGDEBUG)

    except Exception as e:
        logger.log('Error (%s) during download: %s -> %s' % (str(e), url, file_name), log_utils.LOGERROR)
        kodi.notify(msg=i18n('download_error') % (str(e), file_name), duration=5000)
예제 #4
0
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)
예제 #5
0
def scrape_supported(url, html, regex):
    parsed_url = urlparse.urlparse(url)
    links = []
    _filter = [
        '.js', 'data:', 'blob:', 'tab=', 'usp=', '/pixel.', '/1x1.',
        'javascript:', 'rss.', 'blank.', '.rss'
    ]
    sources = []
    with kodi.ProgressDialog('%s...' %
                             kodi.i18n('scraping_for_potential_urls'),
                             '%s: %s' % (kodi.i18n('source'), url),
                             ' ',
                             timer=0.2) as progress_dialog:
        while not progress_dialog.is_canceled():
            new_iter = re.findall(regex, html, re.DOTALL)
            len_iter = len(new_iter)
            for index, match in enumerate(new_iter):
                if progress_dialog.is_canceled():
                    sys.exit(0)
                percent = int((float(index) / float(len_iter)) * 100)
                stream_url = match[0]
                if stream_url == '#' or stream_url == '//' or '/' not in stream_url or not re.match('^[hruf:/].+', stream_url) or \
                        any(item in stream_url for item in _filter) or any(stream_url == t[1] for t in links):
                    progress_dialog.update(
                        percent, kodi.i18n('preparing_results'),
                        '%s: %s' % (kodi.i18n('discarded'), '%s' % stream_url),
                        ' ')
                    continue
                stream_url = __check_for_new_url(stream_url).replace(r'\\', '')
                if stream_url.startswith('//'):
                    stream_url = '%s:%s' % (parsed_url.scheme, stream_url)
                elif stream_url.startswith('/'):
                    stream_url = '%s://%s%s' % (
                        parsed_url.scheme, parsed_url.hostname, stream_url)

                host = urlparse.urlparse(stream_url).hostname
                if host is None:
                    continue
                label = host
                if (len(match) > 2) and (match[2] is not None) and (
                        match[2].strip()) and (host not in match[2]):
                    label = match[2].strip()
                elif (len(match) > 1) and (match[1] is not None) and (
                        match[1].strip()) and (host not in match[1]):
                    label = match[1].strip()
                if not isinstance(label, unicode):
                    label = label.decode('utf-8', 'ignore')
                try:
                    parser = HTMLParser()
                    label = parser.unescape(label)
                    try:
                        label = parser.unescape(label)
                    except:
                        pass
                except:
                    pass
                progress_dialog.update(percent, kodi.i18n('preparing_results'),
                                       '%s: %s' % (kodi.i18n('added'), label),
                                       stream_url)
                sources.append((label, stream_url))
            if progress_dialog.is_canceled():
                sys.exit(0)
            break
        if progress_dialog.is_canceled():
            sys.exit(0)

    with kodi.ProgressDialog('%s...' %
                             kodi.i18n('scraping_for_potential_urls'),
                             '%s: %s' % (kodi.i18n('source'), url),
                             ' ',
                             timer=0.1) as progress_dialog:
        while not progress_dialog.is_canceled():
            len_iter = len(sources)
            for index, source in enumerate(sources):
                if progress_dialog.is_canceled():
                    sys.exit(0)
                percent = int((float(index) / float(len_iter)) * 100)
                label = source[0]
                stream_url = source[1]
                hmf = HostedMediaFile(url=stream_url, include_disabled=False)
                potential_type = __get_potential_type(stream_url)
                is_valid = hmf.valid_url()
                is_valid_type = (potential_type != 'audio') and (potential_type
                                                                 != 'image')

                if is_valid and is_valid_type:
                    progress_dialog.update(
                        percent, kodi.i18n('check_for_support'),
                        '%s [%s]: %s' % (kodi.i18n('support_potential'),
                                         'video', 'URLResolver'),
                        '[%s]: %s' % (label, stream_url))
                    links.append({
                        'label': label,
                        'url': stream_url,
                        'resolver': 'URLResolver',
                        'content_type': 'video'
                    })
                    continue
                else:
                    if potential_type == 'text':
                        if ytdl_supported(stream_url):
                            progress_dialog.update(
                                percent, kodi.i18n('check_for_support'),
                                '%s [%s]: %s' %
                                (kodi.i18n('support_potential'), 'video',
                                 'youtube-dl'),
                                '[%s]: %s' % (label, stream_url))
                            links.append({
                                'label': label,
                                'url': stream_url,
                                'resolver': 'youtube-dl',
                                'content_type': 'video'
                            })
                            continue
                        progress_dialog.update(
                            percent, kodi.i18n('check_for_support'),
                            '%s [%s]: %s' % (kodi.i18n('support_potential'),
                                             potential_type, 'None'),
                            '[%s]: %s' % (label, stream_url))
                    else:
                        progress_dialog.update(
                            percent, kodi.i18n('check_for_support'),
                            '%s [%s]: %s' % (kodi.i18n('support_potential'),
                                             potential_type, 'Kodi'),
                            '[%s]: %s' % (label, stream_url))
                    links.append({
                        'label': label,
                        'url': stream_url,
                        'resolver': None,
                        'content_type': potential_type
                    })
                    continue
            if progress_dialog.is_canceled():
                sys.exit(0)
            break
        if progress_dialog.is_canceled():
            sys.exit(0)
    return links