Guest User

Untitled

a guest
Jun 23rd, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.62 KB | None | 0 0
  1. # © 2011 Betacommand
  2. # used with permission => Addihockey10
  3. import catlib
  4. import codecs
  5. import image
  6. import pagegenerators
  7. import re
  8. import sys
  9. import time
  10. import wikipedia
  11. site = wikipedia.Site('commons',"commons")
  12.  
  13.  
  14. def main():
  15.   # define a list of namespaces where we don't want to replace the file
  16.   skipns = [1,2,3,5,7,9,11,13,15,101,109]
  17.   # grab a list of already confirmed orphaned files that there is no need to re-check
  18.   checked = get_lines('globalusage.log')
  19.   # grab the list of files from a category on commons
  20.   gen = pagegenerators.CategorizedPageGenerator(catlib.Category(site,u'Category:Vector version available'),start=u'600px Amaranto2.png')
  21.   for page in gen:
  22.     # check to ensure that the page we are working on is in fact an image and hasn't already been checked
  23.     if page.namespace() == 6 and page.title() not in checked:
  24.       wikipedia.output(page.title())
  25.       # get the file discription page contents
  26.       text = page.get()
  27.       # look for the template and the new file name.
  28.       new = re.search(ur'\{\{ ?(Vector version available|vva|NowSVG|SupersededSVG|SVG available|vectorversionavailable) ?\|(.*?)\}\}',text,re.I).group(2)
  29.       # depending on how the template functions it sometimes includes "File:" or "Image:" go ahead and strip that out
  30.       new = re.sub(ur'([Ff]ile|[Ii]mage):','',new,re.I)
  31.       new_image = wikipedia.Page(site,u'File:%s' % new)
  32.       # confirm that the new file actually exists
  33.       if new_image.exists():
  34.         # work on all uses of the file
  35.         for pg in page.globalUsage():
  36.           # excluding the skipped namespaces
  37.           if pg.namespace() not in skipns and not re.search(ur'(WikiProject Chemistry\/Image|Graphic Lab\/|[Aa]rchive|Featured picture candidates|[Aa]rchivio)',pg.title(),re.I):
  38.             wikipedia.output(pg.title())
  39.             # send the work off to image.py to do the replacements one page at a time as it doesn't handle cross-wiki too well
  40.             bot = image.ImageRobot([pg],page.titleWithoutNamespace(),re.sub(u'_',u' ',new),loose=True)
  41.             bot.run()
  42.             # add in a second check to avoid self edit conflicts due to replace.py's put_async feature
  43.             while not wikipedia.page_put_queue.empty():
  44.               time.sleep(1)
  45.             # just to be safe lets wait two more seconds
  46.             time.sleep(2)
  47.         # now that we have finished with a file lets double check its an orphan
  48.         list = [x for x in page.globalUsage()]
  49.         list2 = []
  50.         for item in list:
  51.           if item.namespace() not in skipns:
  52.               list2.append(item)
  53.         # file hasnt been used so lets exclude it from all further checks
  54.         if len(list2) == 0:
  55.           log(u'\n%s' % page.title(),'globalusage.log')
  56.         else:
  57.           # for some reason the file wasn't correctly orphaned lets log it for further human review later
  58.           log(u'\n%s' % page.title(),'globalusage_error.log')
  59.       else:
  60.         # the given target file doesnt exist. logging this odd case for human review
  61.         log(u'\n%s' % page.title(),'globalusage_missing.log')
  62.  
  63. # basic function to retrieve a file and convert it into a usable python list: each line is an item in the list
  64. def get_lines(file):
  65.   try:
  66.     f = codecs.open(file,'r', 'utf-8')
  67.     items = f.read().split('\n')
  68.     f.close()
  69.     try:
  70.       items.remove('')
  71.     except:
  72.       pass
  73.   except:
  74.     items = []
  75.   return items
  76.  
  77. # basic function for saving information to a file
  78. def log(text,file):
  79.   f = codecs.open(file, 'a', 'utf-8')
  80.   f.write(text)
  81.   f.close()
  82.  
  83. if __name__ == "__main__":
  84.   try:
  85.     main()
  86.   finally:
  87.     wikipedia.stopme()
Add Comment
Please, Sign In to add comment