import pywikibot
import urllib

tablestyle='<table style='
endtable='</table>'
tds='<td>' 
tde='</td>'
refs='<a'    
refe='</a>'    
    
class Classification:
  def __init__(self):
    self.rank=None
    self.name=''
    self.time=''

class Event:
  def __init__(self):
    self.location=''
    self.rinkt=''
    self.eventdate=''
    self.list=[]
    
def makefilename(geslacht='v',eventtype='nkafs',event='1000',jaar='2019'):
 return(f'NKafstanden/stats-{geslacht}-{eventtype}-{event}-{jaar}')    
    
def makeURL(geslacht='v',eventtype='nkafs',event='1000',jaar='2019'):    
 srcURL=f'http://www.schaatsstatistieken.nl/index.php?file=kampioenschappen&g={geslacht}&type={eventtype}&event={event}&jaar={jaar}'
 return(srcURL)
    
def readfromweb(geslacht='v',eventtype='nkafs',event='1000',jaar='2019'):
 url=makeURL(geslacht=geslacht,eventtype=eventtype,event=event,jaar=jaar)
 ssnl=urllib.request.urlopen(url).read()
 writefile(makefilename(geslacht=geslacht,eventtype=eventtype,event=event,jaar=jaar),ssnl)


def writefile(filename,text2write):
 outfile=open(filename,'a')
 outfile.write(str(text2write))
 outfile.close()

def readfile(filename):
  readfile=open(filename)
  filetxt=readfile.read()
  return filetxt  

def readRange():
  for year in range (1987,2008):
    #print(str(year))
    readfromweb(geslacht='v',eventtype='nkafs',event='1500',jaar=year)

def findtablefield(txt):
  tablefield=''
  start=txt.find('<td')  
  closetag=txt[start+4:].find('>')
  dataend=txt[start+4+closetag:].find('</')
  tablefield=txt[start+4+closetag:start+4+closetag+dataend]  
  return(tablefield,txt[start+4+closetag+dataend:])
    
def parse(txt):
  start=txt.find(tablestyle)  
  startdata=txt[start+len(tablestyle):].find(tablestyle)
  enddata=txt[start+len(tablestyle)+startdata:].find(endtable)  
  header=txt[start:start+startdata]
  table=txt[start+len(tablestyle)+startdata:start+len(tablestyle)+startdata+enddata]  
  print(f'{start}-{startdata}-{enddata}')
  print('================================================')  
  print(header)
  print('================================================')  
  print(table)
  plaats,header=findtablefield(header)
  plaats,header=findtablefield(header)
  ijsbaan,header=findtablefield(header)
  datum,header=findtablefield(header)
  datum,header=findtablefield(header)  
  print('================================================')  
  print(f'{plaats}+{ijsbaan}+{datum}')

print('Begonnen')
mytxt=readfile(makefilename())
parse(mytxt)
print('Klaar')
Begonnen
8834-318-3579
================================================
<table style=\'width:400px\'>\n<tr><td class=\'head\' width=\'60px\'>plaats</td><td>Heerenveen (NED)</td></tr>\n<tr><td class=\'head\'>ijsbaan</td><td><a class=\'link\' href=\'index.php?file=ijsbanen&track=heerenveen-thialf\'>Thialf</a></td></tr>\n<tr><td class=\'head\'>datum</td><td>30 december 2018</td></tr>\n</tab
================================================
<table style=\'width:400px\'>\n<tr class=\'head\'><td>#</td><td colspan=\'2\'>naam</td><td>tijd</td></tr>\n<tr><td>1</td><td><a class=\'link\' href=\'index.php?file=schaatser&code=1986040101\'>Ireen W\xc3\xbcst</a></td><td>NED</td><td>1.14,86</td></tr>\n<tr><td>2</td><td><a class=\'link\' href=\'index.php?file=schaatser&code=1995040601\'>Antoinette de Jong</a></td><td>NED</td><td>1.15,27</td></tr>\n<tr><td>3</td><td><a class=\'link\' href=\'index.php?file=schaatser&code=1998123001\'>Jutta Leerdam</a></td><td>NED</td><td>1.15,31</td></tr>\n<tr><td>4</td><td><a class=\'link\' href=\'index.php?file=schaatser&code=1996041901\'>Sanneke de Neeling</a></td><td>NED</td><td>1.15,42</td></tr>\n<tr><td>5</td><td><a class=\'link\' href=\'index.php?file=schaatser&code=1993030501\'>Letitia de Jong</a></td><td>NED</td><td>1.15,56</td></tr>\n<tr><td>6</td><td><a class=\'link\' href=\'index.php?file=schaatser&code=1991120901\'>Lotte van Beek</a></td><td>NED</td><td>1.15,62</td></tr>\n<tr><td>7</td><td><a class=\'link\' href=\'index.php?file=schaatser&code=1991041801\'>Janine Smit</a></td><td>NED</td><td>1.16,39</td></tr>\n<tr><td>8</td><td><a class=\'link\' href=\'index.php?file=schaatser&code=2000100503\'>Femke Kok</a></td><td>NED</td><td>1.16,46</td></tr>\n<tr><td>9</td><td><a class=\'link\' href=\'index.php?file=schaatser&code=1998092101\'>Elisa Dul</a></td><td>NED</td><td>1.16,59</td></tr>\n<tr><td>10</td><td><a class=\'link\' href=\'index.php?file=schaatser&code=1999042801\'>Joy Beune</a></td><td>NED</td><td>1.16,63</td></tr>\n<tr><td>11</td><td><a class=\'link\' href=\'index.php?file=schaatser&code=1992090703\'>Sanne van der Schaar</a></td><td>NED</td><td>1.16,95</td></tr>\n<tr><td>12</td><td><a class=\'link\' href=\'index.php?file=schaatser&code=1990071101\'>Roxanne van Hemert</a></td><td>NED</td><td>1.17,10</td></tr>\n<tr><td>13</td><td><a class=\'link\' href=\'index.php?file=schaatser&code=1997092501\'>Suzanne Schulting</a></td><td>NED</td><td>1.17,15</td></tr>\n<tr><td>14</td><td><a class=\'link\' href=\'index.php?file=schaatser&code=1998030401\'>Isabelle van Elst</a></td><td>NED</td><td>1.17,31</td></tr>\n<tr><td>15</td><td><a class=\'link\' href=\'index.php?file=schaatser&code=1998080303\'>Helga Drost</a></td><td>NED</td><td>1.17,32</td></tr>\n<tr><td>16</td><td><a class=\'link\' href=\'index.php?file=schaatser&code=1999122001\'>Femke Beuling</a></td><td>NED</td><td>1.17,34</td></tr>\n<tr><td>17</td><td><a class=\'link\' href=\'index.php?file=schaatser&code=1995070402\'>Tessa Boogaard</a></td><td>NED</td><td>1.17,71</td></tr>\n<tr><td>18</td><td><a class=\'link\' href=\'index.php?file=schaatser&code=2001011803\'>Robin Groot</a></td><td>NED</td><td>1.17,97</td></tr>\n<tr><td>19</td><td><a class=\'link\' href=\'index.php?file=schaatser&code=1995072801\'>Aveline Hijlkema</a></td><td>NED</td><td>1.18,12</td></tr>\n<tr><td>20</td><td><a class=\'link\' href=\'index.php?file=schaatser&code=1999070602\'>Michelle de Jong</a></td><td>NED</td><td>1.18,35</td></tr>\n<tr><td>21</td><td><a class=\'link\' href=\'index.php?file=schaatser&code=1992082401\'>Bo van der Werff</a></td><td>NED</td><td>1.18,87</td></tr>\n<tr><td>22</td><td><a class=\'link\' href=\'index.php?file=schaatser&code=2000092203\'>Maud Lugters</a></td><td>NED</td><td>1.18,91</td></tr>\n<tr><td>23</td><td><a class=\'link\' href=\'index.php?file=schaatser&code=1994081101\'>Leeyen Harteveld</a></td><td>NED</td><td>1.18,93</td></tr>\n<tr><td>DQ</td><td><a class=\'link\' href=\'index.php?file=schaatser&code=1996062101\'>Anouk Sanders</a></td><td>NED</td><td>-</td></tr>\n
================================================
>+>ijsbaan+>datum
Klaar