Guest User

Untitled

a guest
Feb 16th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. #Import neccessary modules
  2. from wikitools import *
  3. import time
  4. import urllib
  5. import json
  6. import userpass
  7. import warnings
  8.  
  9. site = wiki.Wiki() #Tell Python to use the English Wikipedia's API
  10. site.login(userpass.username, userpass.password) #login
  11.  
  12. #Find pages to delete
  13. def findpages():
  14.     #parameters for API request
  15.     params = {'action':'query',
  16.               'list':'categorymembers',
  17.               'cmtitle':'Category:Non-free files with orphaned versions more than 7 days old',
  18.               'cmlimit':'max'
  19.               }
  20.     req = api.APIRequest(site, params) #Set the API request
  21.     res = req.query(False) #Send the API request and store the result in res
  22.     touse = pagelist.listFromQuery(site, res['query']['categorymembers']) #Make a list
  23.     return touse
  24.  
  25. def versiontodelete(page):
  26.     params = {'action':'query',
  27.               'prop':'imageinfo',
  28.               'titles':page.unprefixedtitle,
  29.               'iiprop':'archivename',
  30.               'iilimit':'max',
  31.               'formatversion':'2'
  32.               }
  33.     req = api.APIRequest(site, params)
  34.     res = req.query(False)
  35.     whattodel = res['query']['pages'][0]['imageinfo'][1:] #Go into specifics, ignore first result (DatBot's reduced version)
  36.     for result in whattodel:
  37.         if 'filehidden' in result:
  38.             del result['filehidden'] #Remove any "filehidden" results
  39.     whattodel = filter(None, whattodel) # Remove empty results
  40.     return whattodel
  41.  
  42. def deletefile(page, version, token):
  43.     #Un-url-ify
  44.     params = {'action':'delete',
  45.               'title':page.unprefixedtitle,
  46.               'oldimage':version,
  47.               'token':token
  48.               }
  49.     api.APIRequest(site, params).query() #Actually delete it
  50.     print "Done"
  51.     return #Stop the function, ready for the next
  52.    
  53. def main():
  54.     pages = findpages()
  55.     for page in pages: #For page in the list
  56.         #try: #Try to delete the old revision(s)
  57.         todelete = versiontodelete(page)
  58.         print todelete
  59.         for version in todelete:
  60.             #Get a token
  61.             params = { 'action':'query', 'meta':'tokens' }
  62.             token = api.APIRequest(site, params).query()['query']['tokens']['csrftoken']
  63.             deletefile(page, version, token)
  64.         #except: #If there's an error, ignore the file
  65.             #pass
  66. if __name__ == "__main__":
  67.     with warnings.catch_warnings():
  68.         warnings.simplefilter("ignore", FutureWarning)
  69.         main()
Add Comment
Please, Sign In to add comment