95 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			95 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
import aiohttp, asyncio, io
 | 
						|
import customtkinter as tk
 | 
						|
from PIL import Image
 | 
						|
from datetime import datetime, timedelta, timezone
 | 
						|
 | 
						|
API_KEY_TOP_SECRET_DO_NOT_LEAK = "75b4074e2a7743ef8c9121626252110"
 | 
						|
 | 
						|
async def fetch_weather(q):
 | 
						|
    async with aiohttp.ClientSession() as session:
 | 
						|
        async with session.get(f"https://api.weatherapi.com/v1/current.json?key={API_KEY_TOP_SECRET_DO_NOT_LEAK}&q={q}") as response:
 | 
						|
            data = await response.json()
 | 
						|
            return data
 | 
						|
 | 
						|
class App(tk.CTk):
 | 
						|
    def __init__(self):
 | 
						|
        super().__init__()
 | 
						|
        self.geometry("600x400")
 | 
						|
        self.resizable(False, False)
 | 
						|
        
 | 
						|
        self.input = tk.CTkEntry(self, placeholder_text="location")
 | 
						|
        self.button = tk.CTkButton(self, text="get wetha", command=self.button_callbck)
 | 
						|
        self.input.pack(padx=0, pady=20)
 | 
						|
        self.button.pack(padx=20, pady=0)
 | 
						|
        
 | 
						|
        top_frame = tk.CTkFrame(self, fg_color="transparent")
 | 
						|
        top_frame.pack(pady=20)
 | 
						|
 | 
						|
        self.location = tk.CTkLabel(top_frame, text="", fg_color="transparent", width=150, justify="center")
 | 
						|
        self.region = tk.CTkLabel(top_frame, text="", fg_color="transparent", width=150, justify="center")
 | 
						|
        self.country = tk.CTkLabel(top_frame, text="", fg_color="transparent", width=150, justify="center")
 | 
						|
 | 
						|
        self.location.pack(side="left", padx=20)
 | 
						|
        self.region.pack(side="left", padx=20)
 | 
						|
        self.country.pack(side="left", padx=20)
 | 
						|
 | 
						|
        bottom_frame = tk.CTkFrame(self, fg_color="transparent")
 | 
						|
        bottom_frame.pack(pady=5)
 | 
						|
 | 
						|
        self.state = tk.CTkLabel(bottom_frame, text="", fg_color="transparent", width=150, justify="center")
 | 
						|
        self.temp = tk.CTkLabel(bottom_frame, text="", fg_color="transparent", width=150, justify="center")
 | 
						|
 | 
						|
        self.state.pack(side="left", padx=20)
 | 
						|
        self.temp.pack(side="left", padx=20)
 | 
						|
        
 | 
						|
        self.icon_frame = tk.CTkFrame(self, fg_color="transparent")
 | 
						|
        self.icon_frame.pack(pady=10)
 | 
						|
 | 
						|
        self.icon_label = tk.CTkLabel(self.icon_frame, text="")
 | 
						|
        self.icon_label.pack()
 | 
						|
        
 | 
						|
        self.updated_frame = tk.CTkFrame(self, fg_color="transparent")
 | 
						|
        self.updated_frame.pack(pady=45)
 | 
						|
        
 | 
						|
        self.last_updated = tk.CTkLabel(self.updated_frame, text="")
 | 
						|
        self.last_updated.pack()
 | 
						|
 | 
						|
    def button_callbck(self):
 | 
						|
        data = asyncio.run(fetch_weather(self.input.get()))
 | 
						|
        
 | 
						|
        location = data.get('location', {}).get('name')
 | 
						|
        region = data.get('location', {}).get('region')
 | 
						|
        country = data.get('location', {}).get('country')
 | 
						|
        state = data.get('current', {}).get('condition', {}).get('text')
 | 
						|
        icon_url = "https:" + data.get('current', {}).get('condition', {}).get('icon', '')
 | 
						|
        temp = data.get('current', {}).get('temp_c')
 | 
						|
        last_updated = data.get('current', {}).get('last_updated_epoch')
 | 
						|
 | 
						|
        self.location.configure(text=f"Location: {location}")
 | 
						|
        self.region.configure(text=f"Region: {region}")
 | 
						|
        self.country.configure(text=f"Country: {country}")
 | 
						|
        self.state.configure(text=f"Weather: {state}")
 | 
						|
        self.temp.configure(text=f"Temp: {temp}°C")
 | 
						|
        self.last_updated.configure(text=f"Last Updated: {datetime.fromtimestamp(last_updated, tz=timezone(timedelta(hours=2))).strftime("%d.%m.%Y %H:%M:%S %Z")}")
 | 
						|
        
 | 
						|
        asyncio.run(self.load_icon(icon_url))
 | 
						|
        
 | 
						|
    async def load_icon(self, url):
 | 
						|
        try:
 | 
						|
            async with aiohttp.ClientSession() as session:
 | 
						|
                async with session.get(url) as response:
 | 
						|
                    response.raise_for_status()
 | 
						|
                    img_data = await response.read()
 | 
						|
                    pil_image = Image.open(io.BytesIO(img_data)).resize((64, 64))
 | 
						|
                    ctk_image = tk.CTkImage(light_image=pil_image, dark_image=pil_image, size=(64, 64))
 | 
						|
            
 | 
						|
            self.icon_label.configure(image=ctk_image, text="")
 | 
						|
            self.icon_label.image = ctk_image
 | 
						|
        except Exception:
 | 
						|
            self.icon_label.configure(image=None, text="Icon N/A")
 | 
						|
        
 | 
						|
    tk.set_appearance_mode("dark")
 | 
						|
    tk.set_default_color_theme("dark-blue")
 | 
						|
 | 
						|
app = App()
 | 
						|
app.mainloop() |