Skip to content

Instantly share code, notes, and snippets.

@tobias47n9e
Created July 10, 2016 10:16
Show Gist options
  • Save tobias47n9e/79aef2a97e218a1812655c145db62a02 to your computer and use it in GitHub Desktop.
Save tobias47n9e/79aef2a97e218a1812655c145db62a02 to your computer and use it in GitHub Desktop.
import pywikibot
from pywikibot import pagegenerators as pg
from pywikibot import textlib
def list_template_usage(site_obj, tmpl_name):
"""
Takes Site object and template name and returns a generator.
The function expects a Site object (pywikibot.Site()) and
a template name (String). It creates a list of all
pages using that template and returns them as a generator.
The generator will load 50 pages at a time for iteration.
"""
name = "{}:{}".format(site.namespace(10), tmpl_name)
tmpl_page = pywikibot.Page(site, name)
ref_gen = pg.ReferringPageGenerator(tmpl_page, onlyTemplateInclusion=True)
generator = site.preloadpages(ref_gen, pageprops=True)
return generator
def look_for_template(page):
page_text = page.get()
tmplts = textlib.extract_templates_and_params(page_text)
for tmpl in tmplts:
print("-> Looking at template: ", tmpl[0])
if "gjer_g" in tmpl[1]:
print("--- Coordinates key found")
if tmpl[1]["gjer_g"] == "":
print("!!!! Coordinates value missing")
else:
print("xxxx Coordinates found")
template_name = "Infobox vendbanim"
site = pywikibot.Site("sq", 'wikipedia')
tmpl_gen = list_template_usage(site, template_name)
count = 0
for page in tmpl_gen:
print("--------------> ", page.title(), " <--------------")
look_for_template(page)
if count == 10:
break
count += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment