Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- # AUTHOR: Kizar ( *redacted* ); Adapted by Wauteurz ( *redacted* )
- # To purge category pages, specifically Voy:en:Category:Banner missing from Wikidata
- # python pwb.py touch -lang:en -family:wikivoyage -cat:"Banner missing from Wikidata"
- # python pwb.py newitem -lang:en -family:wikivoyage -namespace:0 -pageage:0 -lastedit:0 -cat:"Banner missing from Wikidata"
- import sys, re, time
- import pwb # only needed if you haven't installed the framework as side-package
- import pywikibot
- from pywikibot import pagegenerators
- clean = re.compile('\\s*(.*\\S)?\\s*')
- def has_regexp(article, regexp):
- old = re.compile(regexp, re.UNICODE | re.DOTALL) # Important, but do not replace . with any other character
- tmp_article = pywikibot.replaceExcept(article, old, r'88888888888888888888999998888888888888888888', [])
- if article != tmp_article:
- return True
- else:
- return False
- def replace_regexp(article, regexp, chang):
- old = re.compile(regexp, re.UNICODE | re.DOTALL) # Important, but do not replace . with any other character
- return pywikibot.replaceExcept(article, old, chang, [])
- def search_regexp(article, regexp):
- old = re.compile(regexp, re.UNICODE | re.DOTALL) # Important, but do not replace . with any other character
- return re.search(old, article)
- def is_commons_img(img): # Returns true if image is in commons
- img_page = pywikibot.ImagePage(pywikibot.Site('commons', 'commons'), u'Image:'+img)
- if img_page.exists():
- return True
- else:
- return False
- site = pywikibot.Site('en', 'wikivoyage') # Any site will work, this is just an example.
- repo = pywikibot.Site("wikidata", "wikidata").data_repository() # This is a DataSite object
- cat = pywikibot.Category(site, 'Category:Banner missing from Wikidata')
- gen = pagegenerators.CategorizedPageGenerator(cat)
- for page in gen:
- if page.isRedirectPage() or page.isDisambig():
- continue
- pywikibot.output(page.title())
- try:
- item = pywikibot.ItemPage.fromPage(page) # This can be used for any page object
- except:
- print("!!! Doesn't exist in Wikidata")
- continue
- item.get() # You need to call it to access any data.
- if not 'P948' in item.claims:
- content = page.text
- if has_regexp(content, r'\{\{\s*[Pp]agebanner\s*\|\s*([^|}]+)\s*[|}]' ) == True: # When has correct image
- # Check if the image is in Commons
- photo = search_regexp(content, r'\{\{\s*[Pp]agebanner\s*\|\s*([^|}]+)\s*[|}]' ).group(1)
- photo = clean.match(photo).group(1) # Clean the weird stuff
- pywikibot.output(photo)
- if photo != 'Pagebanner default.jpg' and photo != 'Disambiguation banner.png' and photo != 'TT Banner.jpg' and photo != 'Mena-asia_default_banner.jpg' and != 'Other Destinations Wikivoyage banner.jpg' and != 'Welcome banner.jpg' and != 'Australia-oceania default banner.jpg' and != 'Carribean default banner.jpg' and != 'Disambiguation banner1.jpg' and != 'Itinerary banner.jpg' and != 'NZ default banner.jpg' and != 'S-amer africa default banner.jpg' and != 'TT Banner.jpg' and != 'Whole world North gy.jpg':
- #if is_commons_img(photo):
- img_page = pywikibot.ImagePage(pywikibot.Site('commons', 'commons'), u'Image:'+photo)
- if img_page.exists():
- claim = pywikibot.Claim(repo, 'P948')
- claim.setTarget(img_page)
- item.addClaim(claim)
- else:
- print("!!! Doesn't exist in commons")
- # Purging commands above
- page.save() # Purging cache
- time.sleep(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement