--- /dev/null
+# encoding: utf-8
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+class Migration(SchemaMigration):
+
+ def forwards(self, orm):
+
+ # Adding field 'Feed.link'
+ db.add_column('reader_feed', 'link', self.gf('django.db.models.fields.URLField')(max_length=200, null=True, blank=True), keep_default=False)
+
+ # Adding field 'Entry.link'
+ db.add_column('reader_entry', 'link', self.gf('django.db.models.fields.URLField')(max_length=200, null=True, blank=True), keep_default=False)
+
+ # Changing field 'Entry.summary'
+ db.alter_column('reader_entry', 'summary', self.gf('django.db.models.fields.TextField')(null=True))
+
+ # Changing field 'Entry.content'
+ db.alter_column('reader_entry', 'content', self.gf('django.db.models.fields.TextField')(null=True))
+
+
+ def backwards(self, orm):
+
+ # Deleting field 'Feed.link'
+ db.delete_column('reader_feed', 'link')
+
+ # Deleting field 'Entry.link'
+ db.delete_column('reader_entry', 'link')
+
+ # Changing field 'Entry.summary'
+ db.alter_column('reader_entry', 'summary', self.gf('django.db.models.fields.TextField')(default=''))
+
+ # Changing field 'Entry.content'
+ db.alter_column('reader_entry', 'content', self.gf('django.db.models.fields.TextField')(default=''))
+
+
+ models = {
+ 'auth.group': {
+ 'Meta': {'object_name': 'Group'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
+ 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
+ },
+ 'auth.permission': {
+ 'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
+ 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
+ },
+ 'auth.user': {
+ 'Meta': {'object_name': 'User'},
+ 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+ 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
+ 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+ 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+ 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+ 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+ 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+ 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
+ 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
+ },
+ 'contenttypes.contenttype': {
+ 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
+ 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
+ },
+ 'reader.entry': {
+ 'Meta': {'ordering': "['-published']", 'object_name': 'Entry'},
+ 'content': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}),
+ 'feed': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'entries'", 'to': "orm['reader.Feed']"}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'link': ('django.db.models.fields.URLField', [], {'max_length': '200', 'null': 'True', 'blank': 'True'}),
+ 'published': ('django.db.models.fields.DateTimeField', [], {}),
+ 'summary': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}),
+ 'title': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+ 'updated': ('django.db.models.fields.DateTimeField', [], {}),
+ 'uri': ('django.db.models.fields.CharField', [], {'max_length': '255'})
+ },
+ 'reader.feed': {
+ 'Meta': {'object_name': 'Feed'},
+ 'alive': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+ 'etag': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'link': ('django.db.models.fields.URLField', [], {'max_length': '200', 'null': 'True', 'blank': 'True'}),
+ 'modified': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
+ 'title': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+ 'updated': ('django.db.models.fields.DateTimeField', [], {}),
+ 'url': ('django.db.models.fields.URLField', [], {'max_length': '200'})
+ },
+ 'reader.subscription': {
+ 'Meta': {'object_name': 'Subscription'},
+ 'custom_title': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}),
+ 'feed': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'subscriptions'", 'to': "orm['reader.Feed']"}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'reader_subscriptions'", 'to': "orm['auth.User']"})
+ }
+ }
+
+ complete_apps = ['reader']
import feedparser
-def refresh_feed(feed, save=True):
+def _rate_content(content):
+ if content.type == 'application/xhtml+xml':
+ return 0
+ elif content.type == 'text/html':
+ return 1
+ elif content.type == 'text/plain':
+ return 2
+ else:
+ return 3
+
+
+def _choose_content(contents):
+ limited_contents = [content for content in contents if content.type in ('application/xhtml+xml', 'text/html', 'text/plain')]
+ limited_contents.sort(key=_rate_content)
+ return limited_contents[0] if len(limited_contents) > 0 else None
+
+
+def _parse_date(date):
+ try:
+ return datetime.datetime(*(date[0:6]))
+ except:
+ return None
+
+
+def _add_entry(feed, parsed_entry):
+ title = parsed_entry.get('title', 'Untitled')
+ link = parsed_entry.get('link', feed.link)
+ published = _parse_date(parsed_entry.get('published_parsed', parsed_entry.get('created_parsed', parsed_entry.get('updated_parsed', None))))
+ if not published:
+ published = datetime.datetime.now()
+ updated = _parse_date(parsed_entry.get('updated_parsed', None))
+ if not updated:
+ updated = published
+ contents = parsed_entry.get('content', None)
+ if contents:
+ content = _choose_content(contents).value
+ else:
+ content = None
+ summary = parsed_entry.get('summary', None)
+
+ if summary or content:
+ entry, created = feed.entries.get_or_create(uri=parsed_entry.id, defaults={
+ 'title': title,
+ 'link': link,
+ 'published': published,
+ 'updated': updated,
+ 'summary': summary,
+ 'content': content
+ })
+ if not created:
+ entry.title = title
+ entry.link = link
+ entry.published = published
+ entry.updated = updated
+ entry.summary = summary
+ entry.content = content
+ entry.save()
+
+
+def _add_entries(feed, parsed):
+ for parsed_entry in parsed.entries:
+ _add_entry(feed, parsed_entry)
+
+
+def refresh_feed(feed):
if feed.alive:
parsed = feedparser.parse(feed.url, etag=feed.etag, modified=(feed.modified.timetuple() if feed.modified else None))
if parsed.get('status', None) == 304:
if parsed.has_key('modified'):
feed.modified = datetime.datetime(*(parsed.modified[0:6]))
feed.title = parsed.feed.get('title', feed.url)
- feed.updated = datetime.datetime(*(parsed.feed.get('updated_parsed', datetime.datetime.now().timetuple())[0:6]))
- if save:
- feed.save()
+ feed.updated = _parse_date(parsed.feed.get('updated_parsed', datetime.datetime.now().timetuple())[0:6])
+ feed.link = parsed.feed.get('link', feed.url)
+
+ feed.save()
+ _add_entries(feed, parsed)
+
+
+def refresh_all_feeds():
+ for feed in Feed.objects.all():
+ refresh_feed(feed)
def add_feed(url):