spotframework/alarm.py

79 lines
2.3 KiB
Python
Raw Normal View History

2019-09-04 17:45:26 +01:00
from spotframework.net.user import NetworkUser
2019-06-11 11:59:24 +01:00
from spotframework.net.network import Network
import spotframework.net.const as const
import spotframework.io.json as json
2019-09-15 15:33:29 +01:00
import spotframework.util.monthstrings as month
import os
import datetime
2019-08-07 15:45:25 +01:00
import logging
logger = logging.getLogger('spotframework')
file_log_format = '%(asctime)s %(levelname)s %(name)s:%(funcName)s - %(message)s'
2019-08-07 15:45:25 +01:00
file_handler = logging.FileHandler(".spot/alarm.log")
file_formatter = logging.Formatter(file_log_format)
file_handler.setFormatter(file_formatter)
2019-08-07 15:45:25 +01:00
logger.addHandler(file_handler)
stream_log_format = '%(levelname)s %(name)s:%(funcName)s - %(message)s'
stream_formatter = logging.Formatter(stream_log_format)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(stream_formatter)
logger.addHandler(stream_handler)
2019-06-11 11:59:24 +01:00
def check_phone():
2019-04-30 23:56:55 +01:00
response = os.system("ping -c 1 -w5 " + os.environ['PHONEIP'] + " > /dev/null 2>&1")
2019-08-07 15:45:25 +01:00
logger.info('checking for phone')
2019-04-30 23:56:55 +01:00
if response == 0:
return True
else:
return False
2019-06-11 11:59:24 +01:00
if __name__ == '__main__':
2019-05-14 13:05:25 +01:00
try:
2019-04-30 23:56:55 +01:00
2019-09-04 17:45:26 +01:00
network = Network(NetworkUser(os.environ['SPOTCLIENT'],
os.environ['SPOTSECRET'],
os.environ['SPOTREFRESH']))
2019-04-30 23:56:55 +01:00
2019-05-14 13:05:25 +01:00
found = False
2019-05-14 13:05:25 +01:00
for i in range(0, 36):
2019-06-11 11:59:24 +01:00
if check_phone():
2019-05-14 13:05:25 +01:00
found = True
break
2019-05-14 13:05:25 +01:00
if found:
if os.path.exists(os.path.join(const.config_path, 'config.json')):
data = json.load_json(os.path.join(const.config_path, 'config.json'))
date = datetime.datetime.now()
playlists = network.get_user_playlists()
if data['alarm']['use_month']:
2019-09-15 15:33:29 +01:00
playlisturi = next((i.uri for i in playlists if i.name == month.get_this_month()),
data['alarm']['uri'])
else:
playlisturi = data['alarm']['uri']
2019-05-14 13:05:25 +01:00
2019-08-25 16:30:46 +01:00
network.play(uri=playlisturi, deviceid=network.get_device_id(data['alarm']['device_name']))
network.set_shuffle(True)
network.set_volume(data['alarm']['volume'])
network.next()
2019-05-14 13:05:25 +01:00
2019-08-07 15:45:25 +01:00
except Exception as e:
logger.exception('exception occured')