import pywikibot
from pywikibot import pagegenerators
import mwparserfromhell
from mwparserfromhell.nodes import Template

useSite='nl'
useCategory={'nl':'Categorie:Tennisser naar nationaliteit'}
useTarget={'nl':'Gebruiker:Edoderoo/Tennis-stats','de':'Benutzer:Edoderoo/Tennis-stats','en':'User:Edoderoo/Tennis-stats'}

intro={'nl':'Deze pagina is automatisch aangemaakt. Wijzig het niet handmatig.','de':'','en':'This page is created automatically by a script. Please do not change it manually.','fr':''}
infobox_title={'nl':'Infobox tennisspeler','de':'Infobox Tennisspieler','en':'Infobox tennis biography','fr':'Infobox Joueur de tennis'}
infobox_singles={'nl':'enkelrecord','de':'einzelbilanz','en':'singlesrecord','fr':''}
infobox_prize={'nl':'prijzengeld', 'de':'preisgeld', 'en':'careerprizemoney', 'fr':'gains en tournois'}
infobox_doubles={'nl':'dubbelrecord','de':'doppelbilanz','en':'doublesrecord','fr':''}
infobox_image={'nl':'afbeelding','de':'bild','en':'image','fr':'image'}
table_start='{| class="wikitable sortable"\n|-\n! Type !! Item !! diff !! binary \n|-'
table_end='\n|}\n'
max_man=99
max_man_dollar=50000
max_woman=199

def smallest(x):
  smallval=x[0]
  for y in x:
    if y<smallval:
      smallval=y
  return smallval

def getCategory(page,text2find=None):
  catfound = None
  for c in page.categories():
    if not(text2find==None):
      if ((c.title().upper().find(text2find.upper())) >= 0):
        catfound = c.title()[10:]
        break
    else: 
      catfound=c.title()[10:] 
      break
    
  return catfound

def getTemplate(page,template):                #find one template on a page
  wikicode=mwparserfromhell.parse(page.text)     
  templates=wikicode.filter_templates()
  mytemplates = [x for x in templates if x.name.matches(template)]
  if (len(mytemplates)>0):
    for returntemplate in mytemplates:
      yield  returntemplate                    #return template we searched for  

def get_int_from_str(mystr):
  numbers='0123456789'
  ok=numbers+'.,'
  start=0
  while (start<len(mystr)) and (not(mystr[start] in numbers)):
    start+=1
  stop=start
  result=''
  while (stop<len(mystr)) and (mystr[stop] in ok):
    if (mystr[stop] in numbers):
      result+=mystr[stop]
    stop+=1
  if (result==''):
    return 0
  return int(result)

def strip_from_till(mystr, fromstr, tillstr):
  start=mystr.find(fromstr)+len(fromstr)
  end=mystr.find(tillstr)
  if (start<end):
    return mystr[start:end]
  return mystr

def strip_stuff_from_str(mystr):
  mystr=mystr.lower().replace('[[amerikaanse dollar|us$]]','').replace('[[amerikaanse dollar|$]]','')
  mystr=mystr.replace('[[US$]]','')
  mystr=strip_from_till(mystr,'<!--','-->')
  mystr=strip_from_till(mystr,'<ref>','</ref>')
  return mystr.strip()  

def get_values(lng,pagetxt):
  dollars_value=-1
  image_found=False
  for onetemplate in getTemplate(pagetxt,infobox_title[lng]):
    for oneparam in onetemplate.params:
      paramname=oneparam.name.strip().lower()
      if (paramname in [infobox_prize[lng],infobox_singles[lng],infobox_doubles[lng],infobox_image[lng]]):
        thisvalue = strip_stuff_from_str(oneparam.value)
        if (paramname==infobox_image[lng]):
          image_found=image_found or (thisvalue!='')
        if (paramname==infobox_prize[lng]):
          if (thisvalue.find('p2121') != -1): #not in use right now on de-wiki, but let's hope they mature ;-)
            dollars_value=0
          else:
            dollars_value = get_int_from_str(thisvalue)
  return dollars_value, image_found

def all_from_cat(thissite,thiscat):

 site=pywikibot.Site(thissite)
 cat = pywikibot.Category(site,thiscat)
 gen = pagegenerators.CategorizedPageGenerator(cat,12)
 wdsite=pywikibot.Site('wikidata','wikidata')
 repo=wdsite.data_repository()
 total_women=found_woman=total_men=found_man=0
 
 en_site=pywikibot.Site('en')
 de_site=pywikibot.Site('de') 
 fr_site=pywikibot.Site('fr')
 mantxt=femtxt=''
 
 for src_page in gen: 
   if ('wikibase_item' in src_page.properties()):
     man=woman=False
     wd_dd = -1.11
     de_dollars_value=en_dollars_value=fr_dollars_value=0
     de_image=en_image=fr_image=False
     indicator='$'
     wd=src_page.data_item()
     wd.get(get_redirect=True)
     if ('P597' in wd.claims):
       total_women+=1
       woman = True
     if ('P536' in wd.claims):
       total_men+=1
       man = True
     
     if (('P597' in wd.claims) and (found_woman<max_woman)) or (('P536' in wd.claims) and (found_man<max_man)): #skip if not WTA-id or ATP-id defined 
       male=female=False
       if ('P21' in wd.claims):
         sex=wd.claims.get('P21')[0].getTarget()
         if (sex.title()=='Q6581072'): 
           indicator='$$$'
           female=True
         if (sex.title()=='Q6581097'): 
           indicator='$$'
           male=True
       if ('P2121' in wd.claims):
         wd_pr=wd.claims.get('P2121')[0].getTarget()
         if (isinstance(wd_pr,pywikibot.WbQuantity)):
           wd_dd = wd_pr.amount
         else:
           wd_dd = -2
       if ('dewiki' in wd.sitelinks): 
         de_page=pywikibot.Page(de_site, wd.sitelinks['dewiki'])
         de_dollars_value, de_image=get_values('de',de_page)
       nl_dollars_value, nl_image =get_values(useSite,src_page)  
       if ('enwiki' in wd.sitelinks):
         en_page=pywikibot.Page(en_site, wd.sitelinks['enwiki'])
         en_dollars_value, en_image=get_values('en',en_page)
       if ('frwiki' in wd.sitelinks):
         fr_page=pywikibot.Page(fr_site, wd.sitelinks['frwiki'])
         fr_dollars_value, fr_image=get_values('fr',fr_page);
       if (src_page.text.lower().find('[[amerikaanse doll')>0):
         indicator += '!'
       if (not nl_image) and ((de_image) or (en_image) or (('P18') in wd.claims)):
         indicator += 'i'  #other sites might have a useful image
       
       ax=(wd_dd == -1.11)
       fbx=((not(nl_dollars_value==wd_dd)) and (not(nl_dollars_value>=0)) and(nl_dollars_value!=-1) and (woman))
       mbx=((not(nl_dollars_value==wd_dd)) and (not(nl_dollars_value>=0)) and(nl_dollars_value!=-1) and (man))
       dex=( (((de_dollars_value>wd_dd) and woman) or (de_dollars_value>wd_dd+max_man_dollar)) and (de_dollars_value>0)) 
       enx=( (((en_dollars_value>wd_dd) and woman) or (en_dollars_value>wd_dd+max_man_dollar)) and (en_dollars_value>0))
       frx=( (((fr_dollars_value>wd_dd) and woman) or (fr_dollars_value>wd_dd+max_man_dollar)) and (fr_dollars_value>0))
       if (  ax \
          or fbx or mbx \
          or dex \
          or enx \
          or frx
          ):
         diffde=wd_dd-de_dollars_value
         diffen=wd_dd-en_dollars_value
         difffr=wd_dd-fr_dollars_value
         
         thistxt = ('|-\n|%s||[[:d:%9s]]||{%8d}||%i%i%i%i%i\n' %(indicator,wd.title(),abs(smallest((diffde,diffen,difffr))),ax,fbx or mbx,dex,enx,frx))
         if (male) :
           mantxt += thistxt
           found_man+=1
         if (female) :
           femtxt += thistxt
           found_woman+=1
         #print('man: %d – women: %d' % (found_man, found_woman))
         print('%s'%thistxt)
       else:
         #print('\-\n|%s||[[:d:%9s]]||[%9d]||nl||[%9d]||==||{%8d}||/||de||[%9d]||{%8d}\n' %(indicator,wd.title(),wd_dd,nl_dollars_value,wd_dd-nl_dollars_value,de_dollars_value,de_dollars_value-wd_dd))
         pass

 wikitext = intro[useSite]+'\n\n'+table_start+femtxt+table_end+'\n\n'+table_start+mantxt+table_end+'\n\nMan..:%d\nWomen: %d\n' % (total_men,total_women)

 if (True):
   print('%s' % wikitext)
   pywikibot.Page(pywikibot.getSite(useSite), useTarget[useSite]).put(wikitext, comment='Update tennis statistics suggestions') #Save page  

 
all_from_cat(useSite,useCategory[useSite])
|-
|$$$||[[:d:Q17612134]]||{   13630}||00001

|-
|$$||[[:d: Q1187478]]||{   57920}||00011

|-
|$$$||[[:d:Q20708205]]||{    8125}||00010

|-
|$$$||[[:d:   Q45405]]||{    3625}||00010

|-
|$$$||[[:d: Q2216368]]||{   25950}||00001

|-
|$$||[[:d: Q7700510]]||{  129530}||00001

|-
|$$$||[[:d:  Q766964]]||{    5125}||00001

|-
|$$||[[:d:  Q336862]]||{   60776}||00001

|-
|$$$||[[:d:Q15635268]]||{  138795}||00010

|-
|$$$||[[:d: Q2446649]]||{   13285}||00001

|-
|$$||[[:d: Q6162896]]||{   99070}||00010

|-
|$$||[[:d: Q3720084]]||{   56745}||00011

|-
|$$||[[:d: Q2152388]]||{   90108}||00001

|-
|$$||[[:d:Q22958938]]||{  107815}||00011

|-
|$$$||[[:d:  Q272011]]||{    1390}||00001

|-
|$$||[[:d:  Q559692]]||{   53994}||00011

|-
|$$$||[[:d:  Q191766]]||{   20510}||00010

|-
|$$||[[:d:Q16219277]]||{   68505}||00001

|-
|$$$||[[:d:Q17381675]]||{  196175}||00001

|-
|$$||[[:d:  Q439942]]||{   69000}||00011

|-
|$$||[[:d:Q23668736]]||{   78505}||00011

|-
|$$||[[:d:  Q337681]]||{   94818}||00011

|-
|$$$||[[:d: Q6078092]]||{    1774}||00010

|-
|$$||[[:d:  Q440531]]||{   54078}||00011

|-
|$$$||[[:d:Q30068620]]||{    1496}||00001

|-
|$$$||[[:d:  Q197291]]||{  293617}||00011

|-
|$$||[[:d: Q1355458]]||{   95650}||00011

|-
|$$$||[[:d:   Q30767]]||{    3310}||00010

|-
|$$||[[:d:   Q77149]]||{  122045}||00011

|-
|$$$||[[:d:  Q639366]]||{   11620}||00011

|-
|$$||[[:d:   Q13538]]||{   52595}||00011

|-
|$$||[[:d:  Q176577]]||{  125422}||00011

|-
|$$i||[[:d: Q2093033]]||{   73805}||00011

|-
|$$||[[:d:Q13403768]]||{   79763}||00011

|-
|$$||[[:d:  Q274441]]||{   79764}||00001

|-
|$$||[[:d:  Q212833]]||{   54350}||00011

|-
|$$||[[:d:  Q192661]]||{   87559}||00011

|-
|$$||[[:d: Q5284058]]||{   50750}||00001

|-
|$$||[[:d:Q13360044]]||{  134283}||00011

|-
|$$$||[[:d:   Q34248]]||{   11620}||00010

|-
|$$||[[:d:  Q319572]]||{  151200}||00011

|-
|$$$||[[:d:Q18210941]]||{    6356}||00001

|-
|$$$||[[:d:  Q225612]]||{     173}||00101

|-
|$$||[[:d:  Q311222]]||{   92380}||00111

|-
|$$$||[[:d:Q17466583]]||{   53665}||00001

|-
|$$||[[:d:  Q245241]]||{ 3000000}||00001

|-
|$$||[[:d:  Q295483]]||{   66575}||00011

|-
|$$||[[:d: Q7035585]]||{   69681}||00011

|-
|$$||[[:d: Q1588571]]||{   76600}||00011

|-
|$$$||[[:d: Q5551564]]||{   13844}||00001

|-
|$$||[[:d:  Q557827]]||{   52732}||00001

|-
|$$$||[[:d:Q19802192]]||{   21887}||00010

|-
|$$||[[:d: Q6835083]]||{  102291}||00011

|-
|$$||[[:d:  Q211636]]||{   54241}||00001

|-
|$$||[[:d:  Q345626]]||{   70390}||00011

|-
|$$$||[[:d:  Q236866]]||{    3874}||00100

|-
|$$$||[[:d:Q20140204]]||{    7223}||00001

|-
|$$$||[[:d:  Q239751]]||{    7085}||00010

|-
|$$||[[:d:Q21622022]]||{   59475}||00111

|-
|$$||[[:d:    Q5812]]||{   75290}||00111

|-
|$$||[[:d:  Q551295]]||{   70510}||00011

|-
|$$$||[[:d:Q15830617]]||{    9204}||00010

|-
|$$$||[[:d:Q20968624]]||{    3400}||00001

|-
|$$||[[:d:  Q966542]]||{  219755}||00011

|-
|$$$||[[:d:  Q230260]]||{    6345}||00010

|-
|$$||[[:d:  Q191740]]||{   62238}||00011

|-
|$$$||[[:d:  Q539526]]||{    4698}||00010

|-
|$$||[[:d:  Q193665]]||{  118320}||00011

|-
|$$$||[[:d:  Q289365]]||{     540}||00010

|-
|$$$||[[:d:  Q230353]]||{    8421}||00010

|-
|$$||[[:d:  Q299956]]||{   90990}||00011

|-
|$$||[[:d:  Q778206]]||{   76650}||00011

|-
|$$||[[:d:Q17405398]]||{   50054}||00101

Deze pagina is automatisch aangemaakt. Wijzig het niet handmatig.

{| class="wikitable sortable"
|-
! Type !! Item !! wd$ !! de !! de$ !! diff !! en !! en$ !! diff !! fr !! fr$ !! diff !! binary 
|-|-
|$$$||[[:d:Q17612134]]||{   13630}||00001
|-
|$$$||[[:d:Q20708205]]||{    8125}||00010
|-
|$$$||[[:d:   Q45405]]||{    3625}||00010
|-
|$$$||[[:d: Q2216368]]||{   25950}||00001
|-
|$$$||[[:d:  Q766964]]||{    5125}||00001
|-
|$$$||[[:d:Q15635268]]||{  138795}||00010
|-
|$$$||[[:d: Q2446649]]||{   13285}||00001
|-
|$$$||[[:d:  Q272011]]||{    1390}||00001
|-
|$$$||[[:d:  Q191766]]||{   20510}||00010
|-
|$$$||[[:d:Q17381675]]||{  196175}||00001
|-
|$$$||[[:d: Q6078092]]||{    1774}||00010
|-
|$$$||[[:d:Q30068620]]||{    1496}||00001
|-
|$$$||[[:d:  Q197291]]||{  293617}||00011
|-
|$$$||[[:d:   Q30767]]||{    3310}||00010
|-
|$$$||[[:d:  Q639366]]||{   11620}||00011
|-
|$$$||[[:d:   Q34248]]||{   11620}||00010
|-
|$$$||[[:d:Q18210941]]||{    6356}||00001
|-
|$$$||[[:d:  Q225612]]||{     173}||00101
|-
|$$$||[[:d:Q17466583]]||{   53665}||00001
|-
|$$$||[[:d: Q5551564]]||{   13844}||00001
|-
|$$$||[[:d:Q19802192]]||{   21887}||00010
|-
|$$$||[[:d:  Q236866]]||{    3874}||00100
|-
|$$$||[[:d:Q20140204]]||{    7223}||00001
|-
|$$$||[[:d:  Q239751]]||{    7085}||00010
|-
|$$$||[[:d:Q15830617]]||{    9204}||00010
|-
|$$$||[[:d:Q20968624]]||{    3400}||00001
|-
|$$$||[[:d:  Q230260]]||{    6345}||00010
|-
|$$$||[[:d:  Q539526]]||{    4698}||00010
|-
|$$$||[[:d:  Q289365]]||{     540}||00010
|-
|$$$||[[:d:  Q230353]]||{    8421}||00010

|}


{| class="wikitable sortable"
|-
! Type !! Item !! wd$ !! de !! de$ !! diff !! en !! en$ !! diff !! fr !! fr$ !! diff !! binary 
|-|-
|$$||[[:d: Q1187478]]||{   57920}||00011
|-
|$$||[[:d: Q7700510]]||{  129530}||00001
|-
|$$||[[:d:  Q336862]]||{   60776}||00001
|-
|$$||[[:d: Q6162896]]||{   99070}||00010
|-
|$$||[[:d: Q3720084]]||{   56745}||00011
|-
|$$||[[:d: Q2152388]]||{   90108}||00001
|-
|$$||[[:d:Q22958938]]||{  107815}||00011
|-
|$$||[[:d:  Q559692]]||{   53994}||00011
|-
|$$||[[:d:Q16219277]]||{   68505}||00001
|-
|$$||[[:d:  Q439942]]||{   69000}||00011
|-
|$$||[[:d:Q23668736]]||{   78505}||00011
|-
|$$||[[:d:  Q337681]]||{   94818}||00011
|-
|$$||[[:d:  Q440531]]||{   54078}||00011
|-
|$$||[[:d: Q1355458]]||{   95650}||00011
|-
|$$||[[:d:   Q77149]]||{  122045}||00011
|-
|$$||[[:d:   Q13538]]||{   52595}||00011
|-
|$$||[[:d:  Q176577]]||{  125422}||00011
|-
|$$i||[[:d: Q2093033]]||{   73805}||00011
|-
|$$||[[:d:Q13403768]]||{   79763}||00011
|-
|$$||[[:d:  Q274441]]||{   79764}||00001
|-
|$$||[[:d:  Q212833]]||{   54350}||00011
|-
|$$||[[:d:  Q192661]]||{   87559}||00011
|-
|$$||[[:d: Q5284058]]||{   50750}||00001
|-
|$$||[[:d:Q13360044]]||{  134283}||00011
|-
|$$||[[:d:  Q319572]]||{  151200}||00011
|-
|$$||[[:d:  Q311222]]||{   92380}||00111
|-
|$$||[[:d:  Q245241]]||{ 3000000}||00001
|-
|$$||[[:d:  Q295483]]||{   66575}||00011
|-
|$$||[[:d: Q7035585]]||{   69681}||00011
|-
|$$||[[:d: Q1588571]]||{   76600}||00011
|-
|$$||[[:d:  Q557827]]||{   52732}||00001
|-
|$$||[[:d: Q6835083]]||{  102291}||00011
|-
|$$||[[:d:  Q211636]]||{   54241}||00001
|-
|$$||[[:d:  Q345626]]||{   70390}||00011
|-
|$$||[[:d:Q21622022]]||{   59475}||00111
|-
|$$||[[:d:    Q5812]]||{   75290}||00111
|-
|$$||[[:d:  Q551295]]||{   70510}||00011
|-
|$$||[[:d:  Q966542]]||{  219755}||00011
|-
|$$||[[:d:  Q191740]]||{   62238}||00011
|-
|$$||[[:d:  Q193665]]||{  118320}||00011
|-
|$$||[[:d:  Q299956]]||{   90990}||00011
|-
|$$||[[:d:  Q778206]]||{   76650}||00011
|-
|$$||[[:d:Q17405398]]||{   50054}||00101

|}


Man..:916
Women: 1145

WARNING: /srv/paws/lib/python3.6/site-packages/ipykernel_launcher.py:187: DeprecationWarning: pywikibot.getSite is deprecated for 3 years and 4 months; use pywikibot.Site instead.

WARNING: /srv/paws/lib/python3.6/site-packages/ipykernel_launcher.py:187: DeprecationWarning: comment argument of pywikibot.page.BasePage.put is deprecated; use summary instead.

Page [[nl:Gebruiker:Edoderoo/Tennis-stats]] saved
def smallest(x):
 smallval=x[0]
 for y in x:
    if y<smallval:
        smallval=y
 return smallval

print(abs(smallest((10,-12,30))))
12