#!/usr/bin/env python3

from datetime import datetime
import json, re, requests, psycopg2

RIDE_PAGE = "https://www.knoebels.com/ride"
PATTERN = r'ride-list-item" data-ride="(?P<ride>[\S\s]+?)"[\S\s]+?rounded">\$(?P<price>\d\.\d{2})'

db_params = {
    'dbname': 'gp0',
    'user': 'kuhnobowls',
    'password': 'saddog095',
    'host': '192.168.88.5',
    'port': '5432'
}

insert_query = """
    INSERT INTO knoebels.ride_data_in (time_stamp, ride, price)
    VALUES (%s, %s, %s)
"""

def generic_query(q):
    ret = {}
    with psycopg2.connect(**db_params) as conn:
        with conn.cursor() as cursor:
            cursor.execute(q)
            rows = cursor.fetchall()
            col_names = [desc[0] for desc in cursor.description]
            
            for row in rows:
                ret[row[1]] = row[2]
                    
    return ret

def log_to_database(ride, price):
    data = [datetime.now(), ride, price]

    # Connect to the database using a context manager
    with psycopg2.connect(**db_params) as conn:
        with conn.cursor() as cursor:
            cursor.execute(insert_query, data)
        conn.commit()

def fetch_webpage(url):
    HEADERS = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:124.0) Gecko/20100101 Firefox/124.0',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
        'Accept-Language': 'en-US,en;q=0.5',
        'DNT': '1',
        'Sec-GPC': '1',
        'Connection': 'keep-alive',
        'Upgrade-Insecure-Requests': '1',
        'Sec-Fetch-Dest': 'document',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-Site': 'none',
        'Sec-Fetch-User': '?1',
    }
    try:
        response = requests.get(url, headers=HEADERS)
        
        if response.status_code == 200:
            return response.text
        else:
            return f"Error: {response.status_code} - Unable to fetch webpage"
    except Exception as e:
        return f"Error: {str(e)} - Unable to fetch webpage"

current_values = generic_query("SELECT * FROM knoebels.ride_data_in")

html = fetch_webpage(RIDE_PAGE) # fetch page
matches = re.finditer(PATTERN, html) # run regex

data = []
for match in matches:
    data.append({'ride':match.group('ride').replace(' ', '_').lower(), 'price':match.group('price')}) # regex grabs number of bytes, cast to integer and divide by 1,073,741,824

for r in data:
    current_price = float(r["price"])
    try:
        old_price = float(current_values[r["ride"]])
    except:
        old_price = -1.0
    if current_price != old_price:
        print("New price for: ", r["ride"], current_price, ", Old Price: ", old_price)
        log_to_database(r["ride"], r["price"])
