예제 #1
0
def filter_urlquery(url, keys=[], keys_status=False):
    """Removes unwanted urlquerys

    :param url: an URL
    :param keys: list of query names
    :param keys_status: False = removes querys that are in keys
                        True = allow only querys that are in keys
    :return: URL with filtered query
    """
    parts = urlparse(url)
    query_dict = dict(parse_qsl(parts.query))
    new_query_dict = {}

    for key in keys:
        try:
            if keys_status is True:
                new_query_dict[key] = query_dict[key]
            else:
                del query_dict[key]
        except KeyError:
            continue

    new_parts = list(parts)
    if keys_status is True:
        new_parts[4] = unquote(urlencode(new_query_dict))
    else:
        new_parts[4] = unquote(urlencode(query_dict))
    url = urlunparse(new_parts)
    return url
예제 #2
0
def update_scheme(current, target):
    """
    Take the scheme from the current URL and applies it to the
    target URL if the target URL startswith // or is missing a scheme
    :param current: current URL
    :param target: target URL
    :return: target URL with the current URLs scheme
    """
    target_p = urlparse(target)
    if not target_p.scheme and target_p.netloc:
        return "{0}:{1}".format(urlparse(current).scheme, urlunparse(target_p))
    elif not target_p.scheme and not target_p.netloc:
        return "{0}://{1}".format(
            urlparse(current).scheme, urlunparse(target_p))
    else:
        return target
예제 #3
0
    def auth_url(self, url):
        parsed = urlparse(url)
        path, _ = parsed.path.rsplit("/", 1)
        token_res = http.get(self.token_url, params=dict(acl=path + "/*"))
        authparams = http.json(token_res, schema=self.token_schema)

        existing = dict(parse_qsl(parsed.query))
        existing.update(dict(parse_qsl(authparams)))

        return urlunparse(parsed._replace(query=urlencode(existing)))
예제 #4
0
    def get_stream_url(self, event_id):
        url_m = self.url_re.match(self.url)
        site = url_m.group(1) or url_m.group(2)
        api_url = self.api_url.format(id=event_id, site=site.upper())
        self.logger.debug("Calling API: {0}", api_url)

        stream_url = http.get(api_url).text.strip("\"'")

        parsed = urlparse(stream_url)
        query = dict(parse_qsl(parsed.query))
        return urlunparse(parsed._replace(query="")), query
예제 #5
0
def filter_urlquery(url, keys=[], keys_status=False, new_dict={}):
    """manipulate parameters from an url

    Examples:

        All Examples uses this url.

        url = "http://example.com/z/manifest.f4m?FOO=BAR&n=20&b=1896"

        1. allows only specified parameter and remove all other

            filter_urlquery(url, ["FOO"], True)

            http://example.com/z/manifest.f4m?FOO=BAR

        2. same as 1. and add's a custom parameter

            filter_urlquery(url, ["FOO"], True, {'2FOO2': '2BAR2'})

            http://example.com/z/manifest.f4m?FOO=BAR&2FOO2=2BAR2

        3. remove only specified parameter

            filter_urlquery(url, ["FOO"], False)

            http://example.com/z/manifest.f4m?n=20&b=1896

        4. remove all parameter

            filter_urlquery(url, keys_status=True)

            http://example.com/z/manifest.f4m

        5. add new parameter

            filter_urlquery(url, new_dict={'QFOO': 'QBAR', 'AFOO': 'ABAR'})

            http://example.com/z/manifest.f4m?FOO=BAR&n=20&b=1896&QFOO=QBAR&AFOO=ABAR

    :param url: an URL
    :param keys: list of query names
    :param keys_status: False = removes querys that are in keys
                        True = allow only querys that are in keys
    :param new_dict: dict of new custom urlquerys
    :return: URL with filtered query
    """
    parts = urlparse(url)
    query_dict = dict(parse_qsl(parts.query))
    new_query_dict = {}

    for key in keys:
        try:
            if keys_status is True:
                new_query_dict[key] = query_dict[key]
            else:
                del query_dict[key]
        except KeyError:
            continue

    new_parts = list(parts)
    if keys_status is True:
        query_dict = new_query_dict

    query_dict.update(new_dict)

    new_parts[4] = unquote(urlencode(query_dict))
    url = urlunparse(new_parts)
    return url