예제 #1
0
def embed(request, slug):
    try:
        import oembed
    except:
        raise NotImplementedError
    oembed.autodiscover()
    
    from oembed.consumer import OEmbedConsumer
    client      =   OEmbedConsumer()
    embedded    =   client.parse_text(request.build_absolute_uri().replace('/embed/', '/').replace(request.get_host(), Site.objects.get_current().domain))
    
    return HttpResponse(embedded)
예제 #2
0
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')
예제 #3
0
class ConsumerTestCase(BaseOEmbedTestCase):
    def setUp(self):
        "Set up test environment"
        super(ConsumerTestCase, self).setUp()
        self.oembed_client = OEmbedConsumer()

    def test_parse_text(self):
        consumed = self.oembed_client.parse_text(self.category_url)
        self.assertEqual(consumed, self.category_embed)
    
    def test_parse_html(self):
        consumed = self.oembed_client.parse_html('<p>%s</p>' % self.category_url)
        self.assertEqual(consumed, '<p>%s</p>' % self.category_embed)
    
    def test_extract_oembeds(self):
        embeds = self.oembed_client.extract_oembeds(self.category_url)
        self.assertEqual(len(embeds), 1)
        self.assertEqual(embeds[0]['original_url'], self.category_url)
        
        embeds = self.oembed_client.extract_oembeds(self.category_url, resource_type='photo')
        self.assertEqual(len(embeds), 1)
        self.assertEqual(embeds[0]['original_url'], self.category_url)
        
        embeds = self.oembed_client.extract_oembeds(self.category_url, resource_type='video')
        self.assertEqual(len(embeds), 0)
    
    def test_extract_oembeds_html(self):
        embeds = self.oembed_client.extract_oembeds_html('<p>%s</p>' % self.category_url)
        self.assertEqual(len(embeds), 1)
        self.assertEqual(embeds[0]['original_url'], self.category_url)
        
        embeds = self.oembed_client.extract_oembeds_html('<p>%s</p>' % self.category_url, resource_type='photo')
        self.assertEqual(len(embeds), 1)
        self.assertEqual(embeds[0]['original_url'], self.category_url)
        
        embeds = self.oembed_client.extract_oembeds_html('<p>%s</p>' % self.category_url, resource_type='video')
        self.assertEqual(len(embeds), 0)
        
        embeds = self.oembed_client.extract_oembeds_html('<p><a href="%s">Some link</a></p>' % self.category_url)
        self.assertEqual(len(embeds), 0)
        
        embeds = self.oembed_client.extract_oembeds_html('<p><a href="/some-link/">%s</a></p>' % self.category_url)
        self.assertEqual(len(embeds), 0)
예제 #4
0
파일: views.py 프로젝트: BergSoft/djbookru
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(simplejson.dumps(output), mimetype='application/json')
예제 #5
0
class ConsumerTestCase(BaseOEmbedTestCase):
    def setUp(self):
        "Set up test environment"
        super(ConsumerTestCase, self).setUp()
        self.oembed_client = OEmbedConsumer()

    def test_parse_text(self):
        consumed = self.oembed_client.parse_text(self.category_url)
        self.assertEqual(consumed, self.category_embed)
    
    def test_parse_html(self):
        consumed = self.oembed_client.parse_html('<p>%s</p>' % self.category_url)
        self.assertEqual(consumed, '<p>%s</p>' % self.category_embed)
    
    def test_extract_oembeds(self):
        embeds = self.oembed_client.extract_oembeds(self.category_url)
        self.assertEqual(len(embeds), 1)
        self.assertEqual(embeds[0]['original_url'], self.category_url)
        
        embeds = self.oembed_client.extract_oembeds(self.category_url, resource_type='photo')
        self.assertEqual(len(embeds), 1)
        self.assertEqual(embeds[0]['original_url'], self.category_url)
        
        embeds = self.oembed_client.extract_oembeds(self.category_url, resource_type='video')
        self.assertEqual(len(embeds), 0)
    
    def test_extract_oembeds_html(self):
        embeds = self.oembed_client.extract_oembeds_html('<p>%s</p>' % self.category_url)
        self.assertEqual(len(embeds), 1)
        self.assertEqual(embeds[0]['original_url'], self.category_url)
        
        embeds = self.oembed_client.extract_oembeds_html('<p>%s</p>' % self.category_url, resource_type='photo')
        self.assertEqual(len(embeds), 1)
        self.assertEqual(embeds[0]['original_url'], self.category_url)
        
        embeds = self.oembed_client.extract_oembeds_html('<p>%s</p>' % self.category_url, resource_type='video')
        self.assertEqual(len(embeds), 0)
        
        embeds = self.oembed_client.extract_oembeds_html('<p><a href="%s">Some link</a></p>' % self.category_url)
        self.assertEqual(len(embeds), 0)
        
        embeds = self.oembed_client.extract_oembeds_html('<p><a href="/some-link/">%s</a></p>' % self.category_url)
        self.assertEqual(len(embeds), 0)
    
    def test_strip(self):
        test_string = 'testing [%s] [http://www.google.com]' % self.category_url
        expected = 'testing [] [http://www.google.com]'
        
        self.assertEqual(self.oembed_client.strip(test_string), expected)
        
        # with width & height
        self.assertEqual(self.oembed_client.strip(test_string, 600, 400), expected)
        
        # with resource_type
        self.assertEqual(self.oembed_client.strip(test_string, resource_type='photo'), expected)
        self.assertEqual(self.oembed_client.strip(test_string, resource_type='link'), test_string)
    
    def test_strip_html(self):
        test_string = '<a href="%(match)s">%(match)s</a> <p>%(no_match)s</p>' % \
            {'match': self.category_url, 'no_match': 'http://www.google.com'}
        expected = test_string
        
        self.assertEqual(self.oembed_client.strip(test_string), expected)
    
    def test_strip_html_failure(self):
        # show how strip can fail when handling html - it picks up the match
        # in the p tag then replaces it everywhere, including in the a tags
        test_string = '<a href="%(match)s">%(match)s</a> <p>%(match)s</p> <p>%(no_match)s</p>' % \
            {'match': self.category_url, 'no_match': 'http://www.google.com'}
        expected = test_string
        actual = '<a href=""></a> <p></p> <p>http://www.google.com</p>'
        
        self.assertEqual(self.oembed_client.strip(test_string), actual)
예제 #6
0
class ConsumerTestCase(BaseOEmbedTestCase):
    def setUp(self):
        "Set up test environment"
        super(ConsumerTestCase, self).setUp()
        self.oembed_client = OEmbedConsumer()

    def test_parse_text(self):
        consumed = self.oembed_client.parse_text(self.category_url)
        self.assertEqual(consumed, self.category_embed)

    def test_parse_html(self):
        consumed = self.oembed_client.parse_html('<p>%s</p>' %
                                                 self.category_url)
        self.assertEqual(consumed, '<p>%s</p>' % self.category_embed)

    def test_extract_oembeds(self):
        embeds = self.oembed_client.extract_oembeds(self.category_url)
        self.assertEqual(len(embeds), 1)
        self.assertEqual(embeds[0]['original_url'], self.category_url)

        embeds = self.oembed_client.extract_oembeds(self.category_url,
                                                    resource_type='photo')
        self.assertEqual(len(embeds), 1)
        self.assertEqual(embeds[0]['original_url'], self.category_url)

        embeds = self.oembed_client.extract_oembeds(self.category_url,
                                                    resource_type='video')
        self.assertEqual(len(embeds), 0)

    def test_extract_oembeds_html(self):
        embeds = self.oembed_client.extract_oembeds_html('<p>%s</p>' %
                                                         self.category_url)
        self.assertEqual(len(embeds), 1)
        self.assertEqual(embeds[0]['original_url'], self.category_url)

        embeds = self.oembed_client.extract_oembeds_html('<p>%s</p>' %
                                                         self.category_url,
                                                         resource_type='photo')
        self.assertEqual(len(embeds), 1)
        self.assertEqual(embeds[0]['original_url'], self.category_url)

        embeds = self.oembed_client.extract_oembeds_html('<p>%s</p>' %
                                                         self.category_url,
                                                         resource_type='video')
        self.assertEqual(len(embeds), 0)

        embeds = self.oembed_client.extract_oembeds_html(
            '<p><a href="%s">Some link</a></p>' % self.category_url)
        self.assertEqual(len(embeds), 0)

        embeds = self.oembed_client.extract_oembeds_html(
            '<p><a href="/some-link/">%s</a></p>' % self.category_url)
        self.assertEqual(len(embeds), 0)

    def test_strip(self):
        test_string = 'testing [%s] [http://www.google.com]' % self.category_url
        expected = 'testing [] [http://www.google.com]'

        self.assertEqual(self.oembed_client.strip(test_string), expected)

        # with width & height
        self.assertEqual(self.oembed_client.strip(test_string, 600, 400),
                         expected)

        # with resource_type
        self.assertEqual(
            self.oembed_client.strip(test_string, resource_type='photo'),
            expected)
        self.assertEqual(
            self.oembed_client.strip(test_string, resource_type='link'),
            test_string)

    def test_strip_html(self):
        test_string = '<a href="%(match)s">%(match)s</a> <p>%(no_match)s</p>' % \
            {'match': self.category_url, 'no_match': 'http://www.google.com'}
        expected = test_string

        self.assertEqual(self.oembed_client.strip(test_string), expected)

    def test_strip_html_failure(self):
        # show how strip can fail when handling html - it picks up the match
        # in the p tag then replaces it everywhere, including in the a tags
        test_string = '<a href="%(match)s">%(match)s</a> <p>%(match)s</p> <p>%(no_match)s</p>' % \
            {'match': self.category_url, 'no_match': 'http://www.google.com'}
        expected = test_string
        actual = '<a href=""></a> <p></p> <p>http://www.google.com</p>'

        self.assertEqual(self.oembed_client.strip(test_string), actual)