Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Aggregator: skip feed entries that don't have link or guid.

  • Loading branch information...
commit e3587823c23360ac2f43d847019fa8fb346e86cf 1 parent 1ab59c0
@jacobian jacobian authored
Showing with 17 additions and 15 deletions.
  1. +17 −15 django_website/aggregator/models.py
View
32 django_website/aggregator/models.py
@@ -29,7 +29,7 @@ class Feed(models.Model):
def __unicode__(self):
return self.title
-
+
def save(self, **kwargs):
super(Feed, self).save(**kwargs)
Subscription.objects.subscribe(self.feed_url, settings.PUSH_HUB)
@@ -43,37 +43,37 @@ def create_or_update_by_guid(self, guid, **kwargs):
"""
Look up a FeedItem by GUID, updating it if it exists, and creating
it if it doesn't.
-
+
We don't limit it by feed because an item could be in another feed if
some feeds are themselves aggregators. That's also why we don't update
the feed field if the feed item already exists.
-
+
Returns (item, created) like get_or_create().
"""
try:
item = self.get(guid=guid)
-
+
except self.model.DoesNotExist:
# Create a new item
log.debug('Creating entry: %s', guid)
kwargs['guid'] = guid
item = self.create(**kwargs)
-
+
else:
log.debug('Updating entry: %s', guid)
# Update an existing one.
kwargs.pop('feed', None)
-
+
# Don't update the date since most feeds get this wrong.
kwargs.pop('date_modified')
-
+
for k,v in kwargs.items():
setattr(item, k, v)
item.save()
-
+
return item
-
+
class FeedItem(models.Model):
feed = models.ForeignKey(Feed)
title = models.CharField(max_length=500)
@@ -100,12 +100,15 @@ def feed_updated(sender, notification, **kwargs):
except Feed.DoesNotExist:
log.error("Subscription ID %s (%s) doesn't have a feed.", sender.id, sender.topic)
return
-
+
for entry in notification.entries:
title = entry.title
- guid = entry.get("id", entry.link)
+ try:
+ guid = entry.get("id", entry.link)
+ except AttributeError:
+ log.error("Feed ID %s has an entry ('%s') without a link or guid. Skipping.", feed.id, title)
link = entry.link or guid
-
+
if hasattr(entry, "summary"):
content = entry.summary
elif hasattr(entry, "content"):
@@ -119,14 +122,13 @@ def feed_updated(sender, notification, **kwargs):
date_modified = datetime.datetime(*entry.updated_parsed[:6])
else:
date_modified = datetime.datetime.now()
-
+
FeedItem.objects.create_or_update_by_guid(guid,
feed = feed,
title = title,
link = link,
summary = content,
date_modified = date_modified
- )
+ )
push_signals.updated.connect(feed_updated)
-
Please sign in to comment.
Something went wrong with that request. Please try again.