import pywikibot
from pywikibot import pagegenerators as pg
import requests

class SportsRef:
  def __init__(self):    
    self.nicknames=''
    self.fullname=''
    self.height=''
    self.weight=''
    self.gender=''
    self.born=''
  def print(self):  
    print(f'Nick  : {self.nicknames}')  
    print(f'Full  : {self.fullname}')
    print(f'Height: {self.height}')    
    print(f'Weight: {self.weight}')    
    print(f'Gender: {self.gender}')    
    print(f'Born  : {self.born}')
  def txtsearch(self,txt,startsearch,stopsearch,offset):
   start=txt.find(startsearch)
   if (start>-1):
    stop=txt[start+offset:].find(stopsearch)
    if stop>0: return(txt[start+offset:start+offset-1+stop])
    else: return('')
      

site = pywikibot.Site('wikidata','wikidata')           #Geef aan naar welke site je wilt schrijven
repo = site.data_repository()                     #voor ophalen wikidata-items adhv Qxxxx
baseURL='https://www.sports-reference.com/olympics/athletes/%s.html'
propSR='P1447'
query='select ?item where {?item wdt:P1447 ?srid}'

Pdob='P569'
Pnick='P1449'
Pname='P2561'
Pweight='P2067'
Pheight='P2048'

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



def OneSR(athlete,id):
  f = requests.get(baseURL%id) 
  athlete.nicknames=athlete.txtsearch(f.text,'<span class="bold_text">Nickname','<br>',44)
  athlete.fullname=athlete.txtsearch(f.text,'<span class="bold_text">Full name:','<br>',42)
  athlete.height=athlete.txtsearch(f.text,'<span class="bold_text">Height:</span>','<br>',38)
  athlete.weight=athlete.txtsearch(f.text,'<span class="bold_text">Weight:</span>','<br>',38)
  athlete.gender=athlete.txtsearch(f.text,'<span class="bold_text">Gender:</span>','<br>',38)
  athlete.born=athlete.txtsearch(f.text,'<span class="bold_text">Born:</span>','data-birth=',71)  
  """
  if (start>-1):
    stop=f.text[start+44:].find('<br>')
    if stop>0: =f.text[start+44:start+43+stop]  
  start=f.text.find('<span class="bold_text">Full name:')
  if (start>-1):
    stop=f.text[start+42:].find('<br>')
    if stop>0: athlete.fullname=f.text[start+42:start+41+stop]
  start=f.text.find('<span class="bold_text">Height:</span>')
  if (start>-1):
    stop=f.text[start+38:].find('<br>')
    if stop>0: athlete.height=f.text[start+38:start+37+stop]
  """
  start=f.text.find('<span class="bold_text">Weight:</span>')
  if (start>-1):
    stop=f.text[start+38:].find('<br>')
    if stop>0: athlete.weight=f.text[start+38:start+37+stop]
  start=f.text.find('<span class="bold_text">Gender:</span>')
  if (start>-1):
    stop=f.text[start+38:].find('<br>')
    if stop>0: athlete.gender=f.text[start+38:start+37+stop]
  start=f.text.find('<span class="bold_text">Born:</span>')
  if (start>-1):
    stop=f.text[start:].find('data-birth=')
    if stop>0: athlete.born=f.text[start+71:start+70+stop] 
    else: athlete.born=f'{start}-{stop}'    

  
    

    
print('Begonnen')
counter=0
"""
for wd in wd_sparql_query(query):
  if ('P1447' in wd.claims):
    id=wd.claims['P1447'][0].getTarget()
    if (id!=Null):
      OneSR(id)
"""
testset=['ma/yevgeniya-manyukova-1','uh/peter-uhrig-1','ga/rolando-garbey-1','so/joyce-sombroek-1','ev/eric-evans-1']
for onetst in testset:
  sporter=SportsRef()
  OneSR(sporter,onetst)
  sporter.print()
print('Klaar')
Begonnen
Nick  : None
Full  : Yevgeniya Aleksandrovna Manyukova
Height:  5-9 (176 cm)
Weight:  154 lbs (70 kg)
Gender:  Female
Born  : "1968-05-17"><a href="/olympics/friv/birthdays.cgi?month=5&
Nick  : None
Full  : Peter Uhrig
Height:  6-2.5 (190 cm)
Weight:  157 lbs (71 kg)
Gender:  Male
Born  : "1965-05-23"><a href="/olympics/friv/birthdays.cgi?month=5&
Nick  : None
Full  : Rolando Garbey Garbey
Height:  6-0 (183 cm)
Weight:  157 lbs (71 kg)
Gender:  Male
Born  : "1947-11-19"><a href="/olympics/friv/birthdays.cgi?month=11
Nick  : None
Full  : Joyce Sombroek
Height:  5-10 (179 cm)
Weight:  141 lbs (64 kg)
Gender:  Female
Born  : "1990-09-10"><a href="/olympics/friv/birthdays.cgi?month=9&
Nick  : The Hammer, The Mean One
Full  : Eric Mayfield Evans
Height:  5-10 (178 cm)
Weight:  161 lbs (73 kg)
Gender:  Male
Born  : "1950-02-27"><a href="/olympics/friv/birthdays.cgi?month=2&
Klaar
from sys import *
import os
import time

maxline=1000
for line in range(1,maxline): #Read every line from the file
        done=str(int((float(line)/maxline)*100))
        stdout.write(" File read percentage: %s%%      %s"%(done,"\r"))
        #stdout.flush();
        time.sleep(0.1)
 File read percentage: 23%      
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-3-820a90b963e2> in <module>
      8         stdout.write(" File read percentage: %s%%      %s"%(done,"\r"))
      9         #stdout.flush();
---> 10         time.sleep(0.1)

KeyboardInterrupt: