Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Import neccessary modules
- from wikitools import *
- import time
- import urllib
- import json
- import userpass
- import warnings
- site = wiki.Wiki() #Tell Python to use the English Wikipedia's API
- site.login(userpass.username, userpass.password) #login
- #Find pages to delete
- def findpages():
- #parameters for API request
- params = {'action':'query',
- 'list':'categorymembers',
- 'cmtitle':'Category:Non-free files with orphaned versions more than 7 days old',
- 'cmlimit':'max'
- }
- req = api.APIRequest(site, params) #Set the API request
- res = req.query(False) #Send the API request and store the result in res
- touse = pagelist.listFromQuery(site, res['query']['categorymembers']) #Make a list
- return touse
- def versiontodelete(page):
- params = {'action':'query',
- 'prop':'imageinfo',
- 'titles':page.unprefixedtitle,
- 'iiprop':'archivename',
- 'iilimit':'max',
- 'formatversion':'2'
- }
- req = api.APIRequest(site, params)
- res = req.query(False)
- whattodel = res['query']['pages'][0]['imageinfo'][1:] #Go into specifics, ignore first result (DatBot's reduced version)
- for result in whattodel:
- if 'filehidden' in result:
- del result['filehidden'] #Remove any "filehidden" results
- whattodel = filter(None, whattodel) # Remove empty results
- return whattodel
- def deletefile(page, version, token):
- #Un-url-ify
- params = {'action':'delete',
- 'title':page.unprefixedtitle,
- 'oldimage':version,
- 'token':token
- }
- api.APIRequest(site, params).query() #Actually delete it
- print "Done"
- return #Stop the function, ready for the next
- def main():
- pages = findpages()
- for page in pages: #For page in the list
- #try: #Try to delete the old revision(s)
- todelete = versiontodelete(page)
- print todelete
- for version in todelete:
- #Get a token
- params = { 'action':'query', 'meta':'tokens' }
- token = api.APIRequest(site, params).query()['query']['tokens']['csrftoken']
- deletefile(page, version, token)
- #except: #If there's an error, ignore the file
- #pass
- if __name__ == "__main__":
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", FutureWarning)
- main()
Add Comment
Please, Sign In to add comment