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
Q21032058
Sleeping for 7.9 seconds, 2019-04-06 15:55:50
Sleeping for 9.5 seconds, 2019-04-06 15:55:59
Q59134871
Sleeping for 8.9 seconds, 2019-04-06 15:56:09
Sleeping for 8.3 seconds, 2019-04-06 15:56:20
Q26248378
Sleeping for 9.4 seconds, 2019-04-06 15:56:29
Sleeping for 9.4 seconds, 2019-04-06 15:56:39
Q23887620
Sleeping for 9.4 seconds, 2019-04-06 15:56:49
Sleeping for 9.5 seconds, 2019-04-06 15:56:59
Q433769
Q52266330
Sleeping for 9.2 seconds, 2019-04-06 15:57:09
Sleeping for 9.4 seconds, 2019-04-06 15:57:19
Q7155101
Sleeping for 9.3 seconds, 2019-04-06 15:57:29
Sleeping for 9.5 seconds, 2019-04-06 15:57:39
Q2566338
Sleeping for 9.3 seconds, 2019-04-06 15:57:49
Sleeping for 9.4 seconds, 2019-04-06 15:57:59
Q5651484
Sleeping for 9.2 seconds, 2019-04-06 15:58:09
Sleeping for 9.4 seconds, 2019-04-06 15:58:19
Q2254978
Sleeping for 9.4 seconds, 2019-04-06 15:58:29
Sleeping for 9.6 seconds, 2019-04-06 15:58:39
Q102474
Sleeping for 9.3 seconds, 2019-04-06 15:58:49
Sleeping for 9.4 seconds, 2019-04-06 15:58:59
Q6660365
Sleeping for 9.3 seconds, 2019-04-06 15:59:09
Sleeping for 9.5 seconds, 2019-04-06 15:59:19
Q24333000
Sleeping for 9.3 seconds, 2019-04-06 15:59:29
Sleeping for 9.3 seconds, 2019-04-06 15:59:39
Q1634516
Sleeping for 9.2 seconds, 2019-04-06 15:59:49
Sleeping for 9.5 seconds, 2019-04-06 15:59:59
Q23059692
Sleeping for 9.3 seconds, 2019-04-06 16:00:09
Sleeping for 9.5 seconds, 2019-04-06 16:00:19
Q16956914
Q6373164
Sleeping for 9.1 seconds, 2019-04-06 16:00:29
Sleeping for 9.3 seconds, 2019-04-06 16:00:39
Q7911193
Sleeping for 9.3 seconds, 2019-04-06 16:00:49
Sleeping for 9.3 seconds, 2019-04-06 16:00:59
Q5291000
Sleeping for 9.3 seconds, 2019-04-06 16:01:09
Sleeping for 9.5 seconds, 2019-04-06 16:01:19
Q22086466
Sleeping for 9.3 seconds, 2019-04-06 16:01:29
Sleeping for 9.5 seconds, 2019-04-06 16:01:39
Q7563027
Sleeping for 9.3 seconds, 2019-04-06 16:01:49
Sleeping for 9.4 seconds, 2019-04-06 16:01:59
Q2176192
Sleeping for 9.3 seconds, 2019-04-06 16:02:09
Sleeping for 9.5 seconds, 2019-04-06 16:02:19
Q27858263
Sleeping for 9.2 seconds, 2019-04-06 16:02:29
Sleeping for 9.5 seconds, 2019-04-06 16:02:39
Q27450137
Sleeping for 9.3 seconds, 2019-04-06 16:02:49
Sleeping for 9.4 seconds, 2019-04-06 16:02:59
Q1574147
Sleeping for 9.3 seconds, 2019-04-06 16:03:09
Sleeping for 9.2 seconds, 2019-04-06 16:03:19
Q22957434
Sleeping for 9.3 seconds, 2019-04-06 16:03:29
Sleeping for 9.4 seconds, 2019-04-06 16:03:39
Q6385927
Sleeping for 9.3 seconds, 2019-04-06 16:03:49
Sleeping for 9.4 seconds, 2019-04-06 16:03:59
Q30082774
Q433284
Q59528105
Sleeping for 9.0 seconds, 2019-04-06 16:04:10
Sleeping for 9.5 seconds, 2019-04-06 16:04:19
Q20968655
Sleeping for 9.4 seconds, 2019-04-06 16:04:29
Sleeping for 9.4 seconds, 2019-04-06 16:04:39
Q18335831
Sleeping for 9.3 seconds, 2019-04-06 16:04:49
Sleeping for 9.4 seconds, 2019-04-06 16:04:59
Q23888024
Sleeping for 9.3 seconds, 2019-04-06 16:05:09
Sleeping for 9.4 seconds, 2019-04-06 16:05:19
Q18679121
Sleeping for 9.4 seconds, 2019-04-06 16:05:29
Sleeping for 9.4 seconds, 2019-04-06 16:05:39
Q19957805
Sleeping for 9.3 seconds, 2019-04-06 16:05:49
Sleeping for 9.4 seconds, 2019-04-06 16:05:59
Q12276119
Sleeping for 9.3 seconds, 2019-04-06 16:06:09
Sleeping for 9.5 seconds, 2019-04-06 16:06:19
Q26838133
Sleeping for 9.3 seconds, 2019-04-06 16:06:30
Sleeping for 9.5 seconds, 2019-04-06 16:06:39
Q21970595
Sleeping for 9.3 seconds, 2019-04-06 16:06:49
Sleeping for 9.3 seconds, 2019-04-06 16:06:59
Q61314881
Sleeping for 9.3 seconds, 2019-04-06 16:07:09
Sleeping for 9.4 seconds, 2019-04-06 16:07:19
Q26132241
Sleeping for 9.4 seconds, 2019-04-06 16:07:29
Sleeping for 9.5 seconds, 2019-04-06 16:07:39
Q29642146
Sleeping for 9.3 seconds, 2019-04-06 16:07:50
Sleeping for 8.5 seconds, 2019-04-06 16:08:00
Q26272208
Sleeping for 9.4 seconds, 2019-04-06 16:08:09
Sleeping for 9.5 seconds, 2019-04-06 16:08:19
Q6170835
Q39263365
Q28018647
Sleeping for 8.8 seconds, 2019-04-06 16:08:30
Sleeping for 9.5 seconds, 2019-04-06 16:08:39
Q27064009
Sleeping for 9.3 seconds, 2019-04-06 16:08:50
Sleeping for 8.4 seconds, 2019-04-06 16:09:00
Q27832726
Sleeping for 9.3 seconds, 2019-04-06 16:09:10
Sleeping for 9.3 seconds, 2019-04-06 16:09:20
Q4767623
Sleeping for 9.3 seconds, 2019-04-06 16:09:30
Sleeping for 9.4 seconds, 2019-04-06 16:09:40
Q26721334
Sleeping for 9.3 seconds, 2019-04-06 16:09:50
Sleeping for 9.4 seconds, 2019-04-06 16:10:00
Q25823866
Sleeping for 9.3 seconds, 2019-04-06 16:10:10
Sleeping for 9.4 seconds, 2019-04-06 16:10:20
Q18342465
Sleeping for 9.3 seconds, 2019-04-06 16:10:30
Sleeping for 9.5 seconds, 2019-04-06 16:10:40
Q1866392
Sleeping for 9.3 seconds, 2019-04-06 16:10:50
Sleeping for 9.5 seconds, 2019-04-06 16:11:00
Q18339900
Sleeping for 9.3 seconds, 2019-04-06 16:11:10
Sleeping for 9.5 seconds, 2019-04-06 16:11:20
Q54370269
Q27048627
Processed 57 lines.
klaar