bgneal@285: """ bgneal@285: Views for the oembed application. bgneal@285: """ bgneal@285: import re bgneal@285: import urllib2 bgneal@285: bgneal@285: from django.http import HttpResponse bgneal@285: from django.http import HttpResponseBadRequest bgneal@285: from django.http import HttpResponseForbidden bgneal@285: import django.utils.simplejson as json bgneal@285: bgneal@285: from oembed.models import Provider bgneal@285: from oembed.models import Oembed bgneal@285: from oembed.core import get_oembed bgneal@285: bgneal@285: bgneal@285: def fetch_media(request): bgneal@285: """ bgneal@286: This view returns the HTML media of an embeddable resource as bgneal@286: JSON. This view is the target of an AJAX request. bgneal@285: """ bgneal@285: if not request.user.is_authenticated(): bgneal@285: return HttpResponseForbidden('Please login or register.') bgneal@285: bgneal@285: url = request.POST.get('q') bgneal@285: bgneal@285: if not url: bgneal@285: return HttpResponseBadRequest('Please provide a valid URL.') bgneal@285: bgneal@285: # Is this already in our database? bgneal@285: try: bgneal@285: oembed = Oembed.objects.get(url=url) bgneal@285: except Oembed.DoesNotExist: bgneal@285: pass bgneal@285: else: bgneal@285: data = dict(id=oembed.id, embed=oembed.html) bgneal@285: return HttpResponse(json.dumps(data), content_type='application/json') bgneal@285: bgneal@285: # It isn't in the database, try to find it from our providers bgneal@285: providers = Provider.objects.all() bgneal@285: for provider in providers: bgneal@285: if re.match(provider.url_regex, url): bgneal@285: try: bgneal@285: data = get_oembed(provider.api_endpoint, url) bgneal@285: except IOError, e: bgneal@285: return HttpResponseBadRequest( bgneal@285: "Sorry, we could not retrieve your video (%s)" % e) bgneal@285: bgneal@285: if 'type' not in data or data['type'] != 'video': bgneal@285: return HttpResponseBadRequest( bgneal@285: "Hey, this doesn't look like a video..??") bgneal@285: bgneal@285: oembed = Oembed(url=url, bgneal@285: type=Oembed.VIDEO, bgneal@285: title=data.get('title', ''), bgneal@285: width=int(data.get('width', 0)), bgneal@285: height=int(data.get('height', 0)), bgneal@285: html=data.get('html', '')) bgneal@285: oembed.save() bgneal@285: bgneal@285: data = dict(id=oembed.id, embed=oembed.html) bgneal@285: return HttpResponse(json.dumps(data), bgneal@285: content_type='application/json') bgneal@285: bgneal@287: return HttpResponseBadRequest("Sorry, we couldn't find that video.") bgneal@286: bgneal@286: bgneal@286: def fetch_saved_media(request): bgneal@286: """ bgneal@286: This view returns the HTML embed information for previously saved Oembed bgneal@286: objects as JSON. This view is the target of an AJAX request. bgneal@286: """ bgneal@286: if not request.user.is_authenticated(): bgneal@286: return HttpResponseForbidden('Please login or register.') bgneal@286: bgneal@286: embed_ids = request.GET.getlist('embeds') bgneal@286: if not embed_ids: bgneal@286: return HttpResponseBadRequest('Missing embed list.') bgneal@286: bgneal@286: embeds = Oembed.objects.in_bulk(embed_ids) bgneal@286: bgneal@286: # build results in order bgneal@286: results = [] bgneal@286: for pk in embeds: bgneal@286: results.append(dict(id=pk, html=embeds[pk].html)) bgneal@286: bgneal@286: return HttpResponse(json.dumps(results), content_type='application/json')