Difference between revisions of "User:GrabberBot/cleaner.py"
From The Battle for Wesnoth Wiki
(pasted code) |
|||
Line 7: | Line 7: | ||
url3 = "http://allefant.sourceforge.net/wesnoth/graphiclibrary.py?acquire=1" | url3 = "http://allefant.sourceforge.net/wesnoth/graphiclibrary.py?acquire=1" | ||
− | |||
NO_UPDATE = False | NO_UPDATE = False | ||
COMMANDS_FILE = "" | COMMANDS_FILE = "" | ||
Line 20: | Line 19: | ||
skip -= 1 | skip -= 1 | ||
else: | else: | ||
− | if | + | if sys.argv[i] == "-n": |
− | |||
− | |||
NO_UPDATE = True | NO_UPDATE = True | ||
elif sys.argv[i] == "-c": | elif sys.argv[i] == "-c": | ||
Line 58: | Line 55: | ||
moves = {} | moves = {} | ||
modified = 0 | modified = 0 | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
def repl(mob): | def repl(mob): | ||
− | global commands, moves, modified | + | global commands, moves, modified, VERBOSE, NO_UPDATE, MOVE_ALL, stats |
item = mob.group(0) | item = mob.group(0) | ||
− | name = re.compile("""\\* \\[http://wesnoth.org/wiki/.*? | + | name = re.compile("""\\* \\[http://wesnoth.org/wiki/Contrib:.*? (.*?)\\]""").search(item).group(1) |
− | + | image = re.compile("\\| (http://.*) \\|").search(item).group(1) | |
− | |||
− | |||
− | |||
− | |||
if VERBOSE: | if VERBOSE: | ||
print name, image | print name, image | ||
if mob: | if mob: | ||
for com in commands: | for com in commands: | ||
− | + | if com == "delete " + image: | |
− | if com == "delete | ||
modified = 1 | modified = 1 | ||
stats[0] += 1 | stats[0] += 1 | ||
Line 89: | Line 72: | ||
print " delete" | print " delete" | ||
return "" | return "" | ||
− | elif (com == "move | + | elif (com == "move " + name + " " + image) or MOVE_ALL: |
modified = 1 | modified = 1 | ||
stats[1] += 1 | stats[1] += 1 | ||
Line 118: | Line 101: | ||
for move in moves: | for move in moves: | ||
anything = 0 | anything = 0 | ||
− | + | ||
if VERBOSE: | if VERBOSE: | ||
− | print | + | print move |
− | contrib = mw.fetch( | + | contrib = mw.fetch("Contrib:" + move) |
if contrib.strip() == "": # doesn't exist yet | if contrib.strip() == "": # doesn't exist yet | ||
contrib = "== %s ==\n\n\n== See Also ==\n\n[[GraphicLibrary]]" % move | contrib = "== %s ==\n\n\n== See Also ==\n\n[[GraphicLibrary]]" % move | ||
miss = file("missing_artists.txt", "a") | miss = file("missing_artists.txt", "a") | ||
− | miss.write( | + | miss.write(move + "\n") |
if VERBOSE: | if VERBOSE: | ||
print " creating new page" | print " creating new page" | ||
Line 136: | Line 119: | ||
contrib = "" | contrib = "" | ||
if VERBOSE: | if VERBOSE: | ||
− | print " page %s for %s" % (( | + | print " page %s for %s" % (("Contrib:" + move), move) |
mob = re.compile("\\s*==.*?== *\n\n").search(contrib) | mob = re.compile("\\s*==.*?== *\n\n").search(contrib) | ||
Line 142: | Line 125: | ||
newcontrib = contrib[:mob.end(0)] | newcontrib = contrib[:mob.end(0)] | ||
for pic in moves[move]: | for pic in moves[move]: | ||
− | if not check_already( | + | if not check_already(move, pic): |
newcontrib += pic + "\n" | newcontrib += pic + "\n" | ||
anything += 1 | anything += 1 | ||
Line 153: | Line 136: | ||
contrib = newcontrib | contrib = newcontrib | ||
else: | else: | ||
− | print "Could not add %s for %s." % (moves[move], | + | print "Could not add %s for %s." % (moves[move], move) |
if anything and not NO_UPDATE: | if anything and not NO_UPDATE: | ||
# Update contributor page | # Update contributor page | ||
− | mw.post( | + | mw.post("Contrib:" + move, contrib, "added %d images" % anything) |
if ONE_A_TIME and anything: | if ONE_A_TIME and anything: |
Latest revision as of 13:21, 1 November 2005
#!/usr/bin/python import urllib, urllib2, sys, re, os import mediawiki url3 = "http://allefant.sourceforge.net/wesnoth/graphiclibrary.py?acquire=1" NO_UPDATE = False COMMANDS_FILE = "" WIKI_FILE = "" VERBOSE = False ONE_A_TIME = False MOVE_ALL = False skip = 0 for i in range(len(sys.argv)): if skip: skip -= 1 else: if sys.argv[i] == "-n": NO_UPDATE = True elif sys.argv[i] == "-c": COMMANDS_FILE = sys.argv[i + 1] skip += 1 elif sys.argv[i] == "-w": WIKI_FILE = sys.argv[i + 1] skip += 1 elif sys.argv[i] == "-v": VERBOSE = True elif sys.argv[i] == "-o": ONE_A_TIME = True elif sys.argv[i] == "-a": MOVE_ALL = True if COMMANDS_FILE: commands = file(COMMANDS_FILE).read().splitlines() else: # read commands commands = urllib2.urlopen(url3).read().splitlines() if len(commands) == 0: sys.exit(0) commands.reverse() # that way, last (most recent) command has priority mw = mediawiki.MediaWiki() if WIKI_FILE: data = file(WIKI_FILE).read() else: # read current contents of wiki page data = mw.fetch("UnsortedContrib") stats = [0, 0] moves = {} modified = 0 def repl(mob): global commands, moves, modified, VERBOSE, NO_UPDATE, MOVE_ALL, stats item = mob.group(0) name = re.compile("""\\* \\[http://wesnoth.org/wiki/Contrib:.*? (.*?)\\]""").search(item).group(1) image = re.compile("\\| (http://.*) \\|").search(item).group(1) if VERBOSE: print name, image if mob: for com in commands: if com == "delete " + image: modified = 1 stats[0] += 1 if VERBOSE: print " delete" return "" elif (com == "move " + name + " " + image) or MOVE_ALL: modified = 1 stats[1] += 1 if name in moves: moves[name] += [image] else: moves[name] = [image] if VERBOSE: print " move" if not NO_UPDATE: return "" break # return unmodified return item data = re.compile("\\* \\[http://.*?\n", re.S).sub(repl, data) def check_already(name, link): mob = re.compile(".*/(.+\\.(?:png|gif|bmp|jpg))").search(link) pic = mob.group(1) f = "/home/elias/prog/python/wesnoth/glib/art/%s/%s" % (name, pic) try: file(f) return True except IOError: return False for move in moves: anything = 0 if VERBOSE: print move contrib = mw.fetch("Contrib:" + move) if contrib.strip() == "": # doesn't exist yet contrib = "== %s ==\n\n\n== See Also ==\n\n[[GraphicLibrary]]" % move miss = file("missing_artists.txt", "a") miss.write(move + "\n") if VERBOSE: print " creating new page" firstline = contrib.split("\n", 1)[0].lower() if firstline.find(move.lower()) < 0 and\ firstline.find(move.split(" ")[0].lower()) < 0: print "Right page? %s not in \"%s\"" % (move, firstline) contrib = "" if VERBOSE: print " page %s for %s" % (("Contrib:" + move), move) mob = re.compile("\\s*==.*?== *\n\n").search(contrib) if mob: newcontrib = contrib[:mob.end(0)] for pic in moves[move]: if not check_already(move, pic): newcontrib += pic + "\n" anything += 1 if VERBOSE: print " add: %s" % pic else: if VERBOSE: print " already: %s" % pic newcontrib += contrib[mob.end(0):] contrib = newcontrib else: print "Could not add %s for %s." % (moves[move], move) if anything and not NO_UPDATE: # Update contributor page mw.post("Contrib:" + move, contrib, "added %d images" % anything) if ONE_A_TIME and anything: sys.exit(0) if modified and WIKI_FILE: print "Writing data" file(WIKI_FILE, "w").write(data) if modified and not NO_UPDATE and not WIKI_FILE: # Update UnsortedContrib page mw.post("UnsortedContrib", data, "cleared %d and sorted %d images" % (stats[0], stats[1]))
This page was last edited on 1 November 2005, at 13:21.