How to use python-oembed to automatically choose an endpoint
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
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
I think it would be super handy if autodiscovery was part of python-oembed itself.
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
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