Пример #1
0
    def get_resource(self, url, **kwargs):
        """
        Obtain an OEmbed resource JSON

        :param string url: Requested rich content URL
        :param kwargs: Optional arguments along with this request.
                       Currently only ``maxwidth`` and ``maxheight`` supported.
        :returns: :class:`monocle.resources.Resource`

        .. note::
            Currently only JSON-compatible requests are honored. If a request is
            made for an XML resource, it will still return a JSON resource
        """
        params = kwargs
        params['url'] = url

        # Only support JSON format
        params['format'] = 'json'

        request_url = self.get_request_url(**params)
        logger.info('Obtaining OEmbed resource at %s' % request_url)

        cached, primed = cache.get_or_prime(request_url, primer=Resource(params['url']))

        if primed or cached.is_stale:
            # Prevent many tasks being issued
            if cached.is_stale:
                cache.set(request_url, cached.refresh())

            request_external_oembed.apply_async((request_url,))
            logger.info('Scheduled external request for OEmbed resource %s' % url)

        return cached
Пример #2
0
    def get_resource(self, url, **kwargs):
        self._params = kwargs
        self._params['url'] = url

        # Only support JSON format
        self._params['format'] = 'json'

        if settings.CACHE_INTERNAL_PROVIDERS:
            cache_key = self.get_request_url(**self._params)
            logger.debug('Checking InternalProvider cache for key %s' % cache_key)
            cached, primed = cache.get_or_prime(cache_key, primer=Resource(url))

            if primed or cached.is_stale:
                logger.debug('Rebuilding new or stale internal provider resource at %s' % url)
                # This is just a safeguard in case the rebuild takes a little time
                if cached.is_stale:
                    cache.set(cache_key, cached.refresh())

                cached = self._build_resource(**self._params)
                cache.set(cache_key, cached)

            return cached

        # No caching, build directly
        return self._build_resource(**self._params)
Пример #3
0
    def test_cache_hit_signal(self):
        cb = mock_receiver()
        cache_hit.connect(cb)

        cache.set('foo', 'bar')
        cached, primed = cache.get_or_prime('foo', 'bar')

        self.assertFalse(primed)
        self.assertTrue(cb.called)
Пример #4
0
    def test_get_or_prime_does_not_prime(self):
        cache.set('foo', 'bar')

        cached, primed = cache.get_or_prime('foo', primer='baz')
        self.assertFalse(primed)
        self.assertEqual(cached, 'bar')
Пример #5
0
        try:
            request = urllib2.urlopen(req, timeout=settings.HTTP_TIMEOUT)
        except urllib2.HTTPError, e:
            logger.error('Failed to obtain %s : Status %s' % (url, e.code))
        except urllib2.URLError, e:
            if 'timed out' in str(e):
                # On a timeout, retry in hopes that it won't next time
                self.retry(args=[url], exc=e)
            else:
                logger.exception('Unexeped error when retrieving OEmbed %s' % url)
        else:
            if request.getcode() != 200:
                logger.error('URL %s resulted in unexpected HTTP status' % (url, request.getcode()))
            else:
                original_url = extract_content_url(url)

                try:
                    # TODO: Any validation that should happen here?
                    # Do we store invalid data? If invalid do we clear the cache?
                    data = json.loads(request.read())
                except ValueError:
                    logger.error('OEmbed response from %s contains invalid JSON' % url)
                else:
                    # Update the cache with this data
                    cache.set(url, Resource(original_url, data))
                finally:
                    request.close()


request_external_oembed = registry.tasks[RequestExternalOEmbedTask.name]