# /home/richard/py/FloraCollectData.py ****************************************
# Created by: HARB rboek2@gmail.com februar 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
paswoord = "Ha-8-H+"

while True: #Meaning this loop will run forever *******************************
   now = datetime.datetime.now()                  #Read the current system time
   stampz    = now.strftime("%Y-%m-%d %H:%M:%S")   #Format to a database format

   conn = psycopg2.connect(host="localhost",database="robotigs", 
      user="richard", password=paswoord)          #Create a database connection
   cur1 = conn.cursor()                        #Create a thread to the database
   cur1.execute("""SELECT name
                        , ipaddress  
                     FROM florastations
                    WHERE collectdata = TRUE
                 ORDER BY sort ASC """)                        #Define database
   rows = cur1.fetchall()                              #Perform database action
   for row in rows:                   #Scan all the stations listed in the file
      link = "http://" + row[1]                      #Calculate the entire link
      try:        #Make a provision for if the station does not answer properly
         r = requests.get(link, timeout=6.0000)            
         myfile = (r.text)
         print (row[0], stampz, myfile, end='')
         f= open("/var/www/html/flora/data/" + row[0] + ".txt","a+")
         f.write(stampz + " " + myfile)
         f.close() 
      except:                 #If the station does not answer properly use this
         pass         #Just do nothing if this station does not answer properly
   cur1.close()                              #Close this thread to the database
   conn.close()                          #Close this connection to the database


   link = "https://www.weerclubgouda.nl/weerdata/actueel/WCGactueelweer_vevida.php"
   try:            #Make a provision for if the station does not answer properly
      r = requests.get(link, timeout=5.000)            
      myfile = (r.text)
   except:                     #If the station does not answer properly use this
      pass             #Just do nothing if this station does not answer properly

   try:
      datum = re.search('van de Kinderboerderij op(.+?) om', myfile).group(1)
   except AttributeError:
     datum = '' # apply your error handling

   try:
      tijd = re.search('om (.+?) uur</b></font>', myfile).group(1)
   except AttributeError:
     tijd = '' # apply your error handling

   try:
      temp = re.search('color="#333333">(.+?)&deg;C</font>', myfile).group(1)
   except AttributeError:
     temp = '' # apply your error handling

   try:
      neerslag = re.search('color="#333333"><b>(.+?) mm</b></font>', myfile).group(1)
   except AttributeError:
     neerslag = '' # apply your error handling

   try:
      snelheid = re.search(' size="2" color="#333333"><b>(.+?) mm/uur</b></font>', myfile).group(1)
   except AttributeError:
     snelheid = '' # apply your error handling

   jaar = datum[7:9]
   maand = datum[4:6]
   dag = datum[1:3]
   uur = tijd[0:2]
   minuut = tijd[3:5]

   dataregel = " 20" + jaar + " " + maand + " " + dag + " " + uur + " " + minuut + " " +  temp + neerslag + " " + snelheid
   print ("Rainmeter " + stampz + dataregel)

   f= open("/var/www/html/flora/data/Rainmeter.txt","a+")
   f.write (stampz + dataregel + "\n")
   f.close() 


   print (" ")
   time.sleep(300)   #Just wait 5 minutes before performing this read out again
