| 1 | #!/usr/bin/python |
|---|
| 2 | # -*- coding: UTF-8 -*- |
|---|
| 3 | # |
|---|
| 4 | # Copyright (c) 2007 Tarek Ziadé |
|---|
| 5 | # |
|---|
| 6 | # Authors: |
|---|
| 7 | # Tarek Ziadé <tarek@ziade.org> |
|---|
| 8 | # |
|---|
| 9 | # This program is free software; you can redistribute it and/or |
|---|
| 10 | # modify it under the terms of the GNU General Public License |
|---|
| 11 | # as published by the Free Software Foundation; either version 2 |
|---|
| 12 | # of the License, or (at your option) any later version. |
|---|
| 13 | # |
|---|
| 14 | # This program is distributed in the hope that it will be useful, |
|---|
| 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | # GNU General Public License for more details. |
|---|
| 18 | # |
|---|
| 19 | # You should have received a copy of the GNU General Public License |
|---|
| 20 | # along with this program; if not, write to the Free Software |
|---|
| 21 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 22 | # $Id: tables.py 1518 2007-05-21 12:35:44Z rage $ |
|---|
| 23 | """ export to rss |
|---|
| 24 | """ |
|---|
| 25 | import time |
|---|
| 26 | from cgi import escape |
|---|
| 27 | import os |
|---|
| 28 | |
|---|
| 29 | from outputing import register_output |
|---|
| 30 | |
|---|
| 31 | module_path, file = os.path.split(__file__) |
|---|
| 32 | template = os.path.join(module_path, 'feed.rdf') |
|---|
| 33 | RSS_FMT = open(template, 'r').read() |
|---|
| 34 | |
|---|
| 35 | RDF_NS = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" |
|---|
| 36 | |
|---|
| 37 | RSS_ITEM_LI = '<rdf:li rdf:resource="%(item_id)s" />\n' |
|---|
| 38 | |
|---|
| 39 | RSS_ITEM = """\ |
|---|
| 40 | <item rdf:about="%(item_id)s"> |
|---|
| 41 | <title>%(item_title)s</title> |
|---|
| 42 | <description>%(item_description)s</description> |
|---|
| 43 | <link>%(item_link)s</link> |
|---|
| 44 | %(item_dc)s |
|---|
| 45 | </item> |
|---|
| 46 | """ |
|---|
| 47 | |
|---|
| 48 | RSS_ITEM_DC = '<dc:%(dc_key)s>%(dc_value)s</dc:%(dc_key)s>\n' |
|---|
| 49 | |
|---|
| 50 | def rss(entries): |
|---|
| 51 | """returns a rss feed, given db entries""" |
|---|
| 52 | def _encode_item(item): |
|---|
| 53 | if 'encoding' not in item: |
|---|
| 54 | return item |
|---|
| 55 | encoding = item['encoding'] |
|---|
| 56 | for element in item: |
|---|
| 57 | if isinstance(item[element], unicode): |
|---|
| 58 | item[element] = item[element].encode(encoding) |
|---|
| 59 | return item |
|---|
| 60 | |
|---|
| 61 | base_url = 'http://meta/' |
|---|
| 62 | channel_url = '%sexportrss' % base_url |
|---|
| 63 | channel_description = "Meta Feed" |
|---|
| 64 | header_text = body_text = '' |
|---|
| 65 | |
|---|
| 66 | entries = [_encode_item(entry) for entry in entries.select_entries()] |
|---|
| 67 | |
|---|
| 68 | for item in entries: |
|---|
| 69 | if 'link' in item.keys(): |
|---|
| 70 | url = escape(item['link']) |
|---|
| 71 | elif 'url' in item.keys(): |
|---|
| 72 | url = escape(item['url']) |
|---|
| 73 | else: |
|---|
| 74 | url = 'no link' |
|---|
| 75 | |
|---|
| 76 | header_text += RSS_ITEM_LI % {'item_id': url} |
|---|
| 77 | if 'modified_parsed' in item.keys(): |
|---|
| 78 | item_time = item['modified_parsed'] |
|---|
| 79 | else: |
|---|
| 80 | item_time = time.localtime() |
|---|
| 81 | |
|---|
| 82 | item_date = time.strftime('%c', item_time) |
|---|
| 83 | dc_text = '' |
|---|
| 84 | item_title = escape(item['title']) |
|---|
| 85 | dc_text += ' <dc:subject>%s</dc:subject>\n' % item_title |
|---|
| 86 | strip_html = lambda text: text |
|---|
| 87 | |
|---|
| 88 | if 'summary' in item.keys(): |
|---|
| 89 | summary = strip_html(item['summary']) |
|---|
| 90 | elif 'summary_detail' in item.keys(): |
|---|
| 91 | summary = strip_html(item['summary_detail']['value']) |
|---|
| 92 | if len(summary) > DESCRIPTION_MAX_LENGTH: |
|---|
| 93 | summary = summary[:DESCRIPTION_MAX_LENGTH] |
|---|
| 94 | i = summary.rfind(' ') |
|---|
| 95 | if i > 0: |
|---|
| 96 | summary = summary[:i] |
|---|
| 97 | summary += '...' |
|---|
| 98 | elif 'description' in item.keys(): |
|---|
| 99 | summary = item['description'] |
|---|
| 100 | elif 'content' in item.keys(): |
|---|
| 101 | summary = item['content'] |
|---|
| 102 | else: |
|---|
| 103 | summary = '?' |
|---|
| 104 | |
|---|
| 105 | if 'title' in item.keys(): |
|---|
| 106 | title = item['title'] |
|---|
| 107 | else: |
|---|
| 108 | title = '' |
|---|
| 109 | |
|---|
| 110 | current_item = RSS_ITEM % {'item_id': url, |
|---|
| 111 | 'item_title': escape(title), |
|---|
| 112 | 'item_description': escape(summary), |
|---|
| 113 | 'item_link': url, |
|---|
| 114 | 'item_dc': dc_text} |
|---|
| 115 | |
|---|
| 116 | body_text += current_item |
|---|
| 117 | |
|---|
| 118 | text = RSS_FMT % {'css_url': base_url + 'rss.css', |
|---|
| 119 | 'rdf_ns': RDF_NS, |
|---|
| 120 | 'channel_about': channel_url, |
|---|
| 121 | 'channel_title': 'Atomisator', |
|---|
| 122 | 'channel_link': channel_url, |
|---|
| 123 | 'channel_description': escape(channel_description), |
|---|
| 124 | 'items_li': header_text, |
|---|
| 125 | 'items': body_text, |
|---|
| 126 | 'js_url': base_url + 'rss.js', |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | return text |
|---|
| 130 | |
|---|
| 131 | register_output('rss', rss) |
|---|