-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
73 lines (58 loc) · 1.95 KB
/
main.py
File metadata and controls
73 lines (58 loc) · 1.95 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
import configparser
import datetime
import logging
import os
import signal
import sys
import time
from logging.config import fileConfig
from typing import Union
import requests
import spotipy
from cachetools.func import ttl_cache
from spotipy.oauth2 import SpotifyOAuth
from display import Display
def signal_handler(sig, frame):
logging.info(f'Signal {sig} received')
display.cleanup()
sys.exit(0)
@ttl_cache(ttl=60*60)
def current_temperature() -> str:
api_key = os.environ['WEATHER_API_KEY']
lat, lon = config['weather']['latitude'], config['weather']['longitude']
response = requests.get(
f'https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&units=metric&appid={api_key}')
if not response.ok:
return 'ERROR'
temp_value = response.json()['main']['temp']
return f'{temp_value:.0f} C'
@ttl_cache(ttl=60)
def current_music() -> Union[str, None]:
scope = 'user-read-playback-state'
spotify = spotipy.Spotify(client_credentials_manager=SpotifyOAuth(scope=scope))
result = spotify.currently_playing()
if result is not None:
try:
return f"{result['item']['artists'][0]['name']} - {result['item']['name']}"
except (TypeError, KeyError):
return None
else:
return None
if __name__ == "__main__":
fileConfig('logging.ini')
config = configparser.ConfigParser()
config.read('config.ini')
display = Display(config['led'].getint('width'), config['led'].getint('height'))
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
while True:
try:
text = current_music()
if text is None:
text = current_temperature()
display.print(text)
time.sleep(2)
except Exception as e:
logging.exception('Caught exception')
display.print(datetime.datetime.now().strftime('%H:%M'))
time.sleep(600)