agdj/blog/feeds.py
author Adam Gomaa <code@adam.gomaa.us>
Sun Feb 01 01:35:02 2009 -0500
changeset 416 5a586a8c2f8f
parent 414 4b15f0f7ba8a
child 422 cfcb86035488
permissions -rw-r--r--
Switch to date-based URLs everywhere except the feed URLs of old posts, so I don't spam everyone's feedreader.
     1 """
     2 Make feeds for my blog
     3 Use PyRSS2gen
     4 Not Django's - it's odd.
     5 
     6 I like the function-
     7 oriented interface
     8 not the method one
     9 
    10 TODO: max() on an empty sequence in the lastBuildDate section causes
    11 exceptions
    12 
    13 """
    14 
    15 from django.http import HttpResponse, HttpResponseRedirect, Http404
    16 from django.views.decorators.cache import cache_page
    17 
    18 import PyRSS2Gen
    19 
    20 
    21 def _build_entry_item(entry):
    22     "Build the RSSItem for a blog Entry"
    23 
    24     entry_kwargs = {}
    25     entry_kwargs['title'] = entry.title
    26     entry_kwargs['guid'] = guid = entry.urls.feed_url
    27     entry_kwargs['link'] = "http://adam.gomaa.us%s" % guid
    28     entry_kwargs['description'] = entry.rendered
    29     entry_kwargs['pubDate'] = entry.pub_date
    30 
    31     return PyRSS2Gen.RSSItem(**entry_kwargs)
    32 
    33 
    34 def _build_comment_item(comment):
    35     "Return the RSSItem for the blog Comment"
    36 
    37     item_kwargs = {}
    38     item_kwargs['title'] = "Comment on %s" % comment.entry.title
    39     abs_url = comment.entry.feed_url
    40     item_kwargs['link'] = "http://adam.gomaa.us%s" % abs_url
    41     item_kwargs['guid'] = comment.entry.slug+str(comment.id)
    42     item_kwargs['description'] = comment.rendered
    43     item_kwargs['pubDate'] = comment.pub_date
    44 
    45     return PyRSS2Gen.RSSItem(**item_kwargs)
    46 
    47 
    48 @cache_page(60 * 60)
    49 def latest(request):
    50     "Show the latest blog Entries"
    51     from agdj.blog.models import Entry
    52 
    53     limit = request.GET.get("limit", None)
    54     if limit is None:
    55         limit = 5
    56 
    57     items = Entry.public_objects.order_by("-pub_date")[:limit]
    58 
    59     feed_kwargs = {}
    60     feed_kwargs['title'] = "204 No Content Blog"
    61     feed_kwargs['link'] = "http://adam.gomaa.us/blog/"
    62     feed_kwargs['description'] = "Feed for 204 No Content, Adam Gomaa's blog."
    63     feed_kwargs['lastBuildDate'] = max([item.pub_date for item in items])
    64     feed_kwargs['items'] = [_build_entry_item(item) for item in items]
    65 
    66     rss = PyRSS2Gen.RSS2(**feed_kwargs)
    67     res = HttpResponse(rss.to_xml())
    68     res['content-type'] = "application/rss+xml"
    69     return res
    70 
    71 
    72 def per_tag(request, tag):
    73     "Feed for the latest Entries for a tag"
    74     from agdj.blog.models import Entry
    75     from tagging.models import TaggedItem
    76 
    77     items = Entry.objects.filter(public=True).order_by("-pub_date")
    78     items = TaggedItem.objects.get_by_model(items, tag)[:5]
    79     items = list(items)
    80     if not items:
    81         raise Http404()
    82 
    83     feed_kwargs = {}
    84     feed_kwargs['title'] = "204 No Content Blog"
    85     feed_kwargs['link'] = "http://adam.gomaa.us/blog/"
    86     feed_kwargs['description'] = "Feed for '%s' tag in 204 No Content, Adam Gomaa's blog." % tag
    87     feed_kwargs['lastBuildDate'] = max([item.pub_date for item in items])
    88     feed_kwargs['items'] = [_build_entry_item(item) for item in items]
    89 
    90     rss = PyRSS2Gen.RSS2(**feed_kwargs)
    91     res = HttpResponse(rss.to_xml())
    92     res['content-type'] = "application/rss+xml"
    93     return res
    94 
    95 
    96 def comments(request):
    97     "Feed of the latest comments"
    98     from agdj.blog.models import Comment
    99 
   100     items = Comment.objects.order_by("-pub_date")[:15]
   101 
   102     feed_kwargs = {}
   103     feed_kwargs['title'] = "204 Blog Comments"
   104     feed_kwargs['link'] = "http://adam.gomaa.us/blog/"
   105     feed_kwargs['description'] = "Comment feed for 204 No Content."
   106     feed_kwargs['lastBuildDate'] = max([item.pub_date for item in items])
   107     feed_kwargs['items'] = [_build_comment_item(item) for item in items]
   108 
   109     rss = PyRSS2Gen.RSS2(**feed_kwargs)
   110 
   111     res = HttpResponse(rss.to_xml())
   112     res['content-type'] = "application/rss+xml"
   113     return res
   114 
   115