From a33472ea560f1726df9d8805f26472d64e291bc0 Mon Sep 17 00:00:00 2001 From: ryansurf Date: Sun, 26 May 2024 14:45:11 -0700 Subject: [PATCH] Cleaned up main.py, fixed index.js from displaying wrong data --- src/backend/api.py | 41 +++++++++++++-- src/backend/helper.py | 89 ++++++++++++++++++++++++++++++++ src/backend/main.py | 112 ++++++++--------------------------------- src/backend/server.py | 5 +- src/frontend/script.js | 7 ++- 5 files changed, 154 insertions(+), 100 deletions(-) diff --git a/src/backend/api.py b/src/backend/api.py index 63e65a9..c4ea87d 100644 --- a/src/backend/api.py +++ b/src/backend/api.py @@ -8,7 +8,7 @@ from retry_requests import retry import requests import pandas as pd -from helper import round_decimal +import helper def get_coordinates(args): @@ -134,13 +134,13 @@ def forecast(lat, long, decimal, days=0): response = responses[0] - daily_height_max = round_decimal( + daily_height_max = helper.round_decimal( response.Daily().Variables(0).ValuesAsNumpy(), decimal ) - daily_direction_dominant = round_decimal( + daily_direction_dominant = helper.round_decimal( response.Daily().Variables(1).ValuesAsNumpy(), decimal ) - daily_period_max = round_decimal( + daily_period_max = helper.round_decimal( response.Daily().Variables(2).ValuesAsNumpy(), decimal ) @@ -159,3 +159,36 @@ def forecast(lat, long, decimal, days=0): daily_period_max, daily_data["date"], ] + +def gather_data(lat, long, ocean): + """ + Calls APIs though python files + """ + ocean_data = ocean_information(lat, long, ocean["decimal"], ocean["unit"]) + uv_index = get_uv(lat, long, ocean["decimal"], ocean["unit"]) + + ocean["ocean_data"] = ocean_data + ocean["uv_index"] = uv_index + + data_dict = { + "Location : ": ocean["city"], + "Height: ": ocean_data[0], + "Direction: ": ocean_data[1], + "Period: ": ocean_data[2], + "UV Index: ": uv_index, + } + return ocean_data, uv_index, data_dict + +def seperate_args_and_get_location(args): + """ + Gets user's coordinates from either the argument(location=) or, if none, + the default coordinates(default_location()) + """ + coordinates = get_coordinates(args) + location_data = { + "coordinates" : coordinates, + "lat" : coordinates[0], + "long" : coordinates[1], + "city" : coordinates[2] + } + return location_data \ No newline at end of file diff --git a/src/backend/helper.py b/src/backend/helper.py index 10e0f2a..ba1c183 100644 --- a/src/backend/helper.py +++ b/src/backend/helper.py @@ -1,6 +1,34 @@ """ General helper functions """ +import subprocess +import json +import api +import art + +def arguments_dictionary(lat, long, city, args): + """ + Dictionary to keep cli argument values + """ + arguments = { + "lat": lat, + "long": long, + "city": city, + "show_wave": 1, + "show_large_wave": 0, + "show_uv": 1, + "show_height": 1, + "show_direction": 1, + "show_period": 1, + "show_city": 1, + "show_date": 1, + "json_output" : 0, + "unit": "imperial", + "decimal": extract_decimal(args), + "forecast_days": get_forecast_days(args), + "color": get_color(args) + } + return arguments def query_to_args_list(query): """ @@ -110,3 +138,64 @@ def round_decimal(round_list, decimal): for num in round_list: rounded_list.append(round(num, decimal)) return rounded_list + +def set_output_values(args, ocean): + """ + Takes a list of command line arguments(args) and sets the appropritate values + in the ocean dictionary(show_wave = 1, etc) + """ + if "hide_wave" in args or "hw" in args: + ocean["show_wave"] = 0 + if "show_large_wave" in args or "slw" in args: + ocean["show_large_wave"] = 1 + if "hide_uv" in args or "huv" in args: + ocean["show_uv"] = 0 + if "hide_height" in args or "hh" in args: + ocean["show_height"] = 0 + if "hide_direction" in args or "hdir" in args: + ocean["show_direction"] = 0 + if "hide_period" in args or "hp" in args: + ocean["show_period"] = 0 + if "hide_location" in args or "hl" in args: + ocean["show_city"] = 0 + if "hide_date" in args or "hdate" in args: + ocean["show_date"] = 0 + if "metric" in args or "m" in args: + ocean["unit"] = "metric" + if "json" in args or "j" in args: + ocean["json_output"] = 1 + return ocean + +def start_website(website): + """ + If the WEBSITE .env variable is true, the webserver is started + """ + if website == True: + subprocess.Popen(["python", "-m", "http.server", "9000"], cwd="../frontend") + +def json_output(data_dict): + """ + If JSON=TRUE in .args, we print and return the JSON data + """ + json_output = json.dumps(data_dict, indent=4) + print(json_output) + return json_output + +def print_outputs(lat, long, coordinates, ocean_data, arguments): + """ + Basically the main printing function, calls all the other printing functions + """ + print("\n") + if coordinates == "No data": + print("No location found") + if ocean_data == "No data": + print(coordinates) + print("No ocean data at this location.") + else: + print_location(arguments["city"], arguments["show_city"]) + art.print_wave(arguments["show_wave"], arguments["show_large_wave"], arguments["color"]) + print_output(arguments) + print("\n") + forecast = api.forecast(lat, long, arguments["decimal"], arguments["forecast_days"]) + print_forecast(arguments, forecast) + diff --git a/src/backend/main.py b/src/backend/main.py index 0a11d4b..119e73c 100644 --- a/src/backend/main.py +++ b/src/backend/main.py @@ -8,115 +8,43 @@ import art import os import subprocess -import json from dotenv import load_dotenv +from helper import arguments_dictionary -# Load environment variables from .env file -load_dotenv(override=True) -website = bool(os.getenv("WEBSITE")) - -if website == True: - subprocess.Popen(["python", "-m", "http.server", "9000"], cwd="../frontend") - +#Seperates the cli args into a list args = helper.seperate_args(sys.argv) -# sys cli inputs -# Defaults. 1 == Show, anything else == hide -coordinates = api.get_coordinates(args) -lat = coordinates[0] -long = coordinates[1] -city = coordinates[2] - -# Dictionary to keep ocean vars -ocean = { - "lat": lat, - "long": long, - "city": city, - "show_wave": 1, - "show_large_wave": 0, - "show_uv": 1, - "show_height": 1, - "show_direction": 1, - "show_period": 1, - "show_city": 1, - "show_date": 1, - "json_output" : 0, - "unit": "imperial", - "decimal": helper.extract_decimal(args), - "forecast_days": helper.get_forecast_days(args), - "color": helper.get_color(args), -} - -# Check if specific options are present in args and update ocean dictionary accordingly -if "hide_wave" in args or "hw" in args: - ocean["show_wave"] = 0 -if "show_large_wave" in args or "slw" in args: - ocean["show_large_wave"] = 1 -if "hide_uv" in args or "huv" in args: - ocean["show_uv"] = 0 -if "hide_height" in args or "hh" in args: - ocean["show_height"] = 0 -if "hide_direction" in args or "hdir" in args: - ocean["show_direction"] = 0 -if "hide_period" in args or "hp" in args: - ocean["show_period"] = 0 -if "hide_location" in args or "hl" in args: - ocean["show_city"] = 0 -if "hide_date" in args or "hdate" in args: - ocean["show_date"] = 0 -if "metric" in args or "m" in args: - ocean["unit"] = "metric" -if "json" in args or "j" in args: - ocean["json_output"] = 1 +# return coordinates, lat, long, city +location = api.seperate_args_and_get_location(args) +coordinates, city = location["coordinates"], location["city"] +lat, long = location["lat"], location["long"] -def gather_data(lat=lat, long=long): - # Calls APIs though python files - ocean_data = api.ocean_information(lat, long, ocean["decimal"], ocean["unit"]) - uv_index = api.get_uv(lat, long, ocean["decimal"], ocean["unit"]) +#Sets ocean = dictionary with +arguments = helper.arguments_dictionary(lat, long, city, args) +#Updates the ocean dict with the valeus from the arguments +arguements = helper.set_output_values(args, arguments) - ocean["ocean_data"] = ocean_data - ocean["uv_index"] = uv_index - data_dict = { - "Location : ": city, - "Height: ": ocean_data[0], - "Direction: ": ocean_data[1], - "Period: ": ocean_data[2], - "UV Index: ": uv_index, - } - return ocean_data, uv_index, data_dict - - -def main(lat=lat, long=long): +def main(lat, long): """ Main function """ lat = float(lat) long = float(long) - data = gather_data(lat, long) + # Makes calls to the apis(ocean, UV) and returns the values + data = api.gather_data(lat, long, arguments) ocean_data = data[0] uv_index = data[1] data_dict = data[2] - if ocean["json_output"] == 0: - print("\n") - if coordinates == "No data": - print("No location found") - if ocean_data == "No data": - print(coordinates) - print("No ocean data at this location.") - else: - helper.print_location(ocean["city"], ocean["show_city"]) - art.print_wave(ocean["show_wave"], ocean["show_large_wave"], ocean["color"]) - helper.print_output(ocean) - print("\n") - forecast = api.forecast(lat, long, ocean["decimal"], ocean["forecast_days"]) - helper.print_forecast(ocean, forecast) + #Non-JSON output + if arguments["json_output"] == 0: + helper.print_outputs(lat, long, coordinates, ocean_data, arguments) return data_dict + #JSON Output else: - json_output = json.dumps(data_dict, indent=4) - print(json_output) + json_output = helper.json_output(data_dict) return json_output -if __name__ == "__main__": - main(lat, long) +main(lat, long) + diff --git a/src/backend/server.py b/src/backend/server.py index 0ad9605..1523971 100644 --- a/src/backend/server.py +++ b/src/backend/server.py @@ -5,6 +5,7 @@ import os import http.server import subprocess +import helper from urllib.parse import urlparse, parse_qs from dotenv import load_dotenv from helper import query_to_args_list @@ -14,8 +15,8 @@ port_env = int(os.getenv("PORT")) website = bool(os.getenv("WEBSITE")) -if website == True: - subprocess.Popen(["python", "-m", "http.server", "9000"], cwd="../frontend") +#Starts website if website==True +helper.start_website(website) class MyHTTPRequestHandler(http.server.BaseHTTPRequestHandler): diff --git a/src/frontend/script.js b/src/frontend/script.js index a1c62cc..2e99c8e 100644 --- a/src/frontend/script.js +++ b/src/frontend/script.js @@ -7,6 +7,8 @@ document.getElementById("reportForm").addEventListener("submit", function(event) // Get the value of the location input field var location = document.getElementById("curlInput").value; + location = location.replace(/\s+/g, "_"); + console.log("LOCATION: ", location) // Function to handle the response from the HTTP GET request function handleResponse(responseText) { @@ -15,7 +17,7 @@ document.getElementById("reportForm").addEventListener("submit", function(event) } // Construct the URL with the location query parameter - var url = `http://${ipAddress}:${port}?args=location=${encodeURIComponent(location)}`; + var url = `http://${ipAddress}:${port}?location=${encodeURIComponent(location)}`; console.log(url); // Call httpGetAsync with the URL and the handleResponse function as parameters @@ -41,7 +43,8 @@ document.addEventListener("submit", function() { // Make the HTTP GET request // Get the value of the location input field var location = document.getElementById("curlInput").value; - fetch(`http://${ipAddress}:${port}?args=location=${encodeURIComponent(location)}`) + location = location.replace(/\s+/g, "_"); + fetch(`http://${ipAddress}:${port}?location=${encodeURIComponent(location)}`) .then(response => response.text()) .then(data => { // Parse the response text to extract the desired information