-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather_shellscript.py
More file actions
executable file
·51 lines (40 loc) · 1.55 KB
/
weather_shellscript.py
File metadata and controls
executable file
·51 lines (40 loc) · 1.55 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
import time
import openmeteo_requests
import pandas as pd
import requests_cache
import json
from pprint import pprint
from retry_requests import retry
# Setup the Open-Meteo API client with cache and retry on error
cache_session = requests_cache.CachedSession('.cache', expire_after = 3600)
retry_session = retry(cache_session, retries = 5, backoff_factor = 0.2)
openmeteo = openmeteo_requests.Client(session = retry_session)
url = "https://api.open-meteo.com/v1/forecast"
params = {
"latitude": 51.3396,
"longitude": 12.3713,
"current": ["temperature_2m", "precipitation"],
"forecast_days": 2
}
def truncate(f, n) -> str:
'''Truncates/pads a float f to n decimal places without rounding'''
s = '{}'.format(f)
if 'e' in s or 'E' in s:
return '{0:.{1}f}'.format(f, n)
i, p, d = s.partition('.')
return '.'.join([i, (d+'0'*n)[:n]])
responses = openmeteo.weather_api(url, params=params)
# Process first location. Add a for-loop for multiple locations or weather models
response = responses[0]
# Current values. The order of variables needs to be the same as requested.
current = response.Current()
current_temp = truncate(current.Variables(0).Value(),1) + "°C"
current_precipitation = current.Variables(1).Value()
# Get the timestamp and convert it into date/time -> seperate by space
ct = time.ctime(time.time())
ct = ct.split()
# Print the vars
print(f"Hello, user :)")
print(f"Time: {ct[3]} on the {ct[2]} { ct[1]} of",ct[4])
print(f"Current Temperature in Leipzig {current_temp}")
print(f"Current precipitation {current_precipitation} %")