python-oembed icon indicating copy to clipboard operation
python-oembed copied to clipboard

How to use python-oembed to automatically choose an endpoint

Open thapar opened this issue 12 years ago • 4 comments

As per https://github.com/abarmat/python-oembed#use, 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 http://www.flickr.com/photos/wizardbt/2584979382/ 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. --> http://stackoverflow.com/questions/16353231/how-to-use-python-oembed-to-automatically-choose-an-endpoint

thapar avatar May 03 '13 16:05 thapar

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"],
        }[opt['format']]

        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(resp.read(), 'html5lib')

        def _type_matches(typ):
            return typ in types

        links = soup.find_all('link', rel="alternate", type=_type_matches)
        try:
            return links[0]['href']
        except IndexError:
            return None

sk1p avatar Mar 25 '16 14:03 sk1p

I think it would be super handy if autodiscovery was part of python-oembed itself.

edsu avatar Apr 30 '19 10:04 edsu

Although I guess it ought to be possible to use the provider registry to prevent unnecessary HTML fetches?

https://github.com/iamcal/oembed/tree/master/providers

edsu avatar Apr 30 '19 10:04 edsu

Maybe somebody has already done it, but I created a little wrapper around python-oembed that uses the oembed provider registry to build a consumer with all the providers.

https://github.com/edsu/oembedders#readme

edsu avatar Apr 30 '19 19:04 edsu