Created
March 5, 2013 09:54
-
-
Save legoktm/5089173 to your computer and use it in GitHub Desktop.
A script using Pywikipedia's trunk version to screenscrape to mass-revdel edits.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Copyright (C) 2013 Legoktm | |
Released under MIT License | |
""" | |
####CONFIG | |
filename = 'links.txt' | |
reason = 'Bad info' | |
####END CONFIG | |
import urlparse | |
import webbrowser | |
import wikipedia as w | |
site = w.getSite('en','wikipedia') | |
def revdel(site, revid, pagename, reason, token=None): | |
if not token: | |
token = gettoken(site) | |
address = site.put_address('Special:RevisionDelete') | |
params = dict() | |
params['wpHidePrimary'] = '1' | |
params['wpHideComment'] = '-1' | |
params['wpHideUser'] = '-1' | |
params['wpHideRestricted'] = '1' | |
params['wpRevDeleteReasonList'] = 'other' | |
params['wpReason'] = reason | |
params['wpEditToken'] = token | |
params['target'] = pagename | |
params['type'] = 'revision' | |
params['ids'] = str(revid) | |
params['wpSubmit'] = 'Apply to selected revisions' | |
response, data = site.postForm(address, params) | |
print response.code | |
if response.code != 200: | |
print data | |
quit() | |
#print data | |
def gettoken(site): | |
#prop=info&intoken=delete&titles=Main Page&format=jsonfm | |
params = {'action':'query', | |
'prop':'info', | |
'intoken':'delete', | |
'titles':'kjhkajsdhfksdfhsjdhfgsdfjh' #yeah... | |
} | |
data = w.query.GetData(params, site) | |
return data['query']['pages']['-1']['deletetoken'] | |
def read_from_file(filename): | |
with open(filename, 'r') as f: | |
text = f.read() | |
lines = text.splitlines() | |
for line in lines: | |
if not line.strip(): | |
continue | |
query = urlparse.urlparse(line.strip()).query | |
data = urlparse.parse_qs(query) | |
yield data['title'][0], data['oldid'][0], line.strip() | |
def main(): | |
#fetch the token | |
token = gettoken(site) | |
gen = read_from_file(filename) | |
for title, revid, url in gen: | |
webbrowser.open(url) | |
x=raw_input('Please enter "y" to continue. "s" to skip. Anything else to quit: ') | |
if x.lower().strip() == 'y': | |
revdel(site, revid, title, reason, token) | |
elif x.lower().strip() == 's': | |
print 'Skipping this one.' | |
else: | |
quit() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment