-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathmain.py
More file actions
85 lines (67 loc) · 2.48 KB
/
main.py
File metadata and controls
85 lines (67 loc) · 2.48 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""
Retrieve weather data for a given city and log it.
"""
import csv
import logging
from datetime import datetime
from typing import Optional
import os
import pytz
import requests
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())
def get_weather_data(city: str) -> Optional[dict]:
"""
Get weather data for the given city from the Talk Python Weather API.
:param city: The city for which to get the weather data.
:return: A dictionary containing the weather data or None if the request failed.
"""
api_url = "https://weather.talkpython.fm/api/weather/"
params = {"city": city, "country": "IN"}
try:
response = requests.get(api_url, params=params, timeout=10)
return response.json() if response.status_code == 200 else None
except (requests.Timeout, requests.RequestException):
return None
def log_weather_data(city: str, data: dict) -> None:
"""
Log the weather data for a given city.
:param city: The city for which the weather data is logged.
:param data: The weather data to be logged.
"""
logger.info("City: %s", city)
logger.info("Temperature: %s", data.get("forecast", {}).get("temp"))
logger.info("Feels Like: %s", data.get("forecast", {}).get("feels_like"))
logger.info("Humidity: %s", data.get("forecast", {}).get("humidity"))
utc_now = datetime.utcnow()
ist_now = utc_now.astimezone(pytz.timezone("Asia/Kolkata"))
logger.info("Current IST time: %s", ist_now.strftime("%Y-%m-%d %H:%M:%S"))
with open("weather_data.csv", mode="a", newline="", encoding="utf-8") as file:
writer = csv.writer(file)
if file.tell() == 0:
writer.writerow(["Timestamp", "Temperature", "Feels Like", "Humidity"])
writer.writerow(
[
ist_now.strftime("%Y-%m-%d %H:%M:%S"),
data.get("forecast", {}).get("temp"),
data.get("forecast", {}).get("feels_like"),
data.get("forecast", {}).get("humidity"),
]
)
def main() -> None:
"""
The main function for this module.
"""
token = os.environ.get("SOME_SECRET")
if token is None:
logger.error("Token value is not available.")
return
city = "Pune"
data = get_weather_data(city)
if data is None:
logger.error("Failed to get weather data for %s.", city)
return
log_weather_data(city, data)
if __name__ == "__main__":
main()