def extract_oembeds(text, args=None): """ Extract oembed resources from a block of text. Returns a list of dictionaries. Max width & height can be specified: {% for embed in block_of_text|extract_oembeds:"400x300" %} Resource type can be specified: {% for photo_embed in block_of_text|extract_oembeds:"photo" %} Or both: {% for embed in block_of_text|extract_oembeds:"400x300xphoto" %} """ resource_type = width = height = None if args: dimensions = args.lower().split('x') if len(dimensions) in (3, 1): resource_type = dimensions.pop() if len(dimensions) == 2: width, height = map(lambda x: int(x), dimensions) client = OEmbedConsumer() return client.extract(text, width, height, resource_type)
def oembed_filter(input, args=None): if args: dimensions = args.lower().split('x') if len(dimensions) != 2: raise template.TemplateSyntaxError( "Usage: [width]x[height], e.g. 600x400") width, height = map(int, dimensions) else: width = height = None client = OEmbedConsumer() return mark_safe(client.parse(input, width, height))
def render(self, context): kwargs = {} if self.width and self.height: kwargs['maxwidth'] = self.width kwargs['maxheight'] = self.height kwargs['template_dir'] = self.template_dir kwargs['context'] = context client = OEmbedConsumer() parsed = client.parse(self.nodelist.render(context), **kwargs) if self.var_name: context[self.var_name] = parsed return '' else: return parsed
def consume_json(request): """ Extract and return oembed content for given urls. Required GET params: urls - list of urls to consume Optional GET params: width - maxwidth attribute for oembed content height - maxheight attribute for oembed content template_dir - template_dir to use when rendering oembed Returns: list of dictionaries with oembed metadata and renderings, json encoded """ client = OEmbedConsumer() urls = request.GET.getlist('urls') width = request.GET.get('width') height = request.GET.get('height') template_dir = request.GET.get('template_dir') output = {} ctx = RequestContext(request) for url in urls: try: provider = oembed.site.provider_for_url(url) except OEmbedMissingEndpoint: oembeds = None rendered = None else: oembeds = url rendered = client.parse_text(url, width, height, context=ctx, template_dir=template_dir) output[url] = { 'oembeds': oembeds, 'rendered': rendered, } return HttpResponse(json.dumps(output), mimetype='application/json')
def strip_oembeds(text, args=None): """ Take a block of text and strip all the embeds from it, optionally taking a maxwidth, maxheight / resource_type Usage: {{ post.content|strip_embeds }} {{ post.content|strip_embeds:"600x600xphoto" }} {{ post.content|strip_embeds:"video" }} """ resource_type = width = height = None if args: dimensions = args.lower().split('x') if len(dimensions) in (3, 1): resource_type = dimensions.pop() if len(dimensions) == 2: width, height = map(lambda x: int(x), dimensions) client = OEmbedConsumer() return mark_safe(client.strip(text, width, height, resource_type))
from django.db import models from django.core.urlresolvers import reverse from django.utils.translation import ugettext as _ from django.contrib.contenttypes import generic from django.contrib.contenttypes.models import ContentType from django.contrib.sites.models import Site from django.core.exceptions import ValidationError import oembed from oembed.consumer import OEmbedConsumer from polymorphic import PolymorphicModel from autoslug import AutoSlugField from filebrowser.fields import FileBrowseField oembed_consumer = OEmbedConsumer() class Attachment(PolymorphicModel): class Meta: ordering = ('-creation', ) oembed = models.CharField(max_length=100, verbose_name=_('oembed')) creation = models.DateTimeField(editable=False, auto_now=True) slug = AutoSlugField(populate_from='_slug', unique=True) @models.permalink def get_absolute_url(self): return 'attachments:detail', (), {'slug': self.slug} def clean(self):
def oembed_replace(text): consumer = OEmbedConsumer() return consumer.parse(text)
def setUp(self): "Set up test environment" super(ConsumerTestCase, self).setUp() self.oembed_client = OEmbedConsumer()