Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.30 KB | None | 0 0
  1. # -*- coding: utf-8  -*-
  2. # AUTHOR: Kizar (ngosang@hotmail.es)
  3. # para purgar las páginas de la categoría
  4. # python pwb.py touch -lang:en -family:wikivoyage -cat:"Banner missing from Wikidata"
  5. # python pwb.py newitem -lang:en -family:wikivoyage -namespace:0 -pageage:0 -lastedit:0 -cat:"Banner missing from Wikidata"
  6.  
  7. import sys, re, time
  8.  
  9. import pwb  # only needed if you haven't installed the framework as side-package
  10. import pywikibot
  11. from pywikibot import pagegenerators
  12.  
  13. clean = re.compile('\\s*(.*\\S)?\\s*')
  14.  
  15. def tiene_regexp(articulo, regexp):
  16.     old = re.compile(regexp, re.UNICODE | re.DOTALL) # Importante, sino no toma el . como cualquier caracter
  17.     tmp_articulo = pywikibot.replaceExcept(articulo, old, r'88888888888888888888999998888888888888888888', [])
  18.     if articulo != tmp_articulo:
  19.         return True
  20.     else:
  21.         return False
  22.  
  23. def remplaza_regexp(articulo, regexp, chang):
  24.     old = re.compile(regexp, re.UNICODE | re.DOTALL) # Importante, sino no toma el . como cualquier caracter
  25.     return pywikibot.replaceExcept(articulo, old, chang, [])
  26.  
  27. def busca_regexp(articulo, regexp):
  28.     old = re.compile(regexp, re.UNICODE | re.DOTALL) # Importante, sino no toma el . como cualquier caracter
  29.     return re.search(old, articulo)
  30.    
  31. def is_commons_img(img): #Devuelve verdadero si la imagen se encuentra en commons
  32.     img_page = pywikibot.ImagePage(pywikibot.Site('commons', 'commons'), u'Image:'+img)
  33.     if img_page.exists():
  34.         return True
  35.     else:
  36.         return False
  37.  
  38. site = pywikibot.Site('en', 'wikivoyage')  # any site will work, this is just an example
  39. repo = pywikibot.Site("wikidata", "wikidata").data_repository()  # this is a DataSite object
  40.  
  41. cat = pywikibot.Category(site, 'Category:Banner missing from Wikidata')
  42. gen = pagegenerators.CategorizedPageGenerator(cat)
  43. for pagina in gen:
  44.     if pagina.isRedirectPage() or pagina.isDisambig():
  45.         continue
  46.     pywikibot.output(pagina.title())
  47.    
  48.     try:
  49.         item = pywikibot.ItemPage.fromPage(pagina)  # this can be used for any page object
  50.     except:
  51.         print("!!! No existe en Wikidata")
  52.         continue
  53.     item.get()  # you need to call it to access any data.
  54.    
  55.     if not 'P948' in item.claims:
  56.         content = pagina.text
  57.  
  58.         if tiene_regexp(content, r'\{\{\s*[Pp]agebanner\s*\|\s*([^|}]+)\s*[|}]' ) == True: # Si tiene imagen perfect
  59.             # Comprobamos si la imagen está en commons
  60.             imagen = busca_regexp(content, r'\{\{\s*[Pp]agebanner\s*\|\s*([^|}]+)\s*[|}]' ).group(1)
  61.             imagen = clean.match(imagen).group(1) # Limpiar todo lo raro
  62.             pywikibot.output(imagen)
  63.             if imagen != 'Pagebanner default.jpg' and imagen != 'Disambiguation banner.png' and imagen != 'TT Banner.jpg' and imagen != 'Mena-asia_default_banner.jpg':
  64.                 #if is_commons_img(imagen):
  65.                 img_page = pywikibot.ImagePage(pywikibot.Site('commons', 'commons'), u'Image:'+imagen)
  66.                 if img_page.exists():
  67.                     claim = pywikibot.Claim(repo, 'P948')
  68.                     claim.setTarget(img_page)
  69.                     item.addClaim(claim)
  70.                 else:
  71.                     print("!!! No existe en Commons")
  72.  
  73.     # para purgar utilizar los comandos de arriba
  74.     pagina.save() # purge cache
  75.     time.sleep(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement