예제 #1
0
def reconstruct_url(url: str, maxwidth: int = 640, maxheight: int = 480) -> str:
    # The following code is taken from
    # https://github.com/rafaelmartins/pyoembed/blob/master/pyoembed/__init__.py.
    # This is a helper function which will be indirectly use to mock the HTTP responses.
    provider = get_provider(str(url))
    oembed_url = provider.oembed_url(url)
    scheme, netloc, path, query_string, fragment = urlsplit(oembed_url)

    query_params = OrderedDict(parse_qsl(query_string))
    query_params["maxwidth"] = str(maxwidth)
    query_params["maxheight"] = str(maxheight)
    final_url = urlunsplit((scheme, netloc, path, urlencode(query_params, True), fragment))
    return final_url
예제 #2
0
파일: __init__.py 프로젝트: imclab/pyoembed
def oEmbed(url, maxwidth=None, maxheight=None):
    provider = get_provider(url)
    if provider is None:
        raise ProviderException('No provider found for url: %s' % url)
    oembed_url = provider.oembed_url(url)

    # lets parse url to append our own width/height parameters
    scheme, netloc, path, query_string, fragment = urlsplit(oembed_url)
    query_params = OrderedDict(parse_qsl(query_string))

    # append width/height parameters
    if maxwidth is not None:
        query_params['maxwidth'] = [int(maxwidth)]
    if maxheight is not None:
        query_params['maxheight'] = [int(maxheight)]

    # rebuild url
    final_url = urlunsplit((scheme, netloc, path,
                            urlencode(query_params, True), fragment))

    # do actual oEmbed request
    response = requests.get(final_url)
    if not response.ok:
        raise PyOembedException('Failed to do oEmbed request: %s' % url)

    # get content type
    content_type = response.headers.get('content-type', None)
    parser = get_parser(content_type)
    if parser is None:
        raise ParserException('Failed to find a parser for url: %s' % url)

    # parse oEmbed request
    data = parser.content_parse(response.text)

    # get data type
    data_type = get_data_type(data)
    if data_type is None:
        raise DataTypeException('Failed to find a data type for url: %s' % url)

    # validate data
    data_type.validate_data(data)

    # return data :)
    return data
예제 #3
0
파일: __init__.py 프로젝트: zulip/pyoembed
def oEmbed(url, maxwidth=None, maxheight=None):
    provider = get_provider(url)
    if provider is None:
        raise ProviderException('No provider found for url: %s' % url)
    oembed_url = provider.oembed_url(url)

    # lets parse url to append our own width/height parameters
    scheme, netloc, path, query_string, fragment = urlsplit(oembed_url)
    query_params = OrderedDict(parse_qsl(query_string))

    # append width/height parameters
    if maxwidth is not None:
        query_params['maxwidth'] = [int(maxwidth)]
    if maxheight is not None:
        query_params['maxheight'] = [int(maxheight)]

    # rebuild url
    final_url = urlunsplit(
        (scheme, netloc, path, urlencode(query_params, True), fragment))

    # do actual oEmbed request
    response = requests.get(final_url)
    if not response.ok:
        raise PyOembedException('Failed to do oEmbed request: %s' % url)

    # get content type
    content_type = response.headers.get('content-type', None)
    parser = get_parser(content_type)
    if parser is None:
        raise ParserException('Failed to find a parser for url: %s' % url)

    # parse oEmbed request
    data = parser.content_parse(response.text)

    # get data type
    data_type = get_data_type(data)
    if data_type is None:
        raise DataTypeException('Failed to find a data type for url: %s' % url)

    # validate data
    data_type.validate_data(data)

    # return data :)
    return data