Skip to content

Added a new feature uv_sunscreen_advisor #977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions uv_sunscreen_advisor/README.md
Original file line number Diff line number Diff line change
@@ -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
82 changes: 82 additions & 0 deletions uv_sunscreen_advisor/uv_sunscreen_advisor.py
Original file line number Diff line number Diff line change
@@ -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()