annotate gpp/oembed/core.py @ 285:8fd4984d5c3b
This is a first rough commit for #95, adding the ability to embed YouTube videos in forum posts. Some more polish and testing needs to happen at this point. I wanted to get all these changes off my hard drive and into the repository.
author |
Brian Neal <bgneal@gmail.com> |
date |
Thu, 14 Oct 2010 02:39:35 +0000 |
parents |
|
children |
6d6fdc58487c |
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@285
|
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@285
|
46 with gzip.GzipFile(fileobj=StringIO(result)) as f:
|
bgneal@285
|
47 result = f.read()
|
bgneal@285
|
48
|
bgneal@285
|
49 return json.loads(result)
|
bgneal@285
|
50
|
bgneal@285
|
51 if __name__ == "__main__":
|
bgneal@285
|
52 try:
|
bgneal@285
|
53 print get_oembed("http://www.youtube.com/oembed",
|
bgneal@285
|
54 #"http://www.youtube.com/watch?v=7_IMzJldOf4")
|
bgneal@285
|
55 "http://www.youtube.com/watch?v=99999999999")
|
bgneal@285
|
56 except urllib2.HTTPError, e:
|
bgneal@285
|
57 print e
|