paul@88 | 1 | # -*- coding: iso-8859-1 -*- |
paul@88 | 2 | """ |
paul@88 | 3 | MoinMoin - Event feed importer, based on the FeedReader macro, the irclog |
paul@88 | 4 | script in MoinMoin, and the EventAggregatorNewEvent action |
paul@88 | 5 | |
paul@88 | 6 | @copyright: 2008, 2009, 2010 by Paul Boddie <paul@boddie.org.uk> |
paul@88 | 7 | 2005-2007 MoinMoin:AlexanderSchremmer |
paul@88 | 8 | 2006 MoinMoin:ThomasWaldmann |
paul@88 | 9 | |
paul@88 | 10 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@88 | 11 | """ |
paul@88 | 12 | |
paul@88 | 13 | from MoinMoin.PageEditor import PageEditor |
paul@88 | 14 | from MoinMoin.script import MoinScript |
paul@88 | 15 | import EventAggregatorSupport |
paul@88 | 16 | import urllib |
paul@88 | 17 | import xml.dom.pulldom |
paul@88 | 18 | |
paul@101 | 19 | # Utility class from the irclog script. |
paul@101 | 20 | |
paul@101 | 21 | class IAmRoot(object): |
paul@101 | 22 | def __getattr__(self, name): |
paul@101 | 23 | return lambda *args, **kwargs: True |
paul@101 | 24 | |
paul@88 | 25 | # The script's class. |
paul@88 | 26 | |
paul@88 | 27 | class PluginScript(MoinScript): |
paul@88 | 28 | |
paul@88 | 29 | """\ |
paul@88 | 30 | Purpose: |
paul@88 | 31 | ======== |
paul@88 | 32 | This tool imports events from an RSS feed into event pages. |
paul@88 | 33 | |
paul@88 | 34 | Detailed Instructions: |
paul@88 | 35 | ====================== |
paul@88 | 36 | General syntax: moin [options] import eventfeed [eventfeed-options] |
paul@88 | 37 | |
paul@88 | 38 | [options] usually should be: |
paul@88 | 39 | --config-dir=/path/to/my/cfg/ --wiki-url=wiki.example.org/ |
paul@88 | 40 | |
paul@88 | 41 | [eventfeed-options] see below: |
paul@88 | 42 | 0. To import events from the FSFE event feed |
paul@88 | 43 | moin ... import eventfeed --url=http://www.fsfe.org/events/events.en.rss ... |
paul@88 | 44 | |
paul@88 | 45 | 1. To use a specific template such as 'EventTemplate' |
paul@88 | 46 | moin ... import eventfeed --template=EventTemplate ... |
paul@88 | 47 | |
paul@88 | 48 | 2. To assign pages to specific categories such as 'CategoryEvents CategoryMeetings' |
paul@88 | 49 | moin ... import eventfeed --categories='CategoryEvents CategoryMeetings' ... |
paul@88 | 50 | |
paul@88 | 51 | 3. To use a specific author such as 'EventImporter' |
paul@88 | 52 | moin ... import eventfeed --author=EventImporter ... |
paul@88 | 53 | |
paul@88 | 54 | 4. To add pages under a common parent page such as 'Events' |
paul@88 | 55 | moin ... import eventfeed --parent=Events ... |
paul@88 | 56 | |
paul@88 | 57 | 5. To overwrite existing event pages |
paul@88 | 58 | moin ... import eventfeed --overwrite ... |
paul@88 | 59 | |
paul@101 | 60 | 6. To delete any event pages associated with the feed |
paul@88 | 61 | moin ... import eventfeed --delete ... |
paul@88 | 62 | """ |
paul@88 | 63 | |
paul@88 | 64 | FIELDS = ("title", "link", "description") |
paul@88 | 65 | |
paul@88 | 66 | def __init__(self, argv, def_values): |
paul@88 | 67 | MoinScript.__init__(self, argv, def_values) |
paul@88 | 68 | self.parser.add_option( |
paul@88 | 69 | "--url", dest="url", default="", |
paul@88 | 70 | help="Specify the location of the events RSS feed" |
paul@88 | 71 | ) |
paul@88 | 72 | self.parser.add_option( |
paul@88 | 73 | "--template", dest="template", default="EventTemplate", |
paul@88 | 74 | help="Specify the template used to make the event pages" |
paul@88 | 75 | ) |
paul@88 | 76 | self.parser.add_option( |
paul@88 | 77 | "--categories", dest="categories", default="CategoryEvents", |
paul@88 | 78 | help="Specify the categories to which the event pages will belong" |
paul@88 | 79 | ) |
paul@88 | 80 | self.parser.add_option( |
paul@88 | 81 | "--author", dest="author", default="EventImporter", |
paul@88 | 82 | help="Specify the author of the event pages" |
paul@88 | 83 | ) |
paul@88 | 84 | self.parser.add_option( |
paul@88 | 85 | "--parent", dest="parent", default="", |
paul@88 | 86 | help="Specify the parent page of the event pages" |
paul@88 | 87 | ) |
paul@88 | 88 | self.parser.add_option( |
paul@88 | 89 | "--overwrite", dest="overwrite", action="store_true", |
paul@88 | 90 | help="Request that existing pages be overwritten" |
paul@88 | 91 | ) |
paul@88 | 92 | self.parser.add_option( |
paul@88 | 93 | "--delete", dest="delete", action="store_true", |
paul@88 | 94 | help="Request that event pages associated with the feed be deleted" |
paul@88 | 95 | ) |
paul@88 | 96 | |
paul@88 | 97 | def mainloop(self): |
paul@88 | 98 | self.init_request() |
paul@88 | 99 | if not self.options.url: |
paul@88 | 100 | print "No URL specified. Not importing any events!" |
paul@88 | 101 | else: |
paul@88 | 102 | self.read_events(self.options.url) |
paul@88 | 103 | |
paul@88 | 104 | def read_events(self, url): |
paul@88 | 105 | |
paul@88 | 106 | """ |
paul@88 | 107 | Read events from the given events RSS feed, specified by 'url', creating |
paul@88 | 108 | new Wiki pages where appropriate. |
paul@88 | 109 | """ |
paul@88 | 110 | |
paul@88 | 111 | request = self.request |
paul@101 | 112 | request.user.may = IAmRoot() |
paul@88 | 113 | category_pagenames = self.options.categories.split() |
paul@88 | 114 | |
paul@88 | 115 | # Locate the template for events. |
paul@88 | 116 | |
paul@88 | 117 | template_page = PageEditor(request, self.options.template) |
paul@88 | 118 | |
paul@88 | 119 | if not template_page.exists(): |
paul@88 | 120 | print "Template %r cannot be found. Not importing any events!" % self.options.template |
paul@88 | 121 | return |
paul@88 | 122 | |
paul@88 | 123 | # Process the feed. |
paul@88 | 124 | |
paul@88 | 125 | feed = urllib.urlopen(url) |
paul@88 | 126 | |
paul@88 | 127 | try: |
paul@88 | 128 | nodes = xml.dom.pulldom.parse(feed) |
paul@88 | 129 | event_details = {} |
paul@88 | 130 | |
paul@88 | 131 | in_item = 0 |
paul@88 | 132 | |
paul@88 | 133 | # Read the nodes from the feed. |
paul@88 | 134 | |
paul@88 | 135 | for node_type, value in nodes: |
paul@88 | 136 | if node_type == xml.dom.pulldom.START_ELEMENT: |
paul@88 | 137 | if value.nodeName == "item": |
paul@88 | 138 | in_item = 1 |
paul@88 | 139 | |
paul@88 | 140 | # Get the value of the important fields. |
paul@88 | 141 | |
paul@88 | 142 | elif in_item and value.nodeName in self.FIELDS: |
paul@88 | 143 | nodes.expandNode(value) |
paul@88 | 144 | event_details[value.nodeName] = self.text(value) |
paul@88 | 145 | |
paul@88 | 146 | # Where all fields have been read, make a new page. |
paul@88 | 147 | |
paul@88 | 148 | if reduce(lambda x, y: x and event_details.has_key(y), self.FIELDS, 1): |
paul@88 | 149 | |
paul@88 | 150 | # Define the page. |
paul@88 | 151 | |
paul@88 | 152 | title = event_details["title"] |
paul@88 | 153 | |
paul@88 | 154 | # Use any parent page information. |
paul@88 | 155 | |
paul@88 | 156 | full_title = EventAggregatorSupport.getFullPageName(self.options.parent, title) |
paul@88 | 157 | |
paul@88 | 158 | # Find the start and end dates. |
paul@88 | 159 | |
paul@88 | 160 | dates = EventAggregatorSupport.getDateStrings(title) |
paul@88 | 161 | |
paul@88 | 162 | # Require one or two dates. |
paul@88 | 163 | |
paul@88 | 164 | if dates and 1 <= len(dates) <= 2: |
paul@88 | 165 | |
paul@88 | 166 | # Deduce the end date. |
paul@88 | 167 | |
paul@88 | 168 | if len(dates) == 2: |
paul@88 | 169 | start_date, end_date = dates |
paul@88 | 170 | elif len(dates) == 1: |
paul@88 | 171 | start_date = end_date = dates[0] |
paul@88 | 172 | |
paul@88 | 173 | # Load the new page and replace the event details in the body. |
paul@88 | 174 | |
paul@88 | 175 | new_page = PageEditor(request, full_title, |
paul@88 | 176 | uid_override=self.options.author) |
paul@88 | 177 | |
paul@88 | 178 | # Delete the page if requested. |
paul@88 | 179 | |
paul@88 | 180 | if new_page.exists() and self.options.delete: |
paul@88 | 181 | |
paul@88 | 182 | try: |
paul@88 | 183 | new_page.deletePage() |
paul@88 | 184 | except new_page.AccessDenied: |
paul@88 | 185 | print "Page %r has not been deleted." % full_title |
paul@88 | 186 | |
paul@88 | 187 | # Complete the new page. |
paul@88 | 188 | |
paul@88 | 189 | elif not new_page.exists() or self.options.overwrite: |
paul@88 | 190 | event_details["summary"] = title |
paul@88 | 191 | event_details["start"] = start_date |
paul@88 | 192 | event_details["end"] = end_date |
paul@88 | 193 | |
paul@88 | 194 | try: |
paul@88 | 195 | EventAggregatorSupport.fillEventPageFromTemplate( |
paul@88 | 196 | template_page, new_page, event_details, |
paul@88 | 197 | category_pagenames) |
paul@88 | 198 | |
paul@88 | 199 | except new_page.Unchanged: |
paul@88 | 200 | print "Page %r is not changed." % full_title |
paul@88 | 201 | |
paul@88 | 202 | else: |
paul@88 | 203 | print "Not overwriting page %r." % full_title |
paul@88 | 204 | |
paul@88 | 205 | else: |
paul@88 | 206 | print "Could not deduce dates from %r." % title |
paul@88 | 207 | |
paul@88 | 208 | event_details = {} |
paul@88 | 209 | |
paul@88 | 210 | elif node_type == xml.dom.pulldom.END_ELEMENT: |
paul@88 | 211 | if value.nodeName == "item": |
paul@88 | 212 | in_item = 0 |
paul@88 | 213 | |
paul@88 | 214 | finally: |
paul@88 | 215 | feed.close() |
paul@88 | 216 | |
paul@88 | 217 | def text(self, element): |
paul@88 | 218 | |
paul@88 | 219 | "Return the text within the given 'element'." |
paul@88 | 220 | |
paul@88 | 221 | nodes = [] |
paul@88 | 222 | for node in element.childNodes: |
paul@88 | 223 | if node.nodeType == node.TEXT_NODE: |
paul@88 | 224 | nodes.append(node.nodeValue) |
paul@88 | 225 | return "".join(nodes) |
paul@88 | 226 | |
paul@88 | 227 | # vim: tabstop=4 expandtab shiftwidth=4 |