예제 #1
0
    def read_events(self, url):

        """
        Read events from the given events RSS feed, specified by 'url', creating
        new Wiki pages where appropriate.
        """

        request = self.request
        request.user.may = IAmRoot()
        category_pagenames = self.options.categories.split()

        # Locate the template for events.

        template_page = PageEditor(request, self.options.template)

        if not template_page.exists():
            print "Template %r cannot be found. Not importing any events!" % self.options.template
            return

        # Process the feed.

        feed = urllib.urlopen(url)

        try:
            nodes = xml.dom.pulldom.parse(feed)
            event_details = {}

            in_item = 0

            # Read the nodes from the feed.

            for node_type, value in nodes:
                if node_type == xml.dom.pulldom.START_ELEMENT:
                    if value.nodeName == "item":
                        in_item = 1

                    # Get the value of the important fields.

                    elif in_item and value.nodeName in self.FIELDS:
                        nodes.expandNode(value)
                        event_details[value.nodeName] = self.text(value)

                    # Where all fields have been read, make a new page.

                    if reduce(lambda x, y: x and event_details.has_key(y), self.FIELDS, 1):

                        # Define the page.

                        title = event_details["title"]

                        # Use any parent page information.

                        full_title = EventAggregatorSupport.getFullPageName(self.options.parent, title)

                        # Find the start and end dates.

                        dates = EventAggregatorSupport.getDateStrings(title)

                        # Require one or two dates.

                        if dates and 1 <= len(dates) <= 2:

                            # Deduce the end date.

                            if len(dates) == 2:
                                start_date, end_date = dates
                            elif len(dates) == 1:
                                start_date = end_date = dates[0]

                            # Load the new page and replace the event details in the body.

                            new_page = PageEditor(request, full_title,
                                uid_override=self.options.author)

                            # Delete the page if requested.

                            if new_page.exists() and self.options.delete:

                                try:
                                    new_page.deletePage()
                                except new_page.AccessDenied:
                                    print "Page %r has not been deleted." % full_title

                            # Complete the new page.

                            elif not new_page.exists() or self.options.overwrite:
                                event_details["summary"] = title
                                event_details["start"] = start_date
                                event_details["end"] = end_date

                                try:
                                    EventAggregatorSupport.fillEventPageFromTemplate(
                                        template_page, new_page, event_details,
                                        category_pagenames)

                                except new_page.Unchanged:
                                    print "Page %r is not changed." % full_title

                            else:
                                print "Not overwriting page %r." % full_title

                        else:
                            print "Could not deduce dates from %r." % title

                        event_details = {}

                elif node_type == xml.dom.pulldom.END_ELEMENT:
                    if value.nodeName == "item":
                        in_item = 0

        finally:
            feed.close()