Advertisement
Guest User

Untitled

a guest
Jan 13th, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.96 KB | None | 0 0
  1. import urllib
  2. import urllib2
  3. import ClientCookie
  4. from Tkinter import *
  5. from tkMessageBox import *
  6. from xml.dom.minidom import parse, parseString
  7. cookies = ClientCookie.CookieJar()
  8. opener = ClientCookie.build_opener(ClientCookie.HTTPCookieProcessor(cookies))
  9. opener.addheaders = [("User-agent", "WpCpEdit/1.0")]
  10. ClientCookie.install_opener(opener)
  11. class WpLoginDialog(Tk):
  12.     def __init__(self):
  13.         Tk.__init__(self)
  14.         self.title("Login to Wikipedia")
  15.         self.createElements()
  16.     def createElements(self):
  17.         userFrame = Frame(self)
  18.         userFrame.pack()
  19.         passwdFrame = Frame(self)
  20.         passwdFrame.pack()
  21.         Label(userFrame, text = "Username: ").pack(side = LEFT)
  22.         Label(passwdFrame, text = "Password: ").pack(side = LEFT)
  23.         self.userBox = Entry(userFrame)
  24.         self.userBox.pack(side = RIGHT)
  25.         self.passwdBox = Entry(passwdFrame, show = "*")
  26.         self.passwdBox.pack(side = RIGHT)
  27.         Button(self, text = "Login", command = self.login).pack()
  28.         self.mainloop()
  29.     def login(self):
  30.         loginInformation = {'lgname': self.userBox.get(), 'lgpassword': self.passwdBox.get()}
  31.         loginInformation = urllib.urlencode(loginInformation)
  32.         loginRequest = urllib2.Request("http://en.wikipedia.org/w/api.php?format=xml&action=login", loginInformation)
  33.         loginResponse = ClientCookie.urlopen(loginRequest).read().split('token="')[1].split('"')[0]
  34.         loginInformation = {'lgname': self.userBox.get(), 'lgpassword': self.passwdBox.get()}
  35.         loginInformation['lgtoken'] = loginResponse
  36.         loginInformation = urllib.urlencode(loginInformation)
  37.         loginInformation = urllib2.Request("http://en.wikipedia.org/w/api.php?format=xml&action=login", loginInformation)
  38.         output = ClientCookie.urlopen(loginInformation).read()
  39.         if "Success" in output:
  40.             self.destroy()
  41.         else:
  42.             showerror("Login Error", "Incorrect username or password entered.")
  43. class CategoryGetter:
  44.     def __init__(self, category):
  45.         self.category = category
  46.         self.queryurl = "http://en.wikipedia.org/w/api.php?action=query&format=xml&list=categorymembers&cmlimit=500&cmtitle=" + urllib.quote("Category:" + category)
  47.         self.articles = []
  48.         self.getArticles()
  49.     def getArticles(self, cmcontinue = 0):
  50.         url = self.queryurl
  51.         if cmcontinue != 0:
  52.             url = url + "&cmcontinue=" + cmcontinue
  53.         response = ClientCookie.urlopen(url).read()
  54.         dom = parseString(response)
  55.         pages = [i.attributes['title'].value for i in dom.getElementsByTagName("cm")]
  56.         self.articles += pages
  57.         if "categorymembers cmcontinue" in response:
  58.             continueid = response.split('<categorymembers cmcontinue="')[1].split('"')[0]
  59.             print continueid
  60.             self.getArticles(continueid)
  61. class ArticleSelector(Tk):
  62.     def __init__(self, articles):
  63.         self.articles = articles
  64.         Tk.__init__(self)
  65.         self.title("WpCpEdit 1.0")
  66.         self.createElements()
  67.     def encoded_dict(self, in_dict):
  68.         out_dict = {}
  69.         for k, v in in_dict.iteritems():
  70.             if isinstance(v, unicode):
  71.                 v = v.encode('utf8')
  72.             elif isinstance(v, str):
  73.                 # Must be encoded in UTF-8
  74.                 v.decode('utf8')
  75.             out_dict[k] = v
  76.         return out_dict
  77.     def createElements(self):
  78.         editframe = Frame(self)
  79.         editframe.pack()
  80.         self.lb = Listbox(editframe, width=50, height = 30)
  81.         self.lb.pack(side = LEFT)
  82.         self.lb.insert(END, "User:Fox Wilson/sandbox")
  83.         for article in self.articles:
  84.             self.lb.insert(END, article)
  85.         self.lb.bind("<1>", self.loadArticle)
  86.         self.textbox = Text(editframe, width = 80, height = 30)
  87.         self.textbox.pack(side = RIGHT)
  88.         Button(self, text = "Save Page", command = self.makeEdit).pack(side=BOTTOM)
  89.         self.mainloop()
  90.     def loadArticle(self, e):
  91.         name = self.lb.get(self.lb.curselection()[0])
  92.         self.article = name
  93.         url = "http://en.wikipedia.org/w/api.php?action=query&format=json&titles="+urllib.quote(name)+"&prop=revisions&rvprop=content"
  94.         articleContent = ClientCookie.urlopen(url).read().split('"*":"')[1].split('"}')[0].replace("\\n", "\n").decode("utf-8")
  95.         articleContent = unicode(articleContent)
  96.         print articleContent
  97.         self.textbox.delete(1.0, END)
  98.         self.textbox.insert(END, articleContent)
  99.     def makeEdit(self):
  100.         token = ClientCookie.urlopen("http://en.wikipedia.org/w/api.php?action=tokens&format=xml").read().split('edittoken="')[1].split('" />')[0]
  101.         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."
  102.         edit = self.textbox.get(1.0, END)
  103.         data = self.encoded_dict({"format": "xml", "action": "edit", "token": token, "summary": summary, "text": edit, "bot": "true", "title": self.article})
  104.         #data = dict([(key, value.encode('utf8')) for key, value in data.iteritems()])
  105.         data["text"] = data["text"].replace("\\u", "\u")
  106.         data["text"] = unicode(data["text"])
  107.         editInfo = urllib2.Request("http://en.wikipedia.org/w/api.php", urllib.urlencode(data))
  108.         response = ClientCookie.urlopen(editInfo).read() #Get the response
  109. WpLoginDialog()
  110. articles = CategoryGetter("All articles needing copy edit").articles
  111. ArticleSelector(articles)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement