Exemplo n.º 1
0
def main():
    """
    Handles the core flow of SpotiClick.

    :Example:
    python main.py -c confs/raspotify_conf.yml
                   -l logs/spoticlick.log
    """

    # Initializing
    args = _argparser()
    _setup_log(args.log, args.debug)
    # Load the configuration
    configuration = Configuration(config_src=args.config_file)
    # Init Spotipy
    spoti_read_config = configuration.get_spotifies()[0]
    spoti_modify_config = configuration.get_spotifies()[1]
    target_device_id = spoti_modify_config["target_device_id"]
    spot_read = Spotipy(config=spoti_read_config, token_id='read')
    spot_modify = Spotipy(config=spoti_modify_config, token_id='modify')
    logger.info("Transferring music to device id: %s" % target_device_id)
    spot_modify.play_on_device(target_device_id=target_device_id,
                               session_info=spot_read.get_playback_info())
    logger.info("Music Transferred!")
Exemplo n.º 2
0
def main():
    """
    Handles the core flow of SpotiClick.

    :Example:
    python main.py -c confs/raspotify_conf.yml
                   -l logs/spoticlick.log
    """

    # Initializing
    args = _argparser()
    _setup_log(args.log, args.debug)
    # Load the configuration
    configuration = Configuration(config_src=args.config_file)
    # Init Spotipy
    spoti_read_config = configuration.get_spotifies()[0]
    spoti_modify_config = configuration.get_spotifies()[1]
    spot_read = Spotipy(config=spoti_read_config, token_id='read')
    spot_modify = Spotipy(config=spoti_modify_config, token_id='modify')
    logger.debug("%s volume by 5%%.." % ("Increasing" if args.volume_direction
                                         == 'increase' else "Decreasing"))
    spot_modify.volume_update(direction=args.volume_direction,
                              current_volume=spot_read.get_current_volume())
    logger.debug("Volume changed!")
Exemplo n.º 3
0
def main():
    """
    Handles the core flow of SpotiClick.

    :Example:
    python main.py -m skip_first_press
                   -c confs/raspotify_conf.yml
                   -l logs/spoticlick.log
    """

    # Initializing
    args = _argparser()
    _setup_log(args.log, args.debug)
    logger.info("Starting in run mode: {0}".format(args.run_mode))
    # Load the configuration
    configuration = Configuration(config_src=args.config_file)
    # Get Switchbot config
    switch_conf = configuration.get_switchbots()[0]
    # Init Spotipy
    spoti_config = configuration.get_spotifies()[0]
    target_device = spoti_config["target_device"]
    spot = Spotipy(config=spoti_config, token_id='read')

    # Start the main loop
    target_device_was_active = False
    skip = (args.run_mode == 'skip_first_press')
    error_sleep_time = 1
    while True:
        try:
            target_device_active = spot.is_target_device_active()
            if target_device_active != target_device_was_active:
                if skip:
                    skip = False
                else:
                    logger.info(
                        "%s is now %s music." %
                        (target_device,
                         "playing" if target_device_active else "not playing"))
                    # Call the Switchbot script to click the button.
                    os.popen("python2 %s %s Press" % (switch_conf['src_path'], switch_conf['mac_address']), 'w') \
                        .write('')
                    logger.info("Switchbot clicked the button!")
                target_device_was_active = target_device_active
            error_sleep_time = 1
        except SpotifyException as e:
            error_sleep_time *= 10
            logger.warning(
                "Token expired.\n\tSpotifyException: %s \n\tSleeping for %s seconds..\n\tRefreshing.."
                % (e, error_sleep_time))
            spot.refresh_token()
        except requests.exceptions.ReadTimeout as e:
            error_sleep_time *= 10
            logger.warning(
                "Read Timeout: %s \n\tSleeping for %s seconds..\n\tRetrying.."
                % (e, error_sleep_time))
        except requests.exceptions.ConnectionError as e:
            error_sleep_time *= 10
            logger.warning(
                "Connection Error: %s \n\tSleeping for %s seconds..\n\tRetrying.."
                % (e, error_sleep_time))
        except http.client.RemoteDisconnected as e:
            error_sleep_time *= 10
            logger.warning(
                "Remote Disconnected: %s \n\tSleeping for %s seconds..\n\tRetrying.."
                % (e, error_sleep_time))
        sleep(2)