def _handle_update_subscription(message_data, message, context): """ main aggregator handler for the 'update_subscription' message """ try: updated_items = message_data.get('updated_items', []) if len(updated_items) == 0: log.debug('Ignoring subscription update with no updated items...') return cid = message_data.get('composite_id', None) if cid is None: log.debug('Ignoring subscription update with no composite id...') return bid = message_data.get('bucket_id', None) if bid is None: log.debug('Ignoring subscription update to %s with no bucket id...' % cid) 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 # check source if not bid in composite.subscriptions: log.warn('Ignoring subscription update to %s for non-subscribed bucket %s' % (cid, bid)) log.debug("updating %s (from bucket %s)" % (cid, bid)) updated_refs = [] for item in updated_items: ref = dict([(str(k), v) for k, v in item.items()]) updated_refs.append(NewsItemRef.from_doc(ref, context)) count = composite.filtered_update(updated_refs) if count > 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 except: log.error("Error updating composite subscription %s: %s" % (message_data, traceback.format_exc())) raise
def opml(self, id): composite = Composite.get(id, ctx) if composite is None: abort(404) feeds = [] feed_titles = {} for sub_info in composite.subscriptions.itervalues(): feed_url = sub_info.url feeds.append(feed_url) title = sub_info.title if title: feed_titles[feed_url] = title opmlize_response() return dump_opml(feeds, feed_titles=feed_titles)
def set_opml(self, id): composite = Composite.get(id, ctx) if composite is None: abort(404) opml_data = get_posted_data() try: feeds = set(feeds_in_opml(opml_data)) except: import traceback log.error(traceback.format_exc()) abort(400) result = defaultdict(list) oldfeeds = set(i.url for i in composite.subscriptions.itervalues()) remove = oldfeeds - feeds for url in remove: feed = RemoteFeed.get_by_url(url, ctx) if feed is not None: composite.unsubscribe(feed) result["unsubscribed"].append(url) log.debug('Unsubscribed composite "%s" from %s' % (id, url)) else: result["unsubscribe_failed"].append(url) log.error('Expected composite "%s" to have RemoteFeed for %s' % (id, url)) for url in feeds: if url not in oldfeeds: feed = get_or_immediate_create_by_url(url, ctx) if feed is None: result["subscribe_failed"].append(url) log.warn("Could not get or create feed for %s" % url) continue composite.subscribe(feed) result["subscribed"].append(url) log.debug('Subscribed composite "%s" to %s' % (id, url)) else: result["unchanged"].append(url) composite.save() log.debug('Composite "%s" saved' % id) return json_response(result)
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