Ejemplo n.º 1
0
def subscription_fetch_and_save(request):
    """
    fetch, parse and save entries from a given subscription.
    """
    host = 'http://%s' % request.get_host()
    key = request.POST.get('key')
    logging.info('task run with key %s' % key)
    subscription = db.get(db.Key(key))
    if subscription is None: 
        logging.error('CRON: can not find that subscription %s' % key)
        raise Http404
    else:
        #request latest entries from subscription's feed_url
        if subscription:
            if subscription.updated:
                modified = subscription.updated.timetuple()
            else:
                modified = None
            feed = DataFeed(subscription.feed_url, subscription.etag, modified)
            if feed.parse():
                entries = feed.entries
                subscription.etag = feed.etag
                subscription.updated = datetime.datetime(*feed.modified[:6])
                subscription.save()
            else:
                entries = []
        result = save_feed_entries(subscription, entries, host)
        if result:
            logging.info('CRON:Entries fetched and saved for %s url %s' % (key,
                subscription.feed_url))
            return HttpResponse('Thanks Cron!')
        else:
            logging.error('CRON:Entries NOT saved for %s' % key)
            return HttpResponse('Sorry Cron!')
Ejemplo n.º 2
0
def subscribe_callback(request, key):
    """
    handles both subscription verification from the hub and the processing of
    entries coming from a feed sent by the hub.
    Difference: GET is used for verification and POST is used for pushing the
    feed.
    """
    host = 'http://%s' % request.get_host()
    if request.method == 'GET':
        # just confirm everything for now. nexttime, use verify_token and update
        # the subscription entity.
        hub_challenge = request.GET.get('hub.challenge', '')
        subscription = db.get(db.Key(key))
        if subscription is None: 
            logging.error('CALLBACK:can not find that subscription %s' % key)
            raise Http404
        else:
            subscription.verified = True
            subscription.save()
        return HttpResponse(hub_challenge)
    else:
        feed_body = request.raw_post_data
        subscription = db.get(db.Key(key))
        if subscription:
            if subscription.updated:
                modified = subscription.updated.timetuple()
            else:
                modified = None
            #parse the recieved feed body
            feed = DataFeed(feed_body, subscription.etag, modified)
            if feed.parse():
                entries = feed.entries
                subscription.etag = feed.etag
                subscription.updated = datetime.datetime(*feed.modified[:6])
                subscription.save()
            else:
                entries = []
        result = save_feed_entries(subscription, entries, host)
        if result:
            logging.info('HUB:Entries recieved for subscr. %s' % key)
            return HttpResponse('Thanks Hub!')
        else:
            logging.error('HUB:Can not find subscr. %s' % key)
            return HttpResponse('Sorry Hub!')