From 40a6f4479eb22c46bc261735da4370bb77fdbdf2 Mon Sep 17 00:00:00 2001 From: Rupesh-Singh-Karki Date: Fri, 11 Oct 2024 00:24:44 +0530 Subject: [PATCH] Added a new feature uv_sunscreen_advisor --- uv_sunscreen_advisor/README.md | 23 ++++++ uv_sunscreen_advisor/uv_sunscreen_advisor.py | 82 ++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 uv_sunscreen_advisor/README.md create mode 100644 uv_sunscreen_advisor/uv_sunscreen_advisor.py diff --git a/uv_sunscreen_advisor/README.md b/uv_sunscreen_advisor/README.md new file mode 100644 index 000000000..b9c61a9a4 --- /dev/null +++ b/uv_sunscreen_advisor/README.md @@ -0,0 +1,23 @@ +# UV Index and Sunscreen Advisor + +This Python script fetches the current UV index for a given location and provides personalized sunscreen advice based on the UV index and whether the user is indoors or outdoors. + +## Features + +- Fetches real-time UV index data based on user-provided latitude and longitude. +- Provides personalized sunscreen advice based on UV exposure: + - Low UV index: minimal protection recommended. + - Moderate to extreme UV index: higher SPF and protective measures are suggested. +- Differentiates advice for indoor and outdoor scenarios. + +## Requirements + +- Python 3.x +- `requests` library for making HTTP API requests. + +## Setup + +### Install the required libraries: + +```bash +pip install requests diff --git a/uv_sunscreen_advisor/uv_sunscreen_advisor.py b/uv_sunscreen_advisor/uv_sunscreen_advisor.py new file mode 100644 index 000000000..7a2bee894 --- /dev/null +++ b/uv_sunscreen_advisor/uv_sunscreen_advisor.py @@ -0,0 +1,82 @@ +import requests + + +# Function to get UV index from the API +def get_uv_index(lat, lon, api_key): + url = ( + f"http://api.openweathermap.org/data/2.5/uvi?" + f"lat={lat}&lon={lon}&appid={api_key}" + ) + response = requests.get(url) + if response.status_code == 200: + uv_data = response.json() + return uv_data['value'] + else: + return None + + +# Function to provide sunscreen advice based on UV index and location +def sunscreen_advice(uv_index, indoor): + if indoor: + return ( + "You are indoors, sunscreen is not necessary unless you're " + "near windows with intense sunlight." + ) + + advice = "" + if uv_index < 3: + advice = ( + "Low UV index. No sunscreen is needed, but wearing sunglasses " + "is a good idea." + ) + elif 3 <= uv_index < 6: + advice = ( + "Moderate UV index. Wear SPF 15-30 sunscreen and consider " + "sunglasses and a hat." + ) + elif 6 <= uv_index < 8: + advice = ( + "High UV index. Use SPF 30-50 sunscreen, sunglasses, and " + "seek shade if possible." + ) + elif 8 <= uv_index < 11: + advice = ( + "Very high UV index. Use SPF 50+ sunscreen, wear protective " + "clothing, sunglasses, and avoid direct sun exposure between " + "10 AM and 4 PM." + ) + else: + advice = ( + "Extreme UV index. Stay indoors or use very high SPF sunscreen, " + "protective clothing, sunglasses, and avoid the sun as much " + "as possible." + ) + + return advice + + +# Main function +def main(): + # Enter your details + lat = input("Enter your latitude: ") + lon = input("Enter your longitude: ") + indoor = input("Are you indoors? (yes/no): ").lower() == 'yes' + + # Enter your API key here + # Visit this link to get your API key - https://openweathermap.org/api + api_key = 'your_api_key_here' + + # Fetch the UV-Index at the input latitude and longitude + uv_index = get_uv_index(lat, lon, api_key) + + if uv_index is not None: + print(f"Current UV index: {uv_index}") + # Provide sunscreen advice + advice = sunscreen_advice(uv_index, indoor) + print(advice) + else: + print("Error fetching UV index data.") + + +if __name__ == "__main__": + main()