worldcompany / djangoembed

rich media consuming and providing with django
http://djangoembed.readthedocs.org
MIT License
138 stars 38 forks source link

OEmbedResource.create() fails on '%' in width value returned by SoundCloud oembed response #34

Open ghing opened 12 years ago

ghing commented 12 years ago

Calling the SoundCloud endpoint with minimal parameters returns a width="100%" in the response. For example http://soundcloud.com/oembed?url=http://soundcloud.com/ghing/the-calling-for-diychi-comp&format=json produces this response:

{
    "version": 1,
    "type": "rich",
    "provider_name": "SoundCloud",
    "provider_url": "http://soundcloud.com",
    "height": 166,
    "width": "100%",
    "title": "The Calling (for DIYCHI comp) by ghing",
    "description": "Finished version of \"The Calling\" for the DIY CHI comp",
    "html": "<iframe width=\"100%\" height=\"166\" scrolling=\"no\" frameborder=\"no\" src=\"http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F27467434&show_artwork=true\"></iframe>"
}

I'm not sure whether or not this adheres to the oEmbed spec, but it's a popular service, so it should be handled.

The problem is in these lines in OEmbedResource.create():

data['width'] = data.get('width') and int(data['width']) or None
data['height'] = data.get('height') and int(data['height']) or None

the call to int(data['width']) raises a ValueError because of the '%' in data['width']

A simple workaround would be to wrap those lines in a try/except like this:

try:
    data['width'] = data.get('width') and int(data['width']) or None
except ValueError:
   data['width'] = None
try:
    data['height'] = data.get('height') and int(data['height']) or None
except ValueError:
    data['height'] = None