import pywikibot
import requests
from pywikibot import pagegenerators as pg
from datetime import datetime, date, time

P_nickname='P1449'
P_twitterhandle='P2002'
P_prijzengeld='P2121'
P_hometown='P551'
P_country='P27'
P_dob='P569'
QdartsDB='Q21705941'
P_statedin='P248'
P_retrieved='P813'

query='select ?item ?nickname ?ddid where {?item wdt:P31 wd:Q5 . ?item wdt:P106 wd:Q18574233 . OPTIONAL {?item wdt:P1449 ?nickname } OPTIONAL {?item wdt:P3621 ?ddid}} ORDER BY ?ddid'

site=pywikibot.Site('wikidata','wikidata')
repo=site.data_repository()
languages=['nl','en','fr','de','es','it','pt'] #languages to find label-names from
lng='nl' #language to add aliases to

def wd_sparql_query(spq):
   generator=pg.WikidataSPARQLPageGenerator(spq,site)
   for wd in generator:
     try:
       wd.get(get_redirect=True)
       yield wd
     except:
       pass

def set_nickname(wditem,nickname):
 if not ((P_nickname in wditem.claims) or (nickname=='')):    
  newclaim =pywikibot.Claim(repo,P_nickname)
  newtarget=pywikibot.WbMonolingualText(nickname,language='en')
  newclaim.setTarget(newtarget)

  statedin=pywikibot.Claim(repo,P_statedin)
  src_is=pywikibot.ItemPage(repo,QdartsDB) #darts database
  statedin.setTarget(src_is)
  retrieved=pywikibot.Claim(repo, P_retrieved) #retrieved date
  date=pywikibot.WbTime(year=datetime.now().year, month=datetime.now().month, day=datetime.now().day)
  retrieved.setTarget(date)
  newclaim.addSources([statedin,retrieved])
  wditem.addClaim(newclaim,summary='set nickname ')
  
def wd_get_darts_player(id):
  query=f'select ?item where {{?item wdt:P3621 \'{id}\'}}'
  for wd in wd_sparql_query(query):
    return wd
  return(None)
        
def analyse_dartsplayer(id):
  name=dob=country=nickname=twitterhandle=prijzengeld=hometown=music=''
  baseURL='http://www.dartsdatabase.co.uk/PlayerDetails.aspx?playerKey='
  f = requests.get(baseURL+id) 

  start=f.text.find('<font size=5>')
  end=f.text[start:].find('</font>')
  name=f.text[start+13:start+end]                  

  if (name[0:11]=='HTML PUBLIC'):
    pass  #player not found
    name=''
  else:
    start=f.text.find('https://twitter.com/')+20  #find twitterhandle
    if (start>20):
      end=start
      while f.text[end:end+1] not in ('<','\'','/'):
          end+=1;
      twitterhandle=f.text[start:end]

    start=f.text.find('<b>Nickname : </b>')   #find nickname
    if (start>0):
      bold=f.text[start+18:].find('<b>')    
      if (bold>0):
        boldend=f.text[start+bold:].find('</b>')
        if (boldend>0):
           nickname=f.text[start+bold+21:start+bold+boldend]   

    start=f.text.find('<b>Career Earnings : </b>')
    if (start>0):
      bold=f.text[start+18:].find('<b>')
      if (bold>0):
        boldend=f.text[start+bold:].find('</b>')
        if (boldend>0):
          prijzengeld=f.text[start+bold+22:start+bold+boldend].replace(',','')    
  
    start=f.text.find('<b>Home Town : </b>')
    if (start>0):
      bold=f.text[start+18:].find('<b>')
      if (bold>0):
        boldend=f.text[start+bold:].find('</b>')
        if (boldend>0):
          hometown=f.text[start+bold+21:start+bold+boldend]
  
    start=f.text.find('<b>Country : </b>')
    if (start>0):
      bold=f.text[start+18:].find('<b>')
      if (bold>0):
        boldend=f.text[start+bold:].find('</b>')
        if (boldend>0):
          country=f.text[start+bold+21:start+bold+boldend]
  
    start=f.text.find('<b>Date Of Birth : </b>')
    if (start>0):
      bold=f.text[start+18:].find('<b>')
      if (bold>0):
        boldend=f.text[start+bold:].find('</b>')
        if (boldend>0):
          dob=f.text[start+bold+21:start+bold+boldend]
  
    start=f.text.find('<b>Entrance Music : </b>')
    if (start>0):
      bold=f.text[start+18:].find('<b>')+18
      if (bold>18):
        end=f.text[start+bold:].find('target="_top">')
        if (end>0):
          musicend=f.text[start+bold+end:].find('</a>')    
          music=f.text[start+bold+end+14:start+bold+end+musicend]
  
  return (name,dob,country,nickname,twitterhandle,prijzengeld,hometown,music)

def get_label(wd):
  for lng in languages:
    if (lng in wd.labels):
      return(wd.labels[lng])
  return('')

def wd_get_value(wd,claim):
 if wd!=None:   
  if claim in wd.claims:
    claimval = (wd.claims.get(claim)[0].getTarget())
    if (type(claimval)==pywikibot.WbMonolingualText):
      return claimval.text  
    if (type(claimval)==pywikibot.page.ItemPage):
      claimval.get() 
      return get_label(claimval) 
    if (type(claimval)==pywikibot.WbQuantity):
      return claimval   
    return claimval
 return('')

def set_alias(wditem,nickname):
  if (lng in wditem.aliases):
    alias=wditem.aliases 
  else:
    alias={lng:[]}
  if not(nickname in alias[lng]):  
    alias[lng].append(nickname)
    wditem.editAliases(aliases=alias)
    
def set_prijzengeld(wd,amount):
  if (P_prijzengeld in wd.claims):
    claimval=wd.claims(P_prijzengeld)[0].getTarget()
  else: 
    claimval=pywikibot.Claim(repo,P_prijzengeld)
  target=pywikibot.WbQuantity(amount,site=site)
  claimval.setTarget(target)

  statedin=pywikibot.Claim(repo,P_statedin)
  src_is=pywikibot.ItemPage(repo,QdartsDB) #darts database
  statedin.setTarget(src_is)
  retrieved=pywikibot.Claim(repo, P_retrieved) #retrieved date
  date=pywikibot.WbTime(year=datetime.now().year, month=datetime.now().month, day=datetime.now().day)
  retrieved.setTarget(date)
  claimval.addSources([statedin,retrieved])

  wd.addClaim(claimval,summary='earned GBP from DartsDatabase')  
  print(10/0) #force an error

def print_a_darter(id):
 wd=wd_get_darts_player(id)
 wd_twitter=wd_get_value(wd,P_twitterhandle) 
 wd_nickname=wd_get_value(wd,P_nickname)
 wd_hometown=wd_get_value(wd,P_hometown)
 wd_prijzengeld=wd_get_value(wd,P_prijzengeld)
 wd_country=wd_get_value(wd,P_country)

 name,dob,country,nickname,twitterhandle,prijzengeld,hometown,music = analyse_dartsplayer(id)
 if (wd != None):
   if  (nickname!=''):   
     set_alias(wd,nickname)
     if (P_nickname not in wd.claims):
       set_nickname(wd,nickname)
   
 x=int(prijzengeld)
 y=int('0'+wd_prijzengeld)   
 if (int(prijzengeld)>0):
  if (int(prijzengeld)>int('0'+wd_prijzengeld)):
    set_prijzengeld(wd,int(prijzengeld))
    
 print(f'Speler       : {name}')
 #print(f'Twitter      : {twitterhandle} - {wd_twitter}')
 #print(f'Nickname     : {nickname} - {wd_nickname}')
 print(f'Prijzengeld  : {prijzengeld} - {wd_prijzengeld}')
 #print(f'Woonplaats   : {hometown} - {wd_hometown}')
 #print(f'Land         : {country} - {wd_country}')
 #print(f'Geboren op   : {dob}')      
 #print(f'Opkomstmuziek: {music}')
 print('===========================================')   
    
    
  
repo=pywikibot.Site('wikidata','wikidata').data_repository();
wditem=pywikibot.ItemPage(repo,'Q1677773') #co stompe
wditem.get()
print('start')
for x in range (9001,18000):
  print_a_darter(str(x)) 
start
Speler       : Lito Moises
Prijzengeld  : 0 - 
===========================================
Speler       : Rhen Fuentes
Prijzengeld  : 0 - 
===========================================
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-de60e6f17bc6> in <module>
    208 print('start')
    209 for x in range (9001,18000):
--> 210   print_a_darter(str(x))

<ipython-input-9-de60e6f17bc6> in print_a_darter(id)
    186 
    187  x=int(prijzengeld)
--> 188  y=int('0'+wd_prijzengeld)
    189  if (int(prijzengeld)>0):
    190   if (int(prijzengeld)>int('0'+wd_prijzengeld)):

TypeError: must be str, not WbQuantity
"""
find darts players on en-wiki, and get their Darts Database-ID
if it's missing on Wikidata, just add it as P3621
"""
import pywikibot
from pywikibot import pagegenerators as pg

site=pywikibot.Site('en','wikipedia')
repo=site.data_repository()
counter=0
stopper=[']',' ','\n','&','<','|']

def add_ddid(wd,ddid):
  newclaim =pywikibot.Claim(repo,'P3621')
  newtarget=ddid
  newclaim.setTarget(newtarget)
  statedin=pywikibot.Claim(repo,'P143')
  src_is=pywikibot.ItemPage(repo,'Q328') #darts database
  statedin.setTarget(src_is)
  retrieved=pywikibot.Claim(repo, u'P813') #retrieved date
  date=pywikibot.WbTime(year=datetime.now().year, month=datetime.now().month, day=datetime.now().day)
  retrieved.setTarget(date)
  newclaim.addSources([statedin,retrieved])
  wd.addClaim(newclaim,summary='set #dartsdatabaseID')
  
#print(10/0) #avoid running again
#for x in dir(pg): print(x)
print('Begonnen')
cat=pywikibot.Category(site,'Category:Darts players by nationality')
#cat=pywikibot.Category(site,'Categorie:Darter naar nationaliteit')
for page in pg.CategorizedPageGenerator(cat,111):
 page.text=page.text.lower() #easy searches   
 if ('wikibase_item' in page.properties()):
  counter+=1      
  wd=page.data_item()
  wd.get(get_redirect=True)
  if ('P3621' in wd.claims):
    wdDDid=wd.claims.get('P3621')[0].getTarget()
  else:
    print(page.title())
    wdDDid=''
  pDDid=''
  start=page.text.find('http://www.dartsdatabase.co.uk/')  
  if (start>0):
    keystart=page.text[start:].find('playerkey=')
    if (keystart>0):
      i=0  
      nextchar=''
      while not(nextchar in stopper):  
        pDDid+=nextchar  
        nextchar=page.text[start+keystart+10+i:start+keystart+11+i]
        i+=1
  if (wdDDid=='') and (pDDid!=''):
    add_ddid(wd,pDDid)
  #if (wdDDid!=pDDid):
  #  print(f'{wd.title()}|{wdDDid}|{pDDid}')    
  #if counter>88: break
print('That is all')    
Begonnen
Danny Baggish
Willard Bruguier
Sean Downs
Dave Kelly (darts player)
Dan Lauby Jr
Dan Lauby
Chuck Pankow
Chuck Puleo
Glenn Remick
DJ Sayre
Jerry Umberger
Gerald Verrier
Jim Watkins (darts player)
Chris White (darts player)
Mike Bonser
Tic Bridge
Mal Cuming
Damon Heta
Robbie King (darts player)
Steve MacArthur
Raymond O'Donnell
Tim Pusey
Benjamin Fasching
Christian Gödl
Alex Steinbauer
Patrick Tringler
Brian Raman
Mario Vandenbogaerde
Kenny Neyens
Bruno Rangel (darts player)
Phil Taylor career statistics
WARNING: API error badtoken: Invalid CSRF token.
Sleeping for 9.8 seconds, 2019-10-13 19:04:13
Dean Allsop
Martin Atkins (darts player, born 1975)
Leighton Bennett
Sleeping for 3.8 seconds, 2019-10-13 19:04:29
Bradley Brooks
Ritchie Buckle
Kevin Burrows
Shaun Carroll
Matt Chapman (darts player)
Jarred Cole
Scott Darbyshire
Gary Eastwood
John Ferrell (darts player)
Les Fitton
Maureen Flowers
Kevin Garcia (darts player)
Beau Greaves
Reg Harding
Al Hedman
Mark Holden (darts player)
Dave Honey
Adam Huckvale
Nigel Justice
George Killington
Mark Landers (darts player)
Jason Lowe (darts player)
Martin Lukeman
Mick Manning (darts player)
Dale Newton (darts player)
Steve Raw
Harry Robinson (darts player)
Simon Stainton
Adam Smith-Neale
Steve Smith (darts player)
Gary Spedding
Simon Stevenson
Graeme Stoddart
Scott Taylor (darts player)
Mark Thomson (darts player)
Ross Twell
Gary Welding
Carl Wilkinson (darts player)
Paul Williams (darts player)
Mark Wilson (darts player)
Kevin Burness
Gavin Carlin
Henry O'Neill (darts player)
Nathan Rafferty
Paul Watton
Mark Layton
Alan Reynolds (darts player)
William Borland (darts player)
Jim Brown (darts player)
John Goldie (darts player)
Alex MacKay (darts player)
Martijn Kleermaker
Jack Price (darts)
Shawn Brenneman
Scott Cummings (darts player)
Terry Hayhurst
Dawson Murschell
John Norman Jnr
Rory Orvis
Yuanjun Liu
Tang Jun (darts player)
Zong Xiao Chen
Alan Ljubić
Adam Gawlas
Pavel Jirkal
Ondřej Plšek
Václav Schieferdecker
Martin Staszko
Henning von Essen
Gerda Weltz
Dennie Olde Kalter
Mike van Duivenbode
Erik Hol
Maik Kuivenhoven
Vincent van der Meer
Alexander Merkx
Wessel Nijman
Danny van Trijp
Rafael van der Vaart
Patrick van den Boogaard
Niels Zonneveld
Edward Santos
Teuvo Haverinen
Thibault Tricole
René Berndt
Manfred Bilderl
Christian Bunse
Simeon Heinz
Florian Hempel
Fabian Herz
Mike Holz
Michael Hurtz
Christian Jentschke
Thomas Junghans
Kevin Knopf
Karsten Koch
Thomas Köhnlein
Nico Kurz
Maik Langendorf
Ole Luckow
Tobias Müller (darts player)
Ricardo Pietreczko
Mike Poge
Marko Puls
Steffen Siepmann
Stefan Stoyke
Lukas Wenig
Ioannis Selachoglou
Tamás Alexits
Pál Székely
János Végső
Keane Barry
Jason Cullen
Killian Heffernan
Daniele Petri
Stefano Tomassetti
Yasuhiko Matsunaga
Toru Sano (darts player)
Mark Cleaver
Darren Herewini
John Hurring
Tahuna Irwin
Haupai Puha
Ben Robb
Craig Ross (darts player)
Rob Szabo
Cor Dekker
Tytus Kanik
Sebastian Steyer
Aleksei Kadochnikov
Benjamin Pratnemer
Julio Barbero
Jose Justicia
Jesús Noguera
Andreas Harrysson
Stefan Bellmont
Andy Bless
Alex Fehlmann
That is all
print('No!')