USER="bla"
PASSWORD="blubb"
PAGE="autoTippsIndex"
BLACKLIST="TitelIndex_Blacklist"


import urllib,sys,httplib

def getsites(titleindex):
        sites =  urllib.urlopen(titleindex).readlines()
        i=0
        for line in sites:
                sites[i]=line.strip().decode('utf-8')
                i=i+1
        return sites

def getblacklist(blacklist):
        blacklist = urllib.urlopen(blacklist).readlines()
        i=0
        for line in blacklist:
                blacklist[i]=line.strip().decode('utf-8')
                i=i+1
        return blacklist

def clean(sites,blacklist):
        for line in blacklist:
                try:
                        sites.remove(line)
                except ValueError:
                        pass
        return sites

def login(user,password,host="wiki.debianforum.de"):
        params = urllib.urlencode({"action":"login","name":user,"password":password,"login":"Anmelden"})
        headers = {"Content-type":"application/x-www-form-urlencoded","Accept":"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"}
        conn = httplib.HTTPConnection(host)
        conn.request("POST", "/", params, headers)
        response = conn.getresponse()
        cookieh = response.getheader("Set-Cookie")
        cookie=cookieh.split(";")[0].split("=")[1]
        conn.close()
        return cookie

def lock(id,page,host="wiki.debianforum.de"):
        headers = {"Content-type":"application/x-www-form-urlencoded","Accept":"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5","Cookie":"MOIN_ID="+id}
        conn = httplib.HTTPConnection(host)
        conn.request("GET", "/"+page+"?action=edit&editor=text", None, headers)
        response=conn.getresponse()
        response=response.read()
        id=""
        i=39
        while (response[response.find("<input type=\"hidden\" name=\"rev\" value=\"")+i].isdigit()):
                id+=response[response.find("<input type=\"hidden\" name=\"rev\" value=\"")+i]
                i=i+1
        conn.close()
        return int(id)


def post(id,page,rev,data,host="wiki.debianforum.de"):
        params=urllib.urlencode({"action":"edit","rev":rev,"button_save":"Aenderungen speichern","editor":"text","savetext":data,"comment":"","category":""})
        headers = {"Content-type":"application/x-www-form-urlencoded","Accept":"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5","Cookie":"MOIN_ID="+id}
        conn = httplib.HTTPConnection(host)
        conn.request("POST", "/"+page, params, headers)
        conn.close()


def layout(liste):
        lcmp=lambda i, j: (i.lower() < j.lower()) and -1 or (i.lower() > j.lower()) and 1 or 0
        liste.sort(lcmp)
        check=[]
        data="= Tipps-Index =\nEine Liste aller Wiki Seiten aus der Tipps Ecke. (Alphabetisch geordnet)\nEs befinden sich derzeit "+str(len(liste))+" Seiten im Index. [[BR]] Bitte veraendere diese Seite nicht, sie wird automatisch erstellt.\n\n[#Nummern Nummern] - [#A A] - [#B B] - [#C C] - [#D D] - [#E E] - [#F F] - [#G G] - [#H H] - [#I I] - [#J J] - [#K K] - [#L L] - [#M M] - [#N N] - [#O O] - [#P P] - [#Q Q] - [#R R] - [#S S] - [#T T] - [#U U] - [#V V] - [#W W] - [#X X] - [#Y Y] - [#Z Z]\n\n"
        for line in liste:
            if((check.count(line[0].upper())==0) and ([0,1,2,3,4,5,6,7,8,9].count(line[0])==0)):
                        data+="\n\n[[Anchor("+line[0].upper()+u")]]\n=== - "+line[0].upper()+" - ===\n"
                        check.append(line[0].upper())
            data+="[:"+line+":][[BR]]\n"
        return data.encode('utf-8', 'replace')

if __name__=="__main__":
        sites=getsites("http://wiki.debianforum.de/TitelIndex?action=titleindex")
        blacklist=getblacklist("http://wiki.debianforum.de/"+BLACKLIST+"?action=raw")
        data=layout(clean(sites,blacklist))
        id=login(user=USER,password=PASSWORD)
        rev=lock(id=id,page=PAGE)
        post(id=id,page=PAGE,rev=rev,data=data)    
