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
|
2019-06-28 10:50:59 +01:00
|
|
|
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
|
2019-04-30 23:02:29 +01:00
|
|
|
|
2019-05-14 12:39:17 +01:00
|
|
|
import os
|
|
|
|
import datetime
|
2019-08-07 15:45:25 +01:00
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger('spotframework')
|
|
|
|
|
2019-09-05 12:20:21 +01:00
|
|
|
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")
|
2019-09-05 12:20:21 +01:00
|
|
|
file_formatter = logging.Formatter(file_log_format)
|
|
|
|
file_handler.setFormatter(file_formatter)
|
2019-08-07 15:45:25 +01:00
|
|
|
|
|
|
|
logger.addHandler(file_handler)
|
2019-04-30 23:02:29 +01:00
|
|
|
|
2019-09-05 12:20:21 +01:00
|
|
|
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
|
|
|
|
2019-04-30 23:02:29 +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-10-01 19:20:22 +01:00
|
|
|
network.user.refresh_access_token()
|
2019-04-30 23:56:55 +01:00
|
|
|
|
2019-05-14 13:05:25 +01:00
|
|
|
found = False
|
2019-05-08 22:41:47 +01:00
|
|
|
|
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-08 22:41:47 +01:00
|
|
|
|
2019-05-14 13:05:25 +01:00
|
|
|
if found:
|
2019-05-08 22:41:47 +01:00
|
|
|
|
2019-06-28 10:50:59 +01:00
|
|
|
if os.path.exists(os.path.join(const.config_path, 'config.json')):
|
|
|
|
data = json.load_json(os.path.join(const.config_path, 'config.json'))
|
2019-05-08 22:41:47 +01:00
|
|
|
|
2019-06-28 10:50:59 +01:00
|
|
|
date = datetime.datetime.now()
|
2019-04-30 23:16:54 +01:00
|
|
|
|
2019-06-28 10:50:59 +01:00
|
|
|
playlists = network.get_user_playlists()
|
2019-05-14 12:39:17 +01:00
|
|
|
|
2019-06-28 10:50:59 +01:00
|
|
|
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'])
|
2019-06-28 10:50:59 +01:00
|
|
|
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']))
|
2019-06-28 10:50:59 +01:00
|
|
|
|
|
|
|
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')
|