#!/usr/bin/python2.6 # -*- coding: UTF-8 -*- """Creates an Atom-Feed for single or multiple winterrodeln sled reports. Format: http://www.winterrodeln.org/feeds/berichte/alle http://www.winterrodeln.org/feeds/berichte/bahn/kemater_alm http://www.winterrodeln.org/feeds/berichte/bahnen/22+42+132 See: http://www.atompub.org/ http://effbot.org/zone/element.htm http://www.winterrodeln.org/trac/wiki/UrlSchema """ import sys import datetime from xml.etree.ElementTree import Element, SubElement, tostring from sqlalchemy.engine import create_engine import logging from pylons import request, response, session, config, tmpl_context as c, url from pylons.controllers.util import abort, redirect from wrfeed.lib.base import BaseController, render log = logging.getLogger(__name__) def create_feed(page_title=None, page_ids=None): """If a page_title is given, only the reports for the given sled are shown. If a list of page_ids is given, only the reports for the selected pages are shown. Otherwise, all reports are shown.""" engine = create_engine(config['sqlalchemy.url']) conn = engine.connect() select = "select id, page_title, date_report, date_entry, `condition`, description, author_name, author_username from wrreport " if not page_title is None: # page_title is given page_title = page_title.replace('_', ' ') sql = select + "where lcase(page_title) = lcase(%s) and date_invalid > now() and delete_date is null order by id desc limit 50" result = conn.execute(sql, page_title) elif not page_ids is None: # a list of page_ids is given sql = [select + "where "] if len(page_ids) > 0: sql += '(' sql += " or ".join(['page_id=%s' for page_id in page_ids]) sql += ') ' sql += 'and date_invalid > now() and delete_date is null order by id desc limit 50' page_ids_str = [str(page_id) for page_id in page_ids] result = conn.execute("".join(sql), *page_ids_str) else: # user wants to have all reports sql = select + "where date_invalid > now() and delete_date is null order by id desc limit 50" result = conn.execute(sql) feed = Element("feed", xmlns="http://www.w3.org/2005/Atom") feed_title = SubElement(feed, "title") feed_title.text = "Winterrodeln Rodelbahnberichte" feed_id = SubElement(feed, "id") if not page_title is None: feed_id.text = url(controller='berichte', action='bahn', id=page_title) elif not page_ids is None: feed_id.text = url(controller='berichte', action='bahnen', id="+".join(page_ids_str)) else: feed_id.text = url(controller='berichte', action='alle') feed_updated = SubElement(feed, "updated") feed.append(Element("link", rel="self", href=feed_id.text)) last_updated = None for row in result: id, page_title, date_report, date_entry, condition, description, author_name, author_username = row page_title_url = page_title.replace(u' ', u'_') entry = SubElement(feed, "entry") entry_title = SubElement(entry, "title") entry_title.text = page_title entry.append(Element("link", rel="alternate", href=u"http://www.winterrodeln.org/wiki/{0}".format(page_title_url), type="text/html", hreflang="de")) entry_id = SubElement(entry, "id") entry_id.text = u"http://www.winterrodeln.org/feeds/schneelage/{0}/{1}".format(page_title_url, id) entry_updated = SubElement(entry, "updated") entry_updated.text = date_entry.isoformat() + "+01:00" if last_updated is None: last_updated = date_entry # entry_summary = SubElement(entry, "summary") # entry_summary.text = str(condition) entry_content = SubElement(entry, "content") entry_content.attrib["type"] = "xhtml" entry_content_div = SubElement(entry_content, "div") entry_content_div.attrib["xmlns"] = "http://www.w3.org/1999/xhtml" entry_content_ul = SubElement(entry_content_div, "ul") if not date_report is None: entry_content_date = SubElement(entry_content_ul, "li") entry_content_date.text = u"Bericht für " + date_report.isoformat() if not condition is None: entry_content_condition = SubElement(entry_content_ul, "li") entry_content_condition.text = u"Schneelage: " + {1: u'Sehr gut', 2: u'Gut', 3: u'Mittelmäßig', 4: u'Schlecht', 5: u'Geht nicht'}[condition] entry_content_description = SubElement(entry_content_ul, "li") entry_content_description.text = description entry_author = SubElement(entry, "author") entry_author_name = SubElement(entry_author, "name") if author_name is None: entry_author_name.text = "Anonymous user" else: entry_author_name.text = author_name if last_updated is None: last_updated = datetime.datetime.now() feed_updated.text = last_updated.isoformat() + "+01:00" feed_xml = '\n' + tostring(feed) conn.close() return feed_xml class BerichteController(BaseController): def alle(self): """http://www.winterrodeln.org/feeds/berichte/alle""" response.content_type = 'application/atom+xml' return create_feed() def bahn(self, id): """http://www.winterrodeln.org/feeds/berichte/bahn/kemater_alm""" response.content_type = 'application/atom+xml' return create_feed(page_title=id) def bahnen(self, id): """http://www.winterrodeln.org/feeds/berichte/bahnen/22+42+132""" page_ids = id.split('+') page_ids = [int(page_id) for page_id in page_ids] response.content_type = 'application/atom+xml' return create_feed(page_ids=page_ids)