Skip to content

Commit

Permalink
media rss thumbnail support
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Golub committed Nov 28, 2008
1 parent a6e7483 commit 090a499
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions main.py
@@ -1,4 +1,5 @@
import BeautifulSoup
import feedgenerator
import functools
import os
import uuid
Expand All @@ -10,7 +11,6 @@

from django import newforms as forms
from django.template.defaultfilters import slugify
from django.utils import feedgenerator
from django.utils import simplejson
from django.utils.feedgenerator import Enclosure

Expand Down Expand Up @@ -46,6 +46,24 @@ def wrapper(self, *args, **kwargs):
return wrapper


class MediaRSSFeed(feedgenerator.Atom1Feed):
def root_attributes(self):
attrs = super(MediaRSSFeed, self).root_attributes()
attrs["xmlns:media"] = "http://search.yahoo.com/mrss/"
return attrs

def add_item_elements(self, handler, item):
super(MediaRSSFeed, self).add_item_elements(handler, item)
self.add_thumbnail_element(handler, item)

def add_thumbnail_element(self, handler, item):
thumbnail = item.get("thumbnail", None)
if thumbnail:
handler.addQuickElement("media:thumbnail", "", {
"url": thumbnail["url"],
})


class Entry(db.Model):
author = db.UserProperty()
title = db.StringProperty(required=True)
Expand Down Expand Up @@ -160,9 +178,16 @@ def find_enclosure(self, html):
headers["Content-Type"])
return enclosure
return None

def find_thumbnail(self, html):
soup = BeautifulSoup.BeautifulSoup(html)
img = soup.find("img")
if img:
return {"url": img["src"]}
return None

def render_feed(self, entries):
f = feedgenerator.Atom1Feed(
f = MediaRSSFeed(
title=TITLE,
link="http://" + self.request.host + "/",
description=TITLE,
Expand All @@ -176,7 +201,7 @@ def render_feed(self, entries):
author_name=entry.author.nickname(),
pubdate=entry.published,
categories=entry.tags,
enclosure=self.find_enclosure(entry.body),
thumbnail=self.find_thumbnail(entry.body),
)
data = f.writeString("utf-8")
self.response.headers["Content-Type"] = "application/atom+xml"
Expand Down

0 comments on commit 090a499

Please sign in to comment.