... because from time to time I'm a web developer, too
About me
Projects
Contact
Links

Django + TinyMCE + RSS feeds = Can’t watch youtube movies in Google Reader

August 18, 2009 | frameworks, python, tools
author: Karol Zielinski | comments: 1 | views: 1478
Tags: , , , , , ,

I had a problem with RSS feeds on tspot.pl. What was the problem? I couldn’t watch youtube movies via Google Reader. After a bit of research and compare wordpress‘ feeds with my own (django-based) feeds – I found a reason (hoped that was a truly reason).

Reason: ‘media’ plugin in TinyMCE removes ‘embed’ tag from youtube code.

Ok, so let fix it…

There are two easiest possible ways to solve this:

  1. remove ‘media’ plugin from your TinyMCE configuration
  2. add ‘embed’ tag in our django‘s feed class

Ad. 1

The easiest way to solve the problem is to remove ‘media’ plugin from your TinyMCE configuration. It’s easy enough, so I won’t describe how to do it…

However… first: because of that… we can’t use this plugin in our TinyMCE and second (much more important)… because of that – we can’t see where is the movie in our text. Below I present the same text with this plugin and without it.

TinyMCE with ‘movie’ plugin

TinyMCE with plugin movie

TinyMCE without ‘movie’ plugin

TinyMCE without plugin movie

Much better with this plugin, isn’t it? Sure, it is.

… that’s why I prefer the second way to solve this problem.

Ad. 2

We want to add ‘embed’ tag only to our RSS feeds. We don’t want to add it everywhere. That’s why we will change just one class – our feed class.
Tested on youtube movies, but it should good enough for other video sites, too.

Open your feed class (propably ‘feeds.py’ file) using your favourite text editor and edit it… as shown below.


# -*- coding: utf-8 -*-
from django.contrib.syndication.feeds import Feed
from project.main.models import News

from datetime import datetime as datetime2now

import re

class AbstractFeed:
	title = ''
	lead = ''
	slug = ''
	view_name = ''
	dateCreated = ''
	date = datetime2now.now()

	def get_absolute_url(self):
		return '/' + self.view_name + '/' + self.slug + '/'

	def __unicode__(self):
		return self.title

def getDateCreated(anObject):
    return anObject.dateCreated

class LatestEntries(Feed):
	title = 'title of my feed'
	link = '/'
	description = 'description of my feed'

	def item_pubdate(self, item):
        	return item.date

	def items(self):
                # fetch some entries
		newses2feed = News.objects.order_by('-dateCreated')[:10]

		feed_list = []
		for some_news in newses2feed:
			news_text = some_news.text

			# remove new lines between tags 'object' and 'param'
			text_splitted = news_text.split('\n')
			new_text = ''
			for each_line in text_splitted:
				is_param_or_object = False
				if '<param' in each_line:
					each_line = each_line.replace('\n', '').replace('\t', '').replace('\r', '')
					is_param_or_object = True
				if '<object ' in each_line:
					each_line = each_line.replace('\n', '').replace('\t', '').replace('\r', '')
					is_param_or_object = True

				new_text += each_line
				if not is_param_or_object:
					new_text += '\n'

			# add embed tag before '</object>'
			text_splitted = new_text.split('\n')
			text_re = re.compile('^\<object (.*?)data\=\"(?P<url>.*?)\"(.*?)$')
			type_re = re.compile('^\<object (.*?)type\=\"(?P<type>.*?)\"(.*?)$')
			width_re = re.compile('^\<object (.*?)width\=\"(?P<width>.*?)\"(.*?)$')
			height_re = re.compile('^\<object (.*?)height\=\"(?P<height>.*?)\"(.*?)$')

			flash_data = ''
			flash_type = ''
			flash_width = ''
			flash_height = ''

			new_text = ''

			for each_line in text_splitted:
				match_url = text_re.match(each_line)
				if match_url:
					flash_type = ''
					flash_width = ''
					flash_height = ''

					flash_data = match_url.group('url')

					match = type_re.match(each_line)
					if match:
						flash_type = match.group('type')

					match = width_re.match(each_line)
					if match:
						flash_width = match.group('width')

					match = height_re.match(each_line)
					if match:
						flash_height = match.group('height')

					new_embed = '<embed type="'+flash_type+'" width="'+flash_width+'" height="'+flash_height+'" src="'+flash_data+'" allowscriptaccess="always" allowfullscreen="true"></embed>'
					new_text += each_line.replace('</object>', new_embed+'</object>') + '\n'

				else:
					new_text += each_line + '\n'

			news_text = new_text

			some_object = AbstractFeed()
			some_object.title = some_news.title
			some_object.lead = news_text
			some_object.slug = some_news.slug
			some_object.dateCreated = some_news.dateCreated
			some_object.view_name = 'news'
			some_object.date = some_news.dateCreated

			feed_list.append(some_object)

		feed_list.sort(key=getDateCreated)
		feed_list.reverse()

		return feed_list

I realize that it could be fixed by the other ways, too. However for my needs – it’s good enough.

Bookmark and Share
Post Django + TinyMCE + RSS feeds = Can’t watch youtube movies in Google Reader to develway Post Django + TinyMCE + RSS feeds = Can’t watch youtube movies in Google Reader to Delicious Post Django + TinyMCE + RSS feeds = Can’t watch youtube movies in Google Reader to Digg Post Django + TinyMCE + RSS feeds = Can’t watch youtube movies in Google Reader to Facebook Post Django + TinyMCE + RSS feeds = Can’t watch youtube movies in Google Reader to Reddit Post Django + TinyMCE + RSS feeds = Can’t watch youtube movies in Google Reader to StumbleUpon

Related news and resources

Comments (1)

4Avatars v0.3.1 v0.3.1
RSS/Atom feed in Django and parameters from URL – Just a tech stuff
September 23, 2009, 3:16 am

[...] Django + RSS I was writing some time ago. Today – one more example related to this topic. I will write how [...]

Write a comment

Karol Zielinski :: Just a tech stuff Hello, I'm Karol Zielinski, internet evangelist, an entrepreneur, project manager and a web developer from Gdynia, Poland. I like creative design, good advertisement, social media and all kind of stuff around the web.

Most popular posts

Much more links

Karol Zielinski    |   contact me
Gdynia, Poland
RSS - Just a tech stuff - python, java blog - web development blog Karol Zielinski on twitter Karol Zielinski on LinkedIn Karol Zielinski on facebook Karol Zielinski on delicious Karol Zielinski on digg Karol Zielinski on flickr Karol Zielinski on stumbleupon Karol Zielinski on technorati