import pywikibot
from pywikibot import pagegenerators as pg
import sys

commit=False
verbose=True
srclng='nl'
srcsite=srclng+'wiki'
listi='Q28197061'
listg='Q13406463'

def handle_P31(wd):
  lg=li=False
  if (srclng in wd.labels):
    print(f'{wd.labels[srclng]}')
  else:
    print(f'No label for {wd.title()}')
  if ('P31' in wd.claims):
   for x in wd.claims('P31'):
    li=li or (x.title==listi)
    lg=lg or (x.title==listg)
  if (not li):  
     pass #add P31=listi ... set li TRUE
  if (li and lg):
    pass #delete listg

def check_landen(wd):
  land1=''
  land2=''
  if ('P17' in wd.claims):
    x=wd.claims('P17')
    print(f'Aantal landen: {len(x)}')
  else:      
    pass #landen zoeken en toevoegen uit wikitext
          
def action_one_page(wd):
  handle_P31(wd)
  check_landen(wd)
  return(1)

def all_from_cat(thissite,thiscat):
 print('Start')   
 noodstop=0
 site=pywikibot.Site(thissite)
 cat = pywikibot.Category(site,thiscat)
 gen = pg.CategorizedPageGenerator(cat,12)
 for onepage in gen:
   if ('wikibase_item' in onepage.properties()):
        wd=onepage.data_item()
        wd.get(get_redirect=True)
        #print('xxx: %s'%wd.title())
        noodstop=noodstop+action_one_page(wd)
        if (noodstop>3): 
           exit()
        #print(f'Noodstop={noodstop}')
 print('Klaar')

def test_one():
  repo=pywikibot.Site('wikidata','wikidata').data_repository()
  wd=pywikibot.ItemPage(repo,'Q50809235')
  wd.get(get_redirect=True)
  action_one_page(wd)

#all_from_cat('Lijsten van voetbalinterlands')
#all_from_cat(srclng, 'Lijsten van voetbalinterlands Tahiti')
test_one()
Lijst van voetbalinterlands Tahiti - Tonga
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-5da5161c2dec> in <module>
     64 #all_from_cat('Lijsten van voetbalinterlands')
     65 #all_from_cat(srclng, 'Lijsten van voetbalinterlands Tahiti')
---> 66 test_one()

<ipython-input-9-5da5161c2dec> in test_one()
     60   wd=pywikibot.ItemPage(repo,'Q50809235')
     61   wd.get(get_redirect=True)
---> 62   action_one_page(wd)
     63 
     64 #all_from_cat('Lijsten van voetbalinterlands')

<ipython-input-9-5da5161c2dec> in action_one_page(wd)
     35 
     36 def action_one_page(wd):
---> 37   handle_P31(wd)
     38   check_landen(wd)
     39   return(1)

<ipython-input-9-5da5161c2dec> in handle_P31(wd)
     17     print(f'No label for {wd.title()}')
     18   if ('P31' in wd.claims):
---> 19    for x in wd.claims('P31'):
     20     li=li or (x.title==listi)
     21     lg=lg or (x.title==listg)

TypeError: 'dict' object is not callable
print(wd)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-10-914112b0d1ab> in <module>
----> 1 print(wd)

NameError: name 'wd' is not defined