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'

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,'P248')
  src_is=pywikibot.ItemPage(repo,'Q21705941') #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])
  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)
  wd.addClaim(claimval,summery='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 - 
===========================================
WARNING: WbQuantity now expects a 'site' parameter. This is needed to ensure correct handling of error bounds.
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-25d23196c29f> in <module>()
    195 print('start')
    196 for x in range (9001,18000):
--> 197   print_a_darter(str(x))

<ipython-input-19-25d23196c29f> in print_a_darter(id)
    176  if (int(prijzengeld)>0):
    177   if (int(prijzengeld)>int('0'+wd_prijzengeld)):
--> 178     set_prijzengeld(wd,int(prijzengeld))
    179 
    180  print(f'Speler       : {name}')

<ipython-input-19-25d23196c29f> in set_prijzengeld(wd, amount)
    154   target=pywikibot.WbQuantity(amount)
    155   claimval.setTarget(target)
--> 156   wd.addClaim(claimval,summery='earned GBP from DartsDatabase')
    157   print(10/0) #force an error
    158 

/srv/paws/pwb/pywikibot/page.py in wrapper(self, *args, **kwargs)
    128             pywikibot.async_request(handle, func, self, *args, **kwargs)
    129         else:
--> 130             handle(func, self, *args, **kwargs)
    131 
    132     manage_wrapping(wrapper, func)

/srv/paws/pwb/pywikibot/page.py in handle(func, self, *args, **kwargs)
    107         err = None
    108         try:
--> 109             func(self, *args, **kwargs)
    110         # TODO: other "expected" error types to catch?
    111         except pywikibot.Error as edit_err:

/srv/paws/pwb/pywikibot/page.py in addClaim(self, claim, bot, **kwargs)
   4265         @type callback: callable
   4266         """
-> 4267         self.repo.addClaim(self, claim, bot=bot, **kwargs)
   4268         claim.on_item = self
   4269         for snaks in claim.qualifiers.values():

/srv/paws/pwb/pywikibot/site.py in callee(self, *args, **kwargs)
   1341             else:
   1342                 raise Exception('Not implemented')
-> 1343             return fn(self, *args, **kwargs)
   1344 
   1345         if not __debug__:

TypeError: addClaim() got an unexpected keyword argument 'summery'
"""
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('nl','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)
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:
    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')    
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-18-40d55c650f1d> in <module>()
     24   wd.addClaim(newclaim,summary='set #dartsdatabaseID')
     25 
---> 26 print(10/0) #avoid running again
     27 #for x in dir(pg): print(x)
     28 cat=pywikibot.Category(site,'Category:Darts players by nationality')

ZeroDivisionError: division by zero
import pywikibot

for x in dir(pywikibot.WbQuantity):
    print(x)
__class__
__delattr__
__dict__
__dir__
__doc__
__eq__
__format__
__ge__
__getattribute__
__gt__
__hash__
__init__
__init_subclass__
__le__
__lt__
__module__
__ne__
__new__
__reduce__
__reduce_ex__
__repr__
__setattr__
__sizeof__
__str__
__subclasshook__
__weakref__
_fromdecimal
_items
_require_errors
_todecimal
fromWikibase
get_unit_item
toWikibase
unit