NoPaste

tippsindex.py

von Incom

SNIPPET_TEXT:
  1. USER="bla"
  2. PASSWORD="blubb"
  3. PAGE="autoTippsIndex"
  4. BLACKLIST="TitelIndex_Blacklist"
  5.  
  6.  
  7. import urllib,sys,httplib
  8.  
  9. def getsites(titleindex):
  10.         sites =  urllib.urlopen(titleindex).readlines()
  11.         i=0
  12.         for line in sites:
  13.                 sites[i]=line.strip().decode('utf-8')
  14.                 i=i+1
  15.         return sites
  16.  
  17. def getblacklist(blacklist):
  18.         blacklist = urllib.urlopen(blacklist).readlines()
  19.         i=0
  20.         for line in blacklist:
  21.                 blacklist[i]=line.strip().decode('utf-8')
  22.                 i=i+1
  23.         return blacklist
  24.  
  25. def clean(sites,blacklist):
  26.         for line in blacklist:
  27.                 try:
  28.                         sites.remove(line)
  29.                 except ValueError:
  30.                         pass
  31.         return sites
  32.  
  33. def login(user,password,host="wiki.debianforum.de"):
  34.         params = urllib.urlencode({"action":"login","name":user,"password":password,"login":"Anmelden"})
  35.         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"}
  36.         conn = httplib.HTTPConnection(host)
  37.         conn.request("POST", "/", params, headers)
  38.         response = conn.getresponse()
  39.         cookieh = response.getheader("Set-Cookie")
  40.         cookie=cookieh.split(";")[0].split("=")[1]
  41.         conn.close()
  42.         return cookie
  43.  
  44. def lock(id,page,host="wiki.debianforum.de"):
  45.         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}
  46.         conn = httplib.HTTPConnection(host)
  47.         conn.request("GET", "/"+page+"?action=edit&editor=text", None, headers)
  48.         response=conn.getresponse()
  49.         response=response.read()
  50.         id=""
  51.         i=39
  52.         while (response[response.find("<input type=\"hidden\" name=\"rev\" value=\"")+i].isdigit()):
  53.                 id+=response[response.find("<input type=\"hidden\" name=\"rev\" value=\"")+i]
  54.                 i=i+1
  55.         conn.close()
  56.         return int(id)
  57.  
  58.  
  59. def post(id,page,rev,data,host="wiki.debianforum.de"):
  60.         params=urllib.urlencode({"action":"edit","rev":rev,"button_save":"Aenderungen speichern","editor":"text","savetext":data,"comment":"","category":""})
  61.         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}
  62.         conn = httplib.HTTPConnection(host)
  63.         conn.request("POST", "/"+page, params, headers)
  64.         conn.close()
  65.  
  66.  
  67. def layout(liste):
  68.         lcmp=lambda i, j: (i.lower() < j.lower()) and -1 or (i.lower() > j.lower()) and 1 or 0
  69.         liste.sort(lcmp)
  70.         check=[]
  71.         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"
  72.         for line in liste:
  73.             if((check.count(line[0].upper())==0) and ([0,1,2,3,4,5,6,7,8,9].count(line[0])==0)):
  74.                         data+="\n\n[[Anchor("+line[0].upper()+u")]]\n=== - "+line[0].upper()+" - ===\n"
  75.                         check.append(line[0].upper())
  76.             data+="[:"+line+":][[BR]]\n"
  77.         return data.encode('utf-8', 'replace')
  78.  
  79. if __name__=="__main__":
  80.         sites=getsites("http://wiki.debianforum.de/TitelIndex?action=titleindex")
  81.         blacklist=getblacklist("http://wiki.debianforum.de/"+BLACKLIST+"?action=raw")
  82.         data=layout(clean(sites,blacklist))
  83.         id=login(user=USER,password=PASSWORD)
  84.         rev=lock(id=id,page=PAGE)
  85.         post(id=id,page=PAGE,rev=rev,data=data)

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN