2010-01-26 13:06:47 +00:00
|
|
|
import logging
|
2010-01-27 07:26:47 +00:00
|
|
|
import uuid
|
|
|
|
import re
|
2010-01-26 13:06:47 +00:00
|
|
|
|
2010-01-26 13:44:26 +00:00
|
|
|
from feedformatter import Feed
|
|
|
|
import time
|
2010-01-26 13:06:47 +00:00
|
|
|
|
2010-01-26 13:44:26 +00:00
|
|
|
from scribeengine.lib.base import *
|
|
|
|
from scribeengine.model.meta import Session
|
|
|
|
from scribeengine.model import Post, Variable
|
2010-01-26 13:06:47 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
class FeedController(BaseController):
|
|
|
|
|
2010-01-26 13:44:26 +00:00
|
|
|
def _generate_feed(self):
|
|
|
|
blog_title = Session.query(Variable).get(u'blog title').value
|
|
|
|
blog_slogan = Session.query(Variable).get(u'blog slogan').value
|
2010-01-27 07:26:47 +00:00
|
|
|
blog_link = str('%s://%s' % (request.environ[u'wsgi.url_scheme'], \
|
|
|
|
request.environ[u'HTTP_HOST']))
|
2010-01-26 13:44:26 +00:00
|
|
|
if blog_link.endswith(u'/'):
|
|
|
|
blog_link = blog_link[:-1]
|
|
|
|
posts = Session.query(Post)\
|
|
|
|
.filter_by(status=u'published')\
|
|
|
|
.order_by(Post.created.desc())\
|
|
|
|
.all()
|
|
|
|
# Create the feed
|
|
|
|
feed = Feed()
|
|
|
|
# Set the feed/channel level properties
|
|
|
|
feed.feed[u'title'] = blog_title
|
|
|
|
feed.feed[u'link'] = blog_link + u'/'
|
|
|
|
feed.feed[u'description'] = blog_slogan
|
|
|
|
for post in posts:
|
|
|
|
# Create an item
|
|
|
|
item = {}
|
|
|
|
item[u'title'] = post.title
|
|
|
|
item[u'link'] = blog_link + h.url_for_post(post)
|
|
|
|
item[u'author'] = post.user.nick
|
2010-01-27 07:26:47 +00:00
|
|
|
item[u'description'] = re.sub(r'<(.*?)>', u'', h.teaser(post.body))
|
2010-01-26 13:44:26 +00:00
|
|
|
item[u'pubDate'] = post.created.timetuple()
|
2010-01-27 07:26:47 +00:00
|
|
|
item[u'guid'] = str(uuid.uuid5(uuid.NAMESPACE_URL, blog_link + h.url_for_post(post)))
|
2010-01-26 13:44:26 +00:00
|
|
|
# Add item to feed
|
|
|
|
feed.items.append(item)
|
|
|
|
return feed
|
|
|
|
|
2010-01-26 13:06:47 +00:00
|
|
|
def index(self):
|
2010-01-27 07:26:47 +00:00
|
|
|
h.redirect_to(h.url_for(action=u'atom'))
|
2010-01-26 13:44:26 +00:00
|
|
|
|
|
|
|
def rss(self, id=u'2.0'):
|
|
|
|
feed = self._generate_feed()
|
|
|
|
if id == u'1.0':
|
|
|
|
return feed.format_rss1_string()
|
|
|
|
else:
|
|
|
|
return feed.format_rss2_string()
|
|
|
|
|
|
|
|
def atom(self):
|
|
|
|
feed = self._generate_feed()
|
|
|
|
return feed.format_atom_string()
|
|
|
|
|