-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweatherapp.py
More file actions
71 lines (55 loc) · 2.61 KB
/
weatherapp.py
File metadata and controls
71 lines (55 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import requests
import os
from dotenv import load_dotenv
import google.generativeai as genai
load_dotenv()
def get_weather(api_key, city):
# Önce şehrin place_id'sini bul
place_url = "https://www.meteosource.com/api/v1/free/find_places"
place_params = {
"text": city,
"language": "en",
"key": api_key
}
try:
place_response = requests.get(place_url, params=place_params)
place_response.raise_for_status()
places = place_response.json()
if not places:
print("Şehir bulunamadı.")
return
place_id = places[0]['place_id']
# Hava durumu verilerini al
weather_url = f"https://www.meteosource.com/api/v1/free/point?place_id={place_id}§ions=current&key={api_key}"
weather_response = requests.get(weather_url)
weather_response.raise_for_status()
weather_data = weather_response.json()
current = weather_data.get('current', {})
print(f"{city} için hava durumu:")
print(f"Sıcaklık: {current.get('temperature', 'Bilgi yok')}°C")
print(f"Durum: {current.get('summary', 'Bilgi yok')}")
print(f"Yağış: {current.get('precipitation', {}).get('total', 'Bilgi yok')} mm")
except requests.exceptions.RequestException as e:
print(f"Hata oluştu: {e}")
return current
def generate_outfit(gemini_api_key, weather_data):
try:
genai.configure(api_key=gemini_api_key)
model = genai.GenerativeModel("gemini-2.0-flash-thinking-exp-01-21")
prompt = f"""Aşağıdaki hava durumu koşullarına uygun giyim önerileri yap:
- Sıcaklık: {weather_data.get('temperature', 'Bilinmiyor')}°C
- Hava Durumu: {weather_data.get('summary', 'Bilinmiyor')}
- Yağış: {weather_data.get('precipitation', {}).get('total', 0)} mm
- Rüzgar Hızı: {weather_data.get('wind', {}).get('speed', 0)} m/s
Günlük aktiviteler için pratik ve stil sahibi 3 farklı kombin öner. Kısa ve maddeler halinde ver."""
response = model.generate_content(prompt)
return response.text
except Exception as e:
return f"Hata oluştu: {str(e)}"
# Kullanım örneği
WEATHER_API_KEY = os.getenv("WEATHER_API_KEY") # Meteosource'dan alacağınız ücretsiz API anahtarı
CITY = input("Hava durumu için şehir adı girin: ")
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") # Google Gemini'den alacağınız API anahtarı
result = get_weather(WEATHER_API_KEY, CITY)
suggestion = generate_outfit(GEMINI_API_KEY, result)
print(suggestion)