How to use python-oembed to automatically choose an endpoint #3

thapar opened this Issue May 3, 2013 · 1 comment


None yet
2 participants

thapar commented May 3, 2013

As per, I have to provide the endpoint for a provider. I want to know if there is a way to give python-oembed a url such as and have python-oembed determine the correct endpoint. That way, I can simply supply a picture/video's url, and receive the provider's response easily (I think adding the url to html's img tags works similarly).

If this is not possible, what's a good way to land at the endpoint for a picture/video url?

Aside: I previously asked this question on SO, and I think an answer there might be more useful in terms of future Googling. -->

@thapar thapar closed this May 3, 2013

@thapar thapar reopened this May 3, 2013


sk1p commented Mar 25, 2016

You can write your own OEmbedEndpoint that uses the <link rel="alternate" /> tags found on many sites. Here is what I'm using:

class AutoOEmbedEndpoint(oembed.OEmbedEndpoint):
    def __init__(self):
        super(AutoOEmbedEndpoint, self).__init__("")
        self._implicitFormat = False

    def match(self, url):
        return True  # assume a match.

    def get(self, url, **opt):
        types = {
            "json": ["application/json+oembed"],
            "xml": ["text/xml+oembed", "application/xml+oembed"],

        self._urlApi = self.autodetect(url, types)
        return super(AutoOEmbedEndpoint, self).get(url, **opt)

    def autodetect(self, url, types):
        resp = urllib.request.urlopen(url)
        soup = BeautifulSoup(, 'html5lib')

        def _type_matches(typ):
            return typ in types

        links = soup.find_all('link', rel="alternate", type=_type_matches)
            return links[0]['href']
        except IndexError:
            return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment