Exemplo n.º 1
0
def cond_print(str):
    import kgetcore
    kgetcore.setTextStatus(str)
Exemplo n.º 2
0
def startDownload(kconfig):
    global global_error
    import kgetcore
    # read settings
    option = YoutubeDlOption(kconfig)
    option.parse()
    # Verify video URL format and convert to "standard" format
    video_url_cmdl = kgetcore.getSourceUrl()
    video_url_mo = const_video_url_re.match(video_url_cmdl)
    if video_url_mo is None:
        video_url_mo = const_video_url_re.match(urllib.unquote(video_url_cmdl))
    if video_url_mo is None:
        exit_with_error('URL does not seem to be a youtube video URL. If it is, report a bug.')
        return
    video_url_id = video_url_mo.group(2)
    video_url = const_video_url_str % video_url_id

    video_format = None

    if option.best_quality:
        video_format = const_best_quality_format
        video_extension = '.mp4'
    else:
        video_extension = '.flv'

    if video_format is not None:
        video_url = '%s%s' % (video_url, const_video_url_format_suffix % video_format)

    # Get account information if any
    account_username = None
    account_password = None
    if option.enable_login:
        if option.use_netrc:
            try:
                info = netrc.netrc().authenticators('youtube')
                if info is None:
                    exit_with_error('no authenticators for machine youtube.')
                    return
                account_username = info[0]
                account_password = info[2]
            except IOError:
                exit_with_error('unable to read .netrc file.')
                return
            except netrc.NetrcParseError:
                exit_with_error('unable to parse .netrc file.')
                return
        else:
            # TODO: check
            account_username = option.username
            account_password = option.password

    # Install cookie and proxy handlers
    urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler()))
    urllib2.install_opener(urllib2.build_opener(urllib2.HTTPCookieProcessor()))

    # Log in and confirm age if needed
    if account_username is not None:
        kgetcore.setTextStatus('Logging in...')
        url = const_login_url_str % video_url_id
        post = const_login_post_str % (video_url_id, account_username, account_password)
        reply_page = download_step(True, 'Logging in', 'unable to log in', url, post)
        if global_error:
            return
        if const_bad_login_re.search(reply_page) is not None:
            exit_with_error('unable to log in')
            return

        url = const_age_url_str % video_url_id
        post = const_age_post_str % video_url_id
        download_step(False, 'Confirming age', 'unable to confirm age', url, post)
        if global_error:
            return

    # Install cookie and proxy handlers
    urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler()))
    urllib2.install_opener(urllib2.build_opener(urllib2.HTTPCookieProcessor()))

    # Retrieve video webpage
    video_webpage = download_step(True, 'Retrieving video webpage', 'Unable to retrieve video webpage', video_url)
    if global_error:
        return

    # Extract video title
    video_title = extract_step('Extracting video title', 'Unable to extract video title', const_video_title_re, video_webpage)
    if global_error:
        return

    # Extract needed video URL parameters
    video_url_t_param = extract_step('Extracting URL "t" parameter', 'Unable to extract URL "t" parameter', const_url_t_param_re, video_webpage)
    if global_error:
        return
    video_url_real = const_video_url_real_str % (video_url_id, video_url_t_param)
    if video_format is not None:
        video_url_real = '%s%s' % (video_url_real, const_video_url_format_suffix % video_format)

    # Rebuild filename
    if option.use_literal:
        prefix = title_string_touch(video_title)
        video_filename = '%s-%s%s' % (prefix, video_url_id, video_extension)
    else:
        prefix = title_string_norm(video_title)
    video_filename = '%s%s' % (prefix, video_extension)
    #video_filename = '%s%s' % (video_url_id, video_extension)

    # Check name
    if not video_filename.lower().endswith(video_extension):
        kgetcore.setTextStatus('Warning: video file name does not end in %s\n' % video_extension)

    # Add to kget
    kgetcore.addTransfer(video_url_real, video_filename)
    kgetcore.finish()