Esempio n. 1
0
def readable(request):
    """This is the old api endpoint that returns json data.

    """
    url = request.params.get('url', None)
    LOG.debug('readable process, ' + url)

    if not url:
        LOG.debug('notfound,' + url)
        return HTTPNotFound()

    url = url.strip('/')
    LOG.debug('Checking url: ' + url)

    request.response.headers['Content-Type'] = \
        'application/json; charset="utf8"'

    # allow cross domain requests: xdr
    request.response.headers['Access-Control-Allow-Origin'] = '*'

    exists = WebPageMgr.exists(url=url)
    if exists:
        page = WebPageMgr.get(exists)
        return {
            'data': dict(page),
            'readable': page.readable,
        }
    else:
        LOG.debug('Does not Exist: ...fetching')
        read = ReadableRequest(url)
        read.process()

        if not read.is_error:
            page = WebPageMgr.store_request(read)

            return {
                'data': dict(page),
                'readable': page.readable
            }
        else:
            LOG.error('url_is_error,' + url)
            request.response.status_int = 500
            error_message = 'There was an error reading the page.'
            return {
                'error': error_message
            }
Esempio n. 2
0
def view(request):
    """This is the 'usable' endpoint that displays the trimmed content for
    reading.

    """
    # fetch download of the url
    url = request.params.get('url', None)
    LOG.debug('process, ' + url)

    if not url:
        LOG.debug('notfound,' + url)
        return HTTPNotFound()

    url = url.strip('/')
    LOG.debug('Checking url: ' + url)

    exists = WebPageMgr.exists(url=url)
    if exists:
        LOG.debug('Exists: ...forwarding')
        return HTTPFound(
            location=request.route_url('url', hash_id=exists))

    else:
        LOG.debug('Does not Exist: ...fetching')
        read = ReadableRequest(url)
        read.process()

        if not read.is_error:
            LOG.warning('writing it out')
            page = WebPageMgr.store_request(read)
            return HTTPFound(
                location=request.route_url('url', hash_id=page.hash_id))
        else:
            LOG.error('url_is_error,' + url)
            return render_to_response(
                'error.mako', {
                    'error_message': 'There was an error fetching the url.',
                    'error_details': {
                        'code': read.status_code,
                    },
                    'readable': read,
                    'title': 'Processing Error',
                },
                request=request
            )
Esempio n. 3
0
def api_parse(request):
    """Api to parse a url POST'd"""
    url = request.params.get('url', None)

    resp = request.response
    environ = request.environ
    resp.headers['Access-Control-Allow-Origin'] = environ.get(
        'HTTP_ORIGIN', '')

    if not url:
        params = request.json_body
        LOG.debug(params)
        url = params.get('url', None)

        if not url:
            request.response.status_int = 404
            return {
                'error': 'No url supplied.',
            }

    LOG.debug('api process, ' + url)

    url = url.strip('/')
    LOG.debug('Checking url: ' + url)
    exists = WebPageMgr.exists(url=url)
    if exists:
        LOG.debug('Exists: ...forwarding')
        request.matchdict['hash_id'] = exists
        return api_hash(request)
    else:
        LOG.debug('Does not Exist: ...fetching')
        read = ReadableRequest(url)
        read.process()

        if not read.is_error:
            page = WebPageMgr.store_request(read)
            request.matchdict['hash_id'] = page.hash_id
            return api_hash(request)
        else:
            LOG.error('url_is_error,' + url)
            request.response.status_int = 500
            return {
                'error': 'There was an error fetching content.',
            }