Mercurial > public > sg101
annotate gpp/oembed/core.py @ 487:77d878acea5e
For #233; add social media sharing buttons to podcast pages.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 19 Oct 2011 00:05:54 +0000 |
parents | 6d6fdc58487c |
children |
rev | line source |
---|---|
bgneal@285 | 1 """ |
bgneal@285 | 2 This module contains core functionality for the oembed application. |
bgneal@285 | 3 """ |
bgneal@285 | 4 from __future__ import with_statement |
bgneal@285 | 5 import urllib |
bgneal@285 | 6 import urllib2 |
bgneal@285 | 7 import gzip |
bgneal@285 | 8 try: |
bgneal@285 | 9 from cStringIO import StringIO |
bgneal@285 | 10 except ImportError: |
bgneal@285 | 11 from StringIO import StringIO |
bgneal@285 | 12 |
bgneal@285 | 13 import django.utils.simplejson as json |
bgneal@285 | 14 |
bgneal@285 | 15 |
bgneal@285 | 16 USER_AGENT = 'gremmies python oembed' |
bgneal@285 | 17 |
bgneal@285 | 18 |
bgneal@285 | 19 def get_oembed(api_endpoint, url, format='json', **opts): |
bgneal@285 | 20 """ |
bgneal@285 | 21 Perform the GET request to retrieve the embedded media data from the given |
bgneal@285 | 22 API endpoint for the given URL. Return the result as a Python dictionary. |
bgneal@285 | 23 |
bgneal@285 | 24 format specifies the response format, and should be 'json' or 'xml'. |
bgneal@285 | 25 opts are any additional GET options that should be present in the GET |
bgneal@285 | 26 request. |
bgneal@285 | 27 |
bgneal@285 | 28 """ |
bgneal@285 | 29 opts['url'] = url |
bgneal@285 | 30 opts['format'] = format |
bgneal@285 | 31 api_url = "%s?%s" % (api_endpoint, urllib.urlencode(opts)) |
bgneal@285 | 32 |
bgneal@285 | 33 headers = { |
bgneal@361 | 34 'User-Agent': USER_AGENT, |
bgneal@285 | 35 'Accept-Encoding': 'gzip', |
bgneal@285 | 36 } |
bgneal@285 | 37 request = urllib2.Request(api_url, headers=headers) |
bgneal@285 | 38 |
bgneal@285 | 39 opener = urllib2.build_opener() |
bgneal@285 | 40 f = opener.open(request) |
bgneal@285 | 41 headers = f.info() |
bgneal@285 | 42 result = f.read() |
bgneal@285 | 43 f.close() |
bgneal@285 | 44 |
bgneal@285 | 45 if headers.get('content-encoding') == 'gzip': |
bgneal@361 | 46 f = gzip.GzipFile(fileobj=StringIO(result)) |
bgneal@361 | 47 result = f.read() |
bgneal@361 | 48 f.close() |
bgneal@285 | 49 |
bgneal@285 | 50 return json.loads(result) |
bgneal@285 | 51 |
bgneal@285 | 52 if __name__ == "__main__": |
bgneal@285 | 53 try: |
bgneal@285 | 54 print get_oembed("http://www.youtube.com/oembed", |
bgneal@361 | 55 "http://www.youtube.com/watch?v=7_IMzJldOf4") |
bgneal@285 | 56 except urllib2.HTTPError, e: |
bgneal@285 | 57 print e |