annotate oembed/views.py @ 876:07e4622f7aef

Add 2014 MP3 comp link to sidebar block.
author Brian Neal <bgneal@gmail.com>
date Sat, 27 Dec 2014 12:53:22 -0600
parents 89b240fe9297
children 344f7914d421
rev   line source
bgneal@285 1 """
bgneal@285 2 Views for the oembed application.
bgneal@679 3
bgneal@285 4 """
bgneal@679 5 import json
bgneal@285 6 import re
bgneal@285 7
bgneal@285 8 from django.http import HttpResponse
bgneal@285 9 from django.http import HttpResponseBadRequest
bgneal@285 10 from django.http import HttpResponseForbidden
bgneal@361 11 from django.conf import settings
bgneal@285 12
bgneal@285 13 from oembed.models import Provider
bgneal@285 14 from oembed.models import Oembed
bgneal@285 15 from oembed.core import get_oembed
bgneal@285 16
bgneal@285 17
bgneal@285 18 def fetch_media(request):
bgneal@285 19 """
bgneal@286 20 This view returns the HTML media of an embeddable resource as
bgneal@286 21 JSON. This view is the target of an AJAX request.
bgneal@285 22 """
bgneal@285 23 if not request.user.is_authenticated():
bgneal@285 24 return HttpResponseForbidden('Please login or register.')
bgneal@285 25
bgneal@285 26 url = request.POST.get('q')
bgneal@285 27
bgneal@285 28 if not url:
bgneal@285 29 return HttpResponseBadRequest('Please provide a valid URL.')
bgneal@285 30
bgneal@285 31 # Is this already in our database?
bgneal@285 32 try:
bgneal@285 33 oembed = Oembed.objects.get(url=url)
bgneal@285 34 except Oembed.DoesNotExist:
bgneal@285 35 pass
bgneal@285 36 else:
bgneal@285 37 data = dict(id=oembed.id, embed=oembed.html)
bgneal@285 38 return HttpResponse(json.dumps(data), content_type='application/json')
bgneal@285 39
bgneal@285 40 # It isn't in the database, try to find it from our providers
bgneal@285 41 providers = Provider.objects.all()
bgneal@285 42 for provider in providers:
bgneal@285 43 if re.match(provider.url_regex, url):
bgneal@285 44 try:
bgneal@361 45 data = get_oembed(provider.api_endpoint,
bgneal@361 46 url,
bgneal@361 47 maxwidth=settings.OEMBED_MAXWIDTH,
bgneal@361 48 maxheight=settings.OEMBED_MAXHEIGHT)
bgneal@285 49 except IOError, e:
bgneal@285 50 return HttpResponseBadRequest(
bgneal@285 51 "Sorry, we could not retrieve your video (%s)" % e)
bgneal@285 52
bgneal@285 53 if 'type' not in data or data['type'] != 'video':
bgneal@285 54 return HttpResponseBadRequest(
bgneal@285 55 "Hey, this doesn't look like a video..??")
bgneal@285 56
bgneal@285 57 oembed = Oembed(url=url,
bgneal@285 58 type=Oembed.VIDEO,
bgneal@285 59 title=data.get('title', ''),
bgneal@285 60 width=int(data.get('width', 0)),
bgneal@285 61 height=int(data.get('height', 0)),
bgneal@285 62 html=data.get('html', ''))
bgneal@285 63 oembed.save()
bgneal@285 64
bgneal@285 65 data = dict(id=oembed.id, embed=oembed.html)
bgneal@285 66 return HttpResponse(json.dumps(data),
bgneal@285 67 content_type='application/json')
bgneal@285 68
bgneal@287 69 return HttpResponseBadRequest("Sorry, we couldn't find that video.")
bgneal@286 70
bgneal@286 71
bgneal@286 72 def fetch_saved_media(request):
bgneal@286 73 """
bgneal@286 74 This view returns the HTML embed information for previously saved Oembed
bgneal@286 75 objects as JSON. This view is the target of an AJAX request.
bgneal@286 76 """
bgneal@286 77 if not request.user.is_authenticated():
bgneal@286 78 return HttpResponseForbidden('Please login or register.')
bgneal@286 79
bgneal@286 80 embed_ids = request.GET.getlist('embeds')
bgneal@286 81 if not embed_ids:
bgneal@286 82 return HttpResponseBadRequest('Missing embed list.')
bgneal@286 83
bgneal@286 84 embeds = Oembed.objects.in_bulk(embed_ids)
bgneal@286 85
bgneal@286 86 # build results in order
bgneal@286 87 results = []
bgneal@286 88 for pk in embeds:
bgneal@286 89 results.append(dict(id=pk, html=embeds[pk].html))
bgneal@286 90
bgneal@286 91 return HttpResponse(json.dumps(results), content_type='application/json')