import pywikibot
from pywikibot import pagegenerators as pg
from pywikibot import textlib

def list_template_usage(site_obj, tmpl_name):
    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:
    test_page = pywikibot.Page(site, 'Përdoruesi:Liridon/Livadhi/Lista')
    test_page.text = "#[[" + page.title() + "]]" + test_page.text
    test_page.save()
    if count == 5000:
        break
    count += 1
Retrieving 50 pages from wikipedia:sq.
Sleeping for 9.2 seconds, 2016-07-23 13:37:02
Page [[sq:Përdoruesi:Liridon/Livadhi/Lista]] saved
Sleeping for 9.7 seconds, 2016-07-23 13:37:11
Page [[sq:Përdoruesi:Liridon/Livadhi/Lista]] saved
Sleeping for 9.7 seconds, 2016-07-23 13:37:21
Page [[sq:Përdoruesi:Liridon/Livadhi/Lista]] saved
Sleeping for 9.7 seconds, 2016-07-23 13:37:31
Page [[sq:Përdoruesi:Liridon/Livadhi/Lista]] saved
Sleeping for 9.7 seconds, 2016-07-23 13:37:41
Page [[sq:Përdoruesi:Liridon/Livadhi/Lista]] saved
Sleeping for 9.7 seconds, 2016-07-23 13:37:51
Page [[sq:Përdoruesi:Liridon/Livadhi/Lista]] saved
Sleeping for 9.7 seconds, 2016-07-23 13:38:01
Page [[sq:Përdoruesi:Liridon/Livadhi/Lista]] saved
Sleeping for 9.7 seconds, 2016-07-23 13:38:11
Page [[sq:Përdoruesi:Liridon/Livadhi/Lista]] saved
Sleeping for 9.7 seconds, 2016-07-23 13:38:21