Cuaca API for Python.
Clean example request for the weather feature endpoint. Replace YOUR API and the city input, then run the code.
Checking
Clean example request for the weather feature endpoint. Replace YOUR API and the city input, then run the code.
import requests
import urllib.parse
API_KEY = "YOUR API"
KOTA = "YOUR LOCATION"
ENDPOINT = "https://senpai-bot.store/cuaca"
def main():
encoded_city = urllib.parse.quote(KOTA, safe="")
full_url = f"{ENDPOINT}?apikey={API_KEY}&kota={encoded_city}"
try:
response = requests.get(full_url)
response.raise_for_status()
except requests.RequestException as e:
print(f"Gagal request: {e}")
return
try:
data = response.json()
except ValueError:
print("Gagal parsing JSON.")
return
if data.get("code") != 200:
print(f"API error code: {data.get('code')} - {data.get('message', 'Tidak diketahui')}")
return
result = data.get("result", {})
if result:
print("Informasi Cuaca:")
print(f"Kota : {result.get('kota')}")
print(f"Suhu : {result.get('celcius')} °C")
print(f"Cuaca : {result.get('cuaca')}")
print(f"Waktu : {result.get('waktu')}")
else:
print("Tidak ada data cuaca ditemukan.")
if __name__ == "__main__":
main()