# /home/richard/py/regenmeter.py ***********************************************
# Created by: HARB rboek2@gmail.com april 2020 GPL copyrights                
# Prepaired and optimized for use with Python 3


# IMPORT LIBRARIES install like: pip install --upgrade psycopg2 --user *********
import platform                         #needed to check if we are using Python3
#import psycopg2             #Needed to communicate with any PostgreSQL database
import datetime                 #Needed to create a timestamp for the datarecord
import time            #Needed to be able to perform the request every 5 minutes
import requests                            #Simplest internet reader for Python3
import urllib          #Needed to load the contents of a webpage into a variable
import re                                 #Regular expressions to handle strings

python_version=platform.python_version()                 #Actual version request
print ("De gebruikte versie van Python is:", python_version)       #Show to user
#paswoord = input("Enter password voor de database: ")        #To avoid exposure
link = "https://www.weerclubgouda.nl/weerdata/actueel/WCGactueelweer_vevida.php"

while True: #Meaning this loop will run forever ********************************

   try:            #Make a provision for if the station does not answer properly
      r = requests.get(link, timeout=5.000)            
      myfile = (r.text)
      #print (" -", myfile, "- ")
   except:                     #If the station does not answer properly use this
      pass             #Just do nothing if this station does not answer properly

   str1 = "van de Kinderboerderij op"
   startpos = (myfile.find(str1))

   try:
      datum = re.search('van de Kinderboerderij op(.+?) om', myfile).group(1)
   except AttributeError:
      # AAA, ZZZ not found in the original string
     datum = '' # apply your error handling

   try:
      tijd = re.search('om (.+?) uur</b></font>', myfile).group(1)
   except AttributeError:
      # AAA, ZZZ not found in the original string
     tijd = '' # apply your error handling

   try:
      temp = re.search('color="#333333">(.+?)&deg;C</font>', myfile).group(1)
   except AttributeError:
      # AAA, ZZZ not found in the original string
     temp = '' # apply your error handling





   print (startpos, "-", datum, "-", tijd, "-", temp)
   time.sleep(300)    #Just wait 5 minutes before performing this read out again
