import pywikibot
import datetime
import csv
from datetime import datetime

Prang='P1352'
Psport='P641'
Ptijdstip='P585'
Qenkel='Q18123880'
Qdubbel='Q18123885'

site=pywikibot.Site('wikidata','wikidata')
repo=site.data_repository()
etarget=pywikibot.ItemPage(repo,Qenkel)
dtarget=pywikibot.ItemPage(repo,Qdubbel)

def addRang(player,rang,addtarget,datum,summary):
 if (int(rang)>0):   
  rangclaim=pywikibot.Claim(repo,Prang)
  rangtarget=pywikibot.WbQuantity(rang,site=site)
  rangclaim.setTarget(rangtarget)

  sportclaim=pywikibot.Claim(repo,Psport)
  sportclaim.setTarget(addtarget) 
  datumtarget=pywikibot.WbTime(year=datum.year,month=datum.month,day=datum.day,site=site,precision='day')
  datumclaim=pywikibot.Claim(repo,Ptijdstip)
  datumclaim.setTarget(datumtarget)  
    
  rangclaim.addQualifier(sportclaim)  
  rangclaim.addQualifier(datumclaim)
  player.addClaim(rangclaim,summary=summary)  
    

def process_one(playerID, EnkelRang,EnkelDatum,DubbelRang,DubbelDatum):
  player=pywikibot.ItemPage(repo,playerID)
  player.get(get_redirect=True)
  print(player.title())  
    
  if (not(Prang in player.claims)):
    addRang(player,EnkelRang,etarget,EnkelDatum,'set single ranking, too much fuzz to do that manually')  
    addRang(player,DubbelRang,dtarget,DubbelDatum,'set double ranking, too much fuzz to do that manually')  

def read_csv():
 with open('wtarang.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        if line_count == 0:
            print(f'Column names are {", ".join(row)}')
            line_count += 1
        else:
          if (row[3]!=''):  
            eDate = datetime.strptime(row[3], '%d-%b-%Y')
          if (row[5]!=''):  
            dDate = datetime.strptime(row[5], '%d-%b-%Y')   
          process_one(row[0],row[2],eDate,row[4],dDate)
          line_count += 1
    print(f'Processed {line_count} lines.')    
    
#process_one('Q180446',2,datetime.date(2014,2,17),54,datetime.date(2006,8,28))
read_csv()
print('klaar')
Column names are Wikidata, WTA-id, enkel-pos, enkel-datum, dubbel-pos, dubbel datum, naam, 
Q68151
Q608942
Q16947785
Q6558491
Q59357549
Q4770442
Q47307330
Q5350350
Q455437
Q54336528
Q47939051
Q6729860
Q4490004
Q36397388
Q9184505
Q1972762
Q25820408
Q18062154
Q4776248
Q4532542
Q6749906
Q12324298
Q7934741
Q2924407
Q30538226
Q2506769
Q22953456
Q2224210
Q11715830
Q3847280
Q59550730
Q25062000
Q435123
Q3574170
Q18759205
Q988904
Q20895277
Q24551165
Q3188079
Q59386410
Q4837840
Q12638151
Q5370365
Q4875657
Q20195493
Q17418392
Q29362949
Q30160533
Q4768903
Q18609292
Q56087026
Q24258869
Q15834723
Q25820522
Q33301787
Q54086802
Q18342188
Q16956914
Q433284
Q54370269
Q24955794
Q59517387
Q38689737
Q50332038
Q26262732
Q48547799
Q27536148
Q12894791
Q47088527
Q26263099
Q3194005
Q17346598
Q26722249
Q24394837
Q27809674
Q26721313
Q5276314
Q20895315
Q24262874
Q27048627
Processed 81 lines.
klaar