User:MordantMaenad/scripts/masscat.py

From RationalWiki
Jump to navigation Jump to search
"""
Nifty little interface for mass-adding, mass-removing, or mass-replacing of categories. An application of the pywikipedia library.
Lets you input a list of pages for the particular action you choose.
"""
import wikipedia
from Tkinter import *
site = wikipedia.getSite('en', 'rationalwiki')
class App:
    def __init__(self, master):
        master.title("Category interface")
        master.maxsize(424,627)
        master.minsize(424,627)
        self.catboxlbl = Label(master, text="Category:")
        self.catboxlbl.grid(column=1, padx=3, pady=5, row=1, columnspan=1)
        self.catbox = Entry(master)
        self.catbox.grid(column=2, padx=6, pady=5, row=1, columnspan=1)
        self.lb = Label(master, text="Enter the names of categories here, separated by newlines:")
        self.lb.grid(column=1, padx=6, pady=5, row=2, columnspan=3)
        self.entrybox = Text(master, height=30, width=50)
        self.entrybox.grid(column=1, padx=8, pady=5, row=3, columnspan=3)
        self.addb = Button(master, text="Add this cat", command=self.addcat, width=15, height=1)
        self.addb.grid(column=1, padx=5, pady=5, row=4)
        self.removeb = Button(master, text="Remove this cat", command=self.removecat, width=15, height=1)
        self.removeb.grid(column=2, padx=5, pady=5, row=4)
        self.replaceb = Button(master, text="Swap this cat for:", command=self.swapcat, width=15, height=1)
        self.replaceb.grid(column=3, padx=5, pady=5, row=4)
        self.swapbox = Entry(master)
        self.swapbox.grid(column=3, padx=3, pady=1, row=5)
    def interpret(self, text):
        result = []
        for line in text.splitlines(1):
            if line.endswith('\n'):
                result.append(line[:-1])
            else:
                result.append(line)
        return result
    def addcat(self):
        contents = self.entrybox.get(1.0, END)
        cat = unicode(self.catbox.get())
        pages = self.interpret(contents)
        for page in pages:
            try:
                rwpage = wikipedia.Page(site, unicode(page))
                text = rwpage.get()
            except:
                continue
            if u'[[Category:' + cat + u']]' not in text and u'[[category:' + cat + u']]' not in text:
                text += u'\n[[Category:' + cat + u']]'
                rwpage.put(text, u'Bot: added [[:Category:' + cat + u']] using [[User:Blue/scripts/masscat.py|BlueCat]]', False, True)
    def removecat(self):
        contents = self.entrybox.get(1.0, END)
        cat = unicode(self.catbox.get())
        pages = self.interpret(contents)
        for page in pages:
            try:
                rwpage = wikipedia.Page(site, unicode(page))
                text = rwpage.get()
            except:
                continue
            if u'[[Category:' + cat + u']]' in text or u'[[category:' + cat + u']]' in text:
                text = text.replace(u'[[Category:' + cat + u']]', u'')
                text = text.replace(u'[[category:' + cat + u']]', u'')
                rwpage.put(text, u'Bot: removed category [[:Category:' + cat + u']] using [[User:Blue/scripts/masscat.py|BlueCat]]', False, True)
    def swapcat(self):
        contents = self.entrybox.get(1.0, END)
        cat = unicode(self.catbox.get())
        newcat = self.swapbox.get()
        if newcat != '':
            pages = self.interpret(contents)
            for page in pages:
                try:
                    rwpage = wikipedia.Page(site, unicode(page))
                    text = rwpage.get()
                except:
                    continue
                if u'[[Category:' + cat + u']]' in text or u'[[category:' + cat + u']]' in text:
                    text = text.replace(u'[[Category:' + cat + u']]', u'[[Category:' + newcat + u']]')
                    text = text.replace(u'[[category:' + cat + u']]', u'[[Category:' + newcat + u']]')
                    rwpage.put(text, u'Bot: replaced category [[:Category:' + cat + u']] with [[:Category:' + newcat + u']] using [[User:Blue/scripts/masscat.py|BlueCat]]', False, True)
root = Tk()
app = App(root)
root.mainloop()