Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urllib
- import urllib2
- import ClientCookie
- from Tkinter import *
- from tkMessageBox import *
- from xml.dom.minidom import parse, parseString
- cookies = ClientCookie.CookieJar()
- opener = ClientCookie.build_opener(ClientCookie.HTTPCookieProcessor(cookies))
- opener.addheaders = [("User-agent", "WpCpEdit/1.0")]
- ClientCookie.install_opener(opener)
- class WpLoginDialog(Tk):
- def __init__(self):
- Tk.__init__(self)
- self.title("Login to Wikipedia")
- self.createElements()
- def createElements(self):
- userFrame = Frame(self)
- userFrame.pack()
- passwdFrame = Frame(self)
- passwdFrame.pack()
- Label(userFrame, text = "Username: ").pack(side = LEFT)
- Label(passwdFrame, text = "Password: ").pack(side = LEFT)
- self.userBox = Entry(userFrame)
- self.userBox.pack(side = RIGHT)
- self.passwdBox = Entry(passwdFrame, show = "*")
- self.passwdBox.pack(side = RIGHT)
- Button(self, text = "Login", command = self.login).pack()
- self.mainloop()
- def login(self):
- loginInformation = {'lgname': self.userBox.get(), 'lgpassword': self.passwdBox.get()}
- loginInformation = urllib.urlencode(loginInformation)
- loginRequest = urllib2.Request("http://en.wikipedia.org/w/api.php?format=xml&action=login", loginInformation)
- loginResponse = ClientCookie.urlopen(loginRequest).read().split('token="')[1].split('"')[0]
- loginInformation = {'lgname': self.userBox.get(), 'lgpassword': self.passwdBox.get()}
- loginInformation['lgtoken'] = loginResponse
- loginInformation = urllib.urlencode(loginInformation)
- loginInformation = urllib2.Request("http://en.wikipedia.org/w/api.php?format=xml&action=login", loginInformation)
- output = ClientCookie.urlopen(loginInformation).read()
- if "Success" in output:
- self.destroy()
- else:
- showerror("Login Error", "Incorrect username or password entered.")
- class CategoryGetter:
- def __init__(self, category):
- self.category = category
- self.queryurl = "http://en.wikipedia.org/w/api.php?action=query&format=xml&list=categorymembers&cmlimit=500&cmtitle=" + urllib.quote("Category:" + category)
- self.articles = []
- self.getArticles()
- def getArticles(self, cmcontinue = 0):
- url = self.queryurl
- if cmcontinue != 0:
- url = url + "&cmcontinue=" + cmcontinue
- response = ClientCookie.urlopen(url).read()
- dom = parseString(response)
- pages = [i.attributes['title'].value for i in dom.getElementsByTagName("cm")]
- self.articles += pages
- if "categorymembers cmcontinue" in response:
- continueid = response.split('<categorymembers cmcontinue="')[1].split('"')[0]
- print continueid
- self.getArticles(continueid)
- class ArticleSelector(Tk):
- def __init__(self, articles):
- self.articles = articles
- Tk.__init__(self)
- self.title("WpCpEdit 1.0")
- self.createElements()
- def encoded_dict(self, in_dict):
- out_dict = {}
- for k, v in in_dict.iteritems():
- if isinstance(v, unicode):
- v = v.encode('utf8')
- elif isinstance(v, str):
- # Must be encoded in UTF-8
- v.decode('utf8')
- out_dict[k] = v
- return out_dict
- def createElements(self):
- editframe = Frame(self)
- editframe.pack()
- self.lb = Listbox(editframe, width=50, height = 30)
- self.lb.pack(side = LEFT)
- self.lb.insert(END, "User:Fox Wilson/sandbox")
- for article in self.articles:
- self.lb.insert(END, article)
- self.lb.bind("<1>", self.loadArticle)
- self.textbox = Text(editframe, width = 80, height = 30)
- self.textbox.pack(side = RIGHT)
- Button(self, text = "Save Page", command = self.makeEdit).pack(side=BOTTOM)
- self.mainloop()
- def loadArticle(self, e):
- name = self.lb.get(self.lb.curselection()[0])
- self.article = name
- url = "http://en.wikipedia.org/w/api.php?action=query&format=json&titles="+urllib.quote(name)+"&prop=revisions&rvprop=content"
- articleContent = ClientCookie.urlopen(url).read().split('"*":"')[1].split('"}')[0].replace("\\n", "\n").decode("utf-8")
- articleContent = unicode(articleContent)
- print articleContent
- self.textbox.delete(1.0, END)
- self.textbox.insert(END, articleContent)
- def makeEdit(self):
- token = ClientCookie.urlopen("http://en.wikipedia.org/w/api.php?action=tokens&format=xml").read().split('edittoken="')[1].split('" />')[0]
- summary = "Copyediting page with [[User:Fox Wilson/WpCpEdit|WpCpEdit]] - this tool is currently being tested. If this edit i.e. blanked a page, please revert it and let [[User talk:Fox Wilson|Fox Wilson]] know."
- edit = self.textbox.get(1.0, END)
- data = self.encoded_dict({"format": "xml", "action": "edit", "token": token, "summary": summary, "text": edit, "bot": "true", "title": self.article})
- #data = dict([(key, value.encode('utf8')) for key, value in data.iteritems()])
- data["text"] = data["text"].replace("\\u", "\u")
- data["text"] = unicode(data["text"])
- editInfo = urllib2.Request("http://en.wikipedia.org/w/api.php", urllib.urlencode(data))
- response = ClientCookie.urlopen(editInfo).read() #Get the response
- WpLoginDialog()
- articles = CategoryGetter("All articles needing copy edit").articles
- ArticleSelector(articles)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement