import requests
from bs4 import BeautifulSoup

class REmbedDiscoveryError(Exception):
    '''Thrown if there is an error discovering an OEmbed URL'''

def get_oembed_url(url, format='json'):
    type = __get_type(format)

    response = requests.get(url)

    if not response.ok:
        raise REmbedDiscoveryError('Failed to get %s (status code %s)' % (url, response.status_codes))

    soup = BeautifulSoup(requests.get(url).text)
    link = soup.find('link', type = type, href = True)

    return link['href'] if link else None

def __get_type(format):        
    if format == 'json':
        return 'application/json+oembed'
    elif format == 'xml':
        return 'text/xml+oembed'

    raise REmbedDiscoveryError('Invalid format %s specified (must be json or xml)' % format)
import requests
from bs4 import BeautifulSoup

class REmbedDiscoveryError(Exception):
    '''Thrown if there is an error discovering an OEmbed URL.'''

def get_oembed_url(url, format='json'):
    '''Retrieves the OEmbed URL for a given resource.

    :param url: resource URL.
    :param format: format to use for OEmbed.  One of 'json', 'xml'.
    :returns: OEmbed URL for the resource.
    :raises REmbedDiscoveryError: if there is an error getting the OEmbed URL.
    '''
    type = __get_type(format)

    response = requests.get(url)

    if not response.ok:
        raise REmbedDiscoveryError('Failed to get %s (status code %s)' % (url, response.status_codes))

    soup = BeautifulSoup(requests.get(url).text)
    link = soup.find('link', type = type, href = True)

    return link['href'] if link else None

def __get_type(format):        
    if format == 'json':
        return 'application/json+oembed'
    elif format == 'xml':
        return 'text/xml+oembed'

    raise REmbedDiscoveryError('Invalid format %s specified (must be json or xml)' % format)
Add documentation