Exemplo n.º 1
0
def get_initial_batch(id):
    bucket = NewsBucket.get(id, ctx)
    if bucket is None:
        abort(404)
    batch_args = _get_batch_args()
    entries, next = _bucket_latest_entries_batch(bucket, **batch_args)
    return Dibject(entries=entries, next=next)
Exemplo n.º 2
0
def get_feed_info(id):
    bucket = NewsBucket.get(id, ctx)
    if bucket is None:
        abort(404)

    feed = Dibject()
    feed.id = id
    feed.title = bucket.title
    feed.timestamp = bucket.last_modification_date or datetime.utcnow()

    try:
        limit = int(request.params.get('limit', DEFAULT_FEED_SIZE))
    except:
        limit = DEFAULT_FEED_SIZE
    limit = min(limit, MAX_FEED_SIZE)

    feed.entries = _bucket_latest_entries_batch(bucket, limit=limit)
    return feed
Exemplo n.º 3
0
    def get_batch(self, id):
        bucket = NewsBucket.get(id, ctx)
        if bucket is None:
            abort(404)

        batch_args = _get_batch_args()
        entries, next = _bucket_latest_entries_batch(bucket, **batch_args)
        batch = Dibject(next=next)

        if request.is_xhr:
            # this html section can be optionally omitted by specifying 
            # the query argument no_html=True
            if not asbool(request.params.get('no_html', False)):
                batch.html = render_entries_html(entries)
            batch.entries = [i.item_id for i in entries]
            return json_response(batch)
        else:
            batch.entries = entries
            return self._show_batch(id, batch)
Exemplo n.º 4
0
def _handle_new_subscriptions(message_data, message, context):
    """
    helper handler called when new subscriptions are added to 
    a composite.
    """
    try:
        new_subscriptions = message_data.get('new_subscriptions', [])
        if len(new_subscriptions) == 0:
            log.warn("Ignoring init_subscription with no new subscriptions...")
            return

        cid = message_data.get('bucket_id', None)
        if cid is None:
            log.error("Ignoring init_subscription with no bucket_id: %s" % message_data)
            return

        composite = Composite.get(cid, context)
        if composite is None or not 'Composite' in composite.document_types:
            log.error("Ignoring subscription update for non-existent composite %s" % cid)
            return
        
        new_feeds = []
        updates = 0
        for sub in new_subscriptions:
            if not sub in composite.subscriptions:
                log.warn("ignoring subscription %s -> %s, not in composite" % (sub, cid))
                continue
            
            bucket = NewsBucket.get(sub, context)
            if bucket is None:
                log.warn("Ignoring init subscription to unknown object (%s)" % composite.subscriptions[sub])
                continue

            #  try 'casting' to a RemoteFeed
            if 'RemoteFeed' in bucket.document_types:
                rf = RemoteFeed.from_doc(bucket.unwrap(), context)
                # mark as needing immediate fetch if 
                # there is no history for this feed. 
                if len(rf.update_history) == 0:
                    new_feeds.append(rf.url)
                    continue

            try:
                log.debug("init subscription %s -> %s" % (sub, cid))
                updates += composite.init_subscription(sub)
                sleep(0) # yield control
            except:
                log.error("Error initializing subscription %s -> %s: %s" % (sub, cid, traceback.format_exc()))
        if updates > 0:
            try:
                composite.save()
            except ResourceConflict: 
                # not a big deal in this case. This basically means
                # our timestamp did not become the latest -- we 
                # have made no alterations other than adding items.
                # Our additions succeed/fail independently of this as they
                # are separate documents.
                pass

        # request that we start indexing anything new...
        for url in new_feeds:
            request_feed_index(url, context)
    except:
        log.error("Error handling init_subscrition %s: %s" % (message_data, traceback.format_exc()))
        raise