Пример #1
0
def get_download_filename_from_response(response):
    from urlparse import urlparse
    from urllib2 import unquote as urllib2_unquote
    filename = last_part_name = ''
    try:
        purl = urlparse(response.geturl())
        last_part_name = urllib2_unquote(purl.path.split('/')[-1])
        disposition = response.info().get('Content-disposition', '')
        for p in disposition.split(';'):
            if 'filename' in p:
                if '*=' in disposition:
                    parts = disposition.split('*=')[-1]
                    filename = parts.split('\'')[-1]
                else:
                    filename = disposition.split('=')[-1]
                if filename[0] in ('\'', '"'):
                    filename = filename[1:]
                if filename[-1] in ('\'', '"'):
                    filename = filename[:-1]
                filename = urllib2_unquote(filename)
                break
    except Exception:
        import traceback
        traceback.print_exc()
    return filename or last_part_name
Пример #2
0
def get_download_filename_from_response(response):
    from urlparse import urlparse
    from urllib2 import unquote as urllib2_unquote
    filename = last_part_name = ''
    try:
        purl = urlparse(response.geturl())
        last_part_name = urllib2_unquote(purl.path.split('/')[-1])
        disposition = response.info().get('Content-disposition', '')
        for p in disposition.split(';'):
            if 'filename' in p:
                if '*=' in disposition:
                    parts = disposition.split('*=')[-1]
                    filename = parts.split('\'')[-1]
                else:
                    filename = disposition.split('=')[-1]
                if filename[0] in ('\'', '"'):
                    filename = filename[1:]
                if filename[-1] in ('\'', '"'):
                    filename = filename[:-1]
                filename = urllib2_unquote(filename)
                break
    except Exception:
        import traceback
        traceback.print_exc()
    return filename or last_part_name
Пример #3
0
def get_download_filename_from_response(response):
    from urllib2 import unquote as urllib2_unquote

    filename = last_part_name = ""
    try:
        last_part_name = response.geturl().split("/")[-1]
        disposition = response.info().get("Content-disposition", "")
        for p in disposition.split(";"):
            if "filename" in p:
                if "*=" in disposition:
                    parts = disposition.split("*=")[-1]
                    filename = parts.split("'")[-1]
                else:
                    filename = disposition.split("=")[-1]
                if filename[0] in ("'", '"'):
                    filename = filename[1:]
                if filename[-1] in ("'", '"'):
                    filename = filename[:-1]
                filename = urllib2_unquote(filename)
                break
    except Exception:
        import traceback

        traceback.print_exc()
    return filename or last_part_name
Пример #4
0
def get_download_filename(url, cookie_file=None):
    '''
    Get a local filename for a URL using the content disposition header
    Returns empty string if no content disposition header present
    '''
    from contextlib import closing
    from urllib2 import unquote as urllib2_unquote

    filename = ''

    br = browser()
    if cookie_file:
        from mechanize import MozillaCookieJar
        cj = MozillaCookieJar()
        cj.load(cookie_file)
        br.set_cookiejar(cj)

    last_part_name = ''
    try:
        with closing(br.open(url)) as r:
            last_part_name = r.geturl().split('/')[-1]
            disposition = r.info().get('Content-disposition', '')
            for p in disposition.split(';'):
                if 'filename' in p:
                    if '*=' in disposition:
                        parts = disposition.split('*=')[-1]
                        filename = parts.split('\'')[-1]
                    else:
                        filename = disposition.split('=')[-1]
                    if filename[0] in ('\'', '"'):
                        filename = filename[1:]
                    if filename[-1] in ('\'', '"'):
                        filename = filename[:-1]
                    filename = urllib2_unquote(filename)
                    break
    except:
        import traceback
        traceback.print_exc()

    if not filename:
        filename = last_part_name

    return filename
Пример #5
0
def get_download_filename(url, cookie_file=None):
    '''
    Get a local filename for a URL using the content disposition header
    Returns empty string if no content disposition header present
    '''
    from contextlib import closing
    from urllib2 import unquote as urllib2_unquote

    filename = ''

    br = browser()
    if cookie_file:
        from mechanize import MozillaCookieJar
        cj = MozillaCookieJar()
        cj.load(cookie_file)
        br.set_cookiejar(cj)

    last_part_name = ''
    try:
        with closing(br.open(url)) as r:
            last_part_name = r.geturl().split('/')[-1]
            disposition = r.info().get('Content-disposition', '')
            for p in disposition.split(';'):
                if 'filename' in p:
                    if '*=' in disposition:
                        parts = disposition.split('*=')[-1]
                        filename = parts.split('\'')[-1]
                    else:
                        filename = disposition.split('=')[-1]
                    if filename[0] in ('\'', '"'):
                        filename = filename[1:]
                    if filename[-1] in ('\'', '"'):
                        filename = filename[:-1]
                    filename = urllib2_unquote(filename)
                    break
    except:
        import traceback
        traceback.print_exc()

    if not filename:
        filename = last_part_name

    return filename
Пример #6
0
def authenticate(username, password, auth_url):
    try:
        output_dict = jabber_crypt.dumps({"username": username, "password": password})
        url = auth_url + "/auth"
        log.write("URL: " + url + "\n")

        up = {"exchange": myquote(output_dict)}
        req = urllib2_Request(url, urllib_urlencode(up))
        res = jabber_crypt.loads(urllib2_unquote(urllib2_urlopen(req).read().encode("utf-8")))

        if res == "good":
            return 1, False
        elif res == "bad":
            return 0, "Unauthorized"
        else:
            return 0, "error"

    except urllib2_HTTPError, e:
        if e.code == 401:
            return 0, _("Invalid credentials. Please try again") + "."
        error = "(HTTP code: " + str(e.code) + ")"