Project

General

Profile

Creating wiki pages automatically via python script

Added by André Bachmann over 12 years ago

I want to create some wiki pages automatically via script. To achieve this, I had the idea for a python script: First step is to send the appropiate HTTP POST request to my Redmine server so that I'm logged in. After that, I have the necessary rights to edit a page and can send my new page.

Sounds easy, but I still do not understand the login authentification process of Redmine well enough. For example, what does "Filter chain halted as [:find_existing_or_new_page] rendered_or_redirected." in my production.log mean?

I am also currently in the process of learning python, so please be lenient. Any support will be appreciated. :)

This is my first python script so long:

# -*- coding: utf-8 -*-
import urllib, urllib2, httplib, re
from HTMLParser import HTMLParser

token = None

class MyHTMLParser(HTMLParser):
    def handle_startendtag(self, data, atrs):
        global token
        found = 0
        if re.search("meta", data):
            for option, value in atrs:
                if found == 1:
                    token = value
                    #print "Token (from MyHTMLParser):", token
                    return
                else:
                    if re.search("csrf-token", value):
                        found = 1

try:
    url = 'http://informatix/'
    user_agent_hdr = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MDDS; FDM)'
    accept_hdr = 'application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*'
    referer_hdr = 'http://informatix/projects/mp/wiki/Startseite'
    accept_lang_hdr = 'de-DE'
    #connection_hdr = 'Keep-Alive'

    headers = { 'User-Agent' : user_agent_hdr, 'Accept' : accept_hdr, 'Referer' : referer_hdr, 'Accept-Language': accept_lang_hdr }

    req1 = urllib2.Request(url, None, headers)
    f = urllib2.urlopen(req1)
    print "<!--\n*** First URL opened successfully, getting token..." 
    parser = MyHTMLParser()
    parser.feed(f.read())

    #backurl_encoded = urllib.quote_plus('http://informatix/projects/mp/wiki/Startseite')
    #login_unq = urllib.unquote_plus('Anmelden+%C2%BB') # only Redmine 1.2.0 knows why...

    print "*** Token:", token

    #data = urllib.urlencode({'authenticity_token' : token, 'back_url' : backurl_encoded , 'username' : 'automatic_reporter' , 'password' : 'xyz' , 'login' : login_unq})
    #print "*** Sending parameters:", data

    data_manually = 'authenticity_token=' + token + '&back_url=' + urllib.quote_plus(urllib.quote_plus('http://informatix/projects/mp/wiki/Startseite')) + '&username=automatic_reporter&password=xyz&login=Anmelden+%C2%BB'
    print "*** data_manually:", data_manually

    #req2 = urllib2.Request(url, data, headers)
    req2 = urllib2.Request(url, data_manually, headers)
    print "*** Request successful, URL will be opened..." 
    response = urllib2.urlopen(req2)
    the_page = response.read()
    print "*** Site contents:\n-->", the_page

    f.close()
    response.close()

except urllib2.URLError, e:
    print "*** Error:", e
    exit

This is the output from my production.log:

Processing WikiController#show (for 192.168.115.116 at 2012-01-13 15:57:01) [GET]
  Parameters: {"project_id"=>"mp", "action"=>"show", "controller"=>"wiki"}
Redirected to http://informatix/projects/mp/wiki/Startseite
Filter chain halted as [:find_existing_or_new_page] rendered_or_redirected.
Completed in 0ms (DB: 0) | 302 Found [http://informatix/]

Processing WikiController#show (for 192.168.115.116 at 2012-01-13 15:57:01) [GET]
  Parameters: {"project_id"=>"mp", "action"=>"show", "id"=>"Startseite", "controller"=>"wiki"}
Rendering template within layouts/base
Rendering wiki/show
Completed in 94ms (View: 78, DB: 0) | 200 OK [http://informatix/projects/mp/wiki/Startseite]

Processing WikiController#show (for 192.168.115.116 at 2012-01-13 15:57:01) [POST]
  Parameters: {"back_url"=>"http%3A%2F%2Finformatix%2Fprojects%2Fmp%2Fwiki%2FStartseite", "project_id"=>"mp", "action"=>"show", "authenticity_token"=>"VKUEhgXbOG7wDxx2igiCqdmCzC90vqYP895RN0GWFaQ=", "username"=>"automatic_reporter", "controller"=>"wiki", "password"=>"[FILTERED]", "login"=>"Anmelden »"}
Redirected to http://informatix/projects/mp/wiki/Startseite?authenticity_token=VKUEhgXbOG7wDxx2igiCqdmCzC90vqYP895RN0GWFaQ%3D&back_url=http%253A%252F%252Finformatix%252Fprojects%252Fmp%252Fwiki%252FStartseite&login=Anmelden+%C2%BB&password=xyz&username=automatic_reporter
Filter chain halted as [:find_existing_or_new_page] rendered_or_redirected.
Completed in 16ms (DB: 16) | 302 Found [http://informatix/]

Processing WikiController#show (for 192.168.115.116 at 2012-01-13 15:57:01) [GET]
  Parameters: {"back_url"=>"http%3A%2F%2Finformatix%2Fprojects%2Fmp%2Fwiki%2FStartseite", "project_id"=>"mp", "action"=>"show", "authenticity_token"=>"VKUEhgXbOG7wDxx2igiCqdmCzC90vqYP895RN0GWFaQ=", "username"=>"automatic_reporter", "id"=>"Startseite", "controller"=>"wiki", "login"=>"Anmelden »", "password"=>"[FILTERED]"}
Rendering template within layouts/base
Rendering wiki/show
Completed in 140ms (View: 125, DB: 0) | 200 OK [http://informatix/projects/mp/wiki/Startseite?authenticity_token=VKUEhgXbOG7wDxx2igiCqdmCzC90vqYP895RN0GWFaQ%3D&back_url=http%253A%252F%252Finformatix%252Fprojects%252Fmp%252Fwiki%252FStartseite&login=Anmelden+%C2%BB&password=xyz&username=automatic_reporter]