|
| 1 | +import os |
| 2 | +from time import sleep |
| 3 | +import argparse |
| 4 | +import logging |
| 5 | +import traceback |
| 6 | +import requests.exceptions |
| 7 | + |
| 8 | +from spotipy_lib.spotipy import Spotipy |
| 9 | +from spotipy_lib.spotipy import SpotifyException |
| 10 | +from configuration.configuration import Configuration |
| 11 | + |
| 12 | +logger = logging.getLogger('Spotify Connect') |
| 13 | + |
| 14 | + |
| 15 | +def _setup_log(log_path: str = 'logs/output.log', debug: bool = False) -> None: |
| 16 | + """ Sets up logger. """ |
| 17 | + |
| 18 | + log_path = log_path.split(os.sep) |
| 19 | + if len(log_path) > 1: |
| 20 | + try: |
| 21 | + os.makedirs((os.sep.join(log_path[:-1]))) |
| 22 | + except FileExistsError: |
| 23 | + pass |
| 24 | + log_filename = os.sep.join(log_path) |
| 25 | + # noinspection PyArgumentList |
| 26 | + logging.basicConfig(level=logging.INFO if debug is not True else logging.DEBUG, |
| 27 | + format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', |
| 28 | + datefmt='%Y-%m-%d %H:%M:%S', |
| 29 | + handlers=[ |
| 30 | + logging.FileHandler(log_filename), |
| 31 | + # logging.handlers.TimedRotatingFileHandler(log_filename, when='midnight', interval=1), |
| 32 | + logging.StreamHandler() |
| 33 | + ] |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +def _argparser() -> argparse.Namespace: |
| 38 | + """ Parses and returns the command line arguments. """ |
| 39 | + |
| 40 | + parser = argparse.ArgumentParser( |
| 41 | + description='Script to trigger Spotify Connect to play on specific device.', |
| 42 | + add_help=False) |
| 43 | + # Required Args |
| 44 | + required_arguments = parser.add_argument_group('Required Arguments') |
| 45 | + config_file_params = { |
| 46 | + 'type': argparse.FileType('r'), |
| 47 | + 'required': True, |
| 48 | + 'help': "The configuration yml file" |
| 49 | + } |
| 50 | + |
| 51 | + required_arguments.add_argument('-c', '--config-file', **config_file_params) |
| 52 | + required_arguments.add_argument('-l', '--log', help="Name of the output log file") |
| 53 | + # Optional args |
| 54 | + optional = parser.add_argument_group('Optional Arguments') |
| 55 | + optional.add_argument('-d', '--debug', action='store_true', help='Enables the debug log messages') |
| 56 | + optional.add_argument("-h", "--help", action="help", help="Show this help message and exit") |
| 57 | + |
| 58 | + return parser.parse_args() |
| 59 | + |
| 60 | + |
| 61 | +def main(): |
| 62 | + """ |
| 63 | + Handles the core flow of SpotiClick. |
| 64 | +
|
| 65 | + :Example: |
| 66 | + python main.py -c confs/raspotify_conf.yml |
| 67 | + -l logs/spoticlick.log |
| 68 | + """ |
| 69 | + |
| 70 | + # Initializing |
| 71 | + args = _argparser() |
| 72 | + _setup_log(args.log, args.debug) |
| 73 | + # Load the configuration |
| 74 | + configuration = Configuration(config_src=args.config_file) |
| 75 | + # Init Spotipy |
| 76 | + spoti_read_config = configuration.get_spotifies()[0] |
| 77 | + print(spoti_read_config) |
| 78 | + spoti_modify_config = configuration.get_spotifies()[1] |
| 79 | + target_device_id_1 = spoti_read_config["target_device_id"] |
| 80 | + target_device_id_2 = spoti_modify_config["target_device_id"] |
| 81 | + spot_read = Spotipy(config=spoti_read_config, token_id='read') |
| 82 | + # Check active |
| 83 | + device_1_active = spot_read.is_target_device_active() |
| 84 | + if not device_1_active: |
| 85 | + target_device_id = target_device_id_1 |
| 86 | + else: |
| 87 | + target_device_id = target_device_id_2 |
| 88 | + |
| 89 | + spot_modify = Spotipy(config=spoti_modify_config, token_id='modify') |
| 90 | + # Change device |
| 91 | + logger.info("Transferring music to device id: %s" % target_device_id) |
| 92 | + spot_modify.play_on_device(target_device_id=target_device_id, session_info=spot_read.get_playback_info()) |
| 93 | + logger.info("Music Transferred!") |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == '__main__': |
| 97 | + try: |
| 98 | + main() |
| 99 | + except Exception as e: |
| 100 | + logging.error(str(e) + '\n' + str(traceback.format_exc())) |
| 101 | + raise e |
0 commit comments