# /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


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
   print (" ")
   time.sleep(300)   #Just wait 5 minutes before performing this read out again
