forked from DJAkbar/cloudy-a
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudy.py
executable file
·77 lines (64 loc) · 2.08 KB
/
cloudy.py
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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import logging
import random
import sys
import time
from pathlib import Path, PurePath
from colorzero import Color
from darksky.api import DarkSky
from gpiozero import RGBLED
from gpiozero.pins.pigpio import PiGPIOFactory
WEATHER_COLOR_MAP = {
'clear-day': 'yellow',
'clear-night': 'yellow',
'cloudy': 'white',
'fog': 'white',
'partly-cloudy-day': 'white',
'partly-cloudy-night': 'white',
'rain': 'blue',
'sleet': 'darkslategrey',
'snow': 'darkslategrey',
'unknown_icon': 'purple',
'wind': 'yellow',
}
def weather_indicator(led, color):
led.off()
for i in range(20):
led.pulse(
random.uniform(0.05, 0.06),
random.uniform(0.05, 0.06),
on_color=Color(color),
n=1,
)
time.sleep(random.uniform(0.1, 0.5))
led.off()
def main(api_key, lat, long):
# log in home dir
logging.basicConfig(
filename=PurePath(Path.home()).joinpath('logs/cloud.log'), level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
pi_led = RGBLED(red=17, green=27, blue=24, pin_factory=PiGPIOFactory())
darksky = DarkSky(api_key)
forecast = darksky.get_forecast(
lat, long, exclude=['minutely', 'hourly', 'daily', 'alerts', 'flags']
).currently.icon
logging.info('Icon: {}'.format(forecast))
weather_indicator(pi_led, WEATHER_COLOR_MAP.get(forecast, 'unknown_icon'))
if __name__ == '__main__':
import argparse
try:
parser = argparse.ArgumentParser()
parser.add_argument(
'--api-key',
help='Black Sky API key. Get one at https://darksky.net/dev/register',
required=True,
)
parser.add_argument('--lat', help='Location latitude', required=True)
parser.add_argument('--long', help='Location longitude', required=True)
args = parser.parse_args()
main(api_key=args.api_key, lat=args.lat, long=args.long)
except KeyboardInterrupt:
print('\nExiting by user request.\n')
sys.exit(0)