import streamlit as st import os import requests from datetime import datetime, timedelta import pytz # 1. Page Setup st.set_page_config(page_title="SkyCast Pro", page_icon="☁️", layout="centered") # 2. Styling (Glassmorphism) st.markdown(""" """, unsafe_allow_html=True) st.title("SKYCAST PRO") # 3. API Logic API_KEY = os.environ.get('Api_key') def get_weather(city_name): if not API_KEY: return {"error": "API Key missing!"} url = f"https://api.openweathermap.org/data/2.5/weather?q={city_name}&appid={API_KEY}&units=metric" try: r = requests.get(url, timeout=10) return r.json() if r.status_code == 200 else {"error": "City not found"} except: return {"error": "Connection error"} # 4. Search & Dropdown (Duplicate City Fix) # Humne common cities ke options de diye hain taaki confusion na ho search_mode = st.radio("Search Method", ["Direct Search", "Quick Select"], horizontal=True) selected_city = "" if search_mode == "Quick Select": city_options = { "Hyderabad, Pakistan": "Hyderabad,PK", "Hyderabad, India": "Hyderabad,IN", "Karachi, Pakistan": "Karachi,PK", "Mumbai, India": "Mumbai,IN", "London, UK": "London,GB", "New York, USA": "New York,US" } choice = st.selectbox("Choose a City:", list(city_options.keys())) selected_city = city_options[choice] else: selected_city = st.text_input("Search Location", placeholder="e.g. Hyderabad,PK") # 5. Result Display if selected_city: with st.spinner("Fetching data..."): data = get_weather(selected_city) if "error" in data: st.error(data["error"]) else: # 6. CALCULATING LOCAL TIME # OpenWeather gives timezone offset in seconds from UTC timezone_offset = data['timezone'] utc_now = datetime.now(pytz.utc) local_time = utc_now + timedelta(seconds=timezone_offset) formatted_time = local_time.strftime('%H:%M | %A') # Extracting Details city = data['name'] country = data['sys']['country'] temp = round(data['main']['temp']) feels_like = round(data['main']['feels_like']) desc = data['weather'][0]['description'].title() humidity = data['main']['humidity'] wind = data['wind']['speed'] icon = data['weather'][0]['icon'] st.markdown(f"""
SkyCast Pro | Powered by OpenWeather
", unsafe_allow_html=True)