annotate oembed/core.py @ 881:13f2d4393ec4

More work on ssl_images command. Uploading now works.
author Brian Neal <bgneal@gmail.com>
date Thu, 15 Jan 2015 21:05:05 -0600
parents 89b240fe9297
children 344f7914d421
rev   line source
bgneal@285 1 """
bgneal@285 2 This module contains core functionality for the oembed application.
bgneal@679 3
bgneal@285 4 """
bgneal@679 5 import json
bgneal@285 6 import urllib
bgneal@285 7 import urllib2
bgneal@285 8 import gzip
bgneal@285 9 try:
bgneal@285 10 from cStringIO import StringIO
bgneal@285 11 except ImportError:
bgneal@285 12 from StringIO import StringIO
bgneal@285 13
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