annotate gpp/oembed/views.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 72fd300685d5
rev   line source
bgneal@285 1 """
bgneal@285 2 Views for the oembed application.
bgneal@285 3 """
bgneal@285 4 import re
bgneal@285 5 import urllib2
bgneal@285 6
bgneal@285 7 from django.http import HttpResponse
bgneal@285 8 from django.http import HttpResponseBadRequest
bgneal@285 9 from django.http import HttpResponseForbidden
bgneal@285 10 import django.utils.simplejson as json
bgneal@285 11
bgneal@285 12 from oembed.models import Provider
bgneal@285 13 from oembed.models import Oembed
bgneal@285 14 from oembed.core import get_oembed
bgneal@285 15
bgneal@285 16
bgneal@285 17 def fetch_media(request):
bgneal@285 18 """
bgneal@285 19 This view returns the HTML media of an embeddable resource.
bgneal@285 20 This view is the target of an AJAX request.
bgneal@285 21 """
bgneal@285 22 if not request.user.is_authenticated():
bgneal@285 23 return HttpResponseForbidden('Please login or register.')
bgneal@285 24
bgneal@285 25 url = request.POST.get('q')
bgneal@285 26
bgneal@285 27 if not url:
bgneal@285 28 return HttpResponseBadRequest('Please provide a valid URL.')
bgneal@285 29
bgneal@285 30 # Is this already in our database?
bgneal@285 31 try:
bgneal@285 32 oembed = Oembed.objects.get(url=url)
bgneal@285 33 except Oembed.DoesNotExist:
bgneal@285 34 pass
bgneal@285 35 else:
bgneal@285 36 data = dict(id=oembed.id, embed=oembed.html)
bgneal@285 37 return HttpResponse(json.dumps(data), content_type='application/json')
bgneal@285 38
bgneal@285 39 # It isn't in the database, try to find it from our providers
bgneal@285 40 providers = Provider.objects.all()
bgneal@285 41 for provider in providers:
bgneal@285 42 if re.match(provider.url_regex, url):
bgneal@285 43 try:
bgneal@285 44 data = get_oembed(provider.api_endpoint, url)
bgneal@285 45 except IOError, e:
bgneal@285 46 return HttpResponseBadRequest(
bgneal@285 47 "Sorry, we could not retrieve your video (%s)" % e)
bgneal@285 48
bgneal@285 49 if 'type' not in data or data['type'] != 'video':
bgneal@285 50 return HttpResponseBadRequest(
bgneal@285 51 "Hey, this doesn't look like a video..??")
bgneal@285 52
bgneal@285 53 oembed = Oembed(url=url,
bgneal@285 54 type=Oembed.VIDEO,
bgneal@285 55 title=data.get('title', ''),
bgneal@285 56 width=int(data.get('width', 0)),
bgneal@285 57 height=int(data.get('height', 0)),
bgneal@285 58 html=data.get('html', ''))
bgneal@285 59 oembed.save()
bgneal@285 60
bgneal@285 61 data = dict(id=oembed.id, embed=oembed.html)
bgneal@285 62 return HttpResponse(json.dumps(data),
bgneal@285 63 content_type='application/json')
bgneal@285 64
bgneal@285 65 return HttpBadRequest("Sorry, we couldn't find that video.")