Dynastie/generators/rss.py

85 lines
3.0 KiB
Python
Raw Normal View History

import os
import datetime
import xml
from dynastie.generators.generator import DynastieGenerator
from xml.dom.minidom import getDOMImplementation
from django.db import models
class RSS(DynastieGenerator):
def appendElement(self, dom, root, name='', content='', attributes=None):
elem = dom.createElement(name)
if not attributes is None:
for k, v in attributes.iteritems():
elem.setAttribute(k, v)
if content != '':
elem.appendChild(dom.createTextNode(content))
root.appendChild(elem)
return elem
def generate(self, blog, src, output):
2012-08-28 09:09:14 +02:00
from dynastie.models import Post, Blog
now = datetime.datetime.now()
impl = getDOMImplementation()
dom = impl.createDocument(None, "rss", None)
root = dom.documentElement
root.setAttribute('version', '2.0')
root.setAttributeNS('xmlns', 'xmlns:atom', 'http://www.w3.org/2005/Atom')
channel = dom.createElement('channel')
root.appendChild(channel)
self.appendElement(dom, channel, 'title', blog.title)
self.appendElement(dom, channel, 'description', blog.description)
self.appendElement(dom, channel, 'link', 'http://' + blog.name)
builddate = now.strftime('%a, %d %b %Y %H:%M:%S')
self.appendElement(dom, channel, 'lastBuildDate', builddate)
self.appendElement(dom, channel, 'generator', 'Dynastie')
self.appendElement(dom, channel, 'language', 'en-gb')
2012-09-08 12:35:52 +02:00
posts = Post.objects.filter(published=True).order_by('-modification_date')[:10]
2012-08-28 09:09:14 +02:00
for post in posts:
item = dom.createElement('item')
2012-08-28 09:09:14 +02:00
self.appendElement(dom, item, 'title', post.title)
path = 'http://' + blog.name + post.getPath()
self.appendElement(dom, item, 'link', path)
self.appendElement(dom, item, 'guid', path)
2012-08-28 09:09:14 +02:00
filename = blog.src_path + '/_post/' + str(post.id)
if not os.path.exists(filename):
self.addError('File does not exists ' + filename)
return
f = open(filename, 'rb')
2012-08-28 09:09:14 +02:00
post_content = f.read()
f.close()
2012-08-28 09:09:14 +02:00
self.appendElement(dom, item, 'description', '<![CDATA[' + post_content + ']]>')
try:
author = post.author.email
author += ' (' + post.author.first_name + ' ' + post.author.last_name + ')'
self.appendElement(dom, item, 'author', author)
self.appendElement(dom, item, 'category', post.category.name)
except:
pass
2012-08-28 09:09:14 +02:00
creationDate = post.creation_date.strftime('%a, %d %b %Y %H:%M:%S')
self.appendElement(dom, item, 'pubDate', creationDate)
channel.appendChild(item)
filename = 'rss.xml'
self.writeIfNotTheSame(output + '/' + filename, root)
if not self.somethingWrote:
self.addReport('Nothing changed')
return self.report