SkillAgentSearch skills...

SpotiClick

An app that clicks a physical button whenever Spotify starts playing on the target device.

Install / Use

/learn @drkostas/SpotiClick
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

SpotiClick

GitHub license

Table of Contents

About <a name = "about"></a>

<b>An app that clicks a physical button whenever Spotify starts playing on a target device.</b>

<b>Real case</b>: <i>I have Raspberry Pi connected to a Stereo Receiver and a Spotify Client installed. The Stereo Receiver has a physical button that needs to be pressed in order to power on. I want this button to be pressed whenever music starts or stops playing on this device.</i>

demo

<b>The app consists of 3 parts</b>:

  • A spotify client installed on a raspberry pi 3 (or any device with spotify) capable of playing music to a stereo speaker device (which needs to be switched on/off by pressing a button)
  • A switchbot device that can be triggered (via bluetooth) to click a button
  • The spotify API that is capable of checking on which device the music is playing for a user

<b>If the project is setup correctly the flow is the following</b>:

  1. The user opens Spotify on any device and from the spotify connect menu, he selects the target device (for me is a raspberry client - raspotify)
  2. The main.py which is checking (through Spotify API) every few seconds which device spotify is playing music on, is notified that music is now playing on the target device.
  3. It calls the switchbot.py with the correct arguments which triggers the Switchbot to press the power-on button of the stereo device.
  4. Whenever music stops playing on the target device, the same flow will cause the button to be pressed again and the stereo to get switched off.

Getting Started <a name = "getting_started"></a>

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Prerequisites <a name = "prerequisites"></a>

  1. First of all you should have a Premium Spotify account.

  2. You need to have a machine (tested on Ubuntu 18.04) with:

    • Both Python 2.7 and Python> 3.6 and any Bash based shell (e.g. zsh) installed

      $ sudo apt-get install -y python2-dev python3-dev 
      
      $ python3 -V
      Python 3.6.9
      
      python2 -V
      Python 2.7.17
      
      $ echo $SHELL
      /usr/bin/zsh
      
    • Bluetooth enabled and the following libraries installed:

      sudo apt-get install -y python-pexpect libusb-dev libdbus-1-dev libglib2.0-dev libudev-dev libical-dev libreadline-dev
      
  3. You will also need to setup Spotify API (Reference 1, Reference 2)

  4. You should own a Switchbot clicker.

  5. (Optional) If you want to use a Raspberry Pi as your target Spotify client, you need to do the following:

    • Ensure your Raspberry Pi has bluetooth
    • Install the raspotify client on your Raspberry (also connect to your Spotify account)
    • Connect your Raspberry to your speakers

Set the required environment variables <a name = "env_variables"></a>

Before running the main.py you will need to set the following environmental variables in an .env file that you should load before running it:

SPOTIFY_USERNAME=<Value>
SPOTIFY_CLIENT_ID=<Value>
SPOTIFY_SECRET=<Value>
SWITCHBOT_MAC=<Value>

Installing, Building <a name = "installing"></a>

All the installation steps are being handled by the Makefile.

<i>If you don't want to go through the setup steps and finish the installation at once, execute the following command:</i>

$ make install

<i>If you executed the previous command, you can skip through to the Running locally section.</i>

Check the available make commands <a name = "check_make_commamnds"></a>

$ make help

-----------------------------------------------------------------------------------------------------------
                                              DISPLAYING HELP                                              
-----------------------------------------------------------------------------------------------------------
make delete_venv
       Delete the current venv
make create_venv
       Create a new venv for the specified python version
make requirements
       Upgrade pip and install the requirements
make setup
       Call setup.py install
make clean_pyc
       Clean all the pyc files
make clean_build
       Clean all the build folders
make clean
       Call delete_venv clean_pyc clean_build
make install
       Call clean create_venv requirements setup
make help
       Display this message
-----------------------------------------------------------------------------------------------------------

Clean any previous builds <a name = "clean_previous"></a>

$ make clean
make delete_venv
make[1]: Entering directory '/home/drkostas/Projects/SpotiClick'
Deleting venv..
rm -rf venv
make[1]: Leaving directory '/home/drkostas/Projects/SpotiClick'
make clean_pyc
make[1]: Entering directory '/home/drkostas/Projects/SpotiClick'
Cleaning pyc files..
find . -name '*.pyc' -delete
find . -name '*.pyo' -delete
find . -name '*~' -delete
make[1]: Leaving directory '/home/drkostas/Projects/SpotiClick'
make clean_build
make[1]: Entering directory '/home/drkostas/Projects/SpotiClick'
Cleaning build directories..
rm --force --recursive build/
rm --force --recursive dist/
rm --force --recursive *.egg-info
make[1]: Leaving directory '/home/drkostas/Projects/SpotiClick'

Create a new venv and install the requirements <a name = "venv_requirements"></a>

$ make create_venv
Creating venv..
python3.6 -m venv ./venv

$ make requirements
Upgrading pip..
venv/bin/pip install --upgrade pip wheel setuptools
Collecting pip
.................

Build the project locally <a name = "build_locally"></a>

To build the project locally using the setup.py command, execute the following command:

$ make setup
venv/bin/python setup.py install
running install
.................

Running the code locally <a name = "run_locally"></a>

In order to run the code now, you will only need to change the yml file if you need to and run either the main or the created console script.

Modifying the Configuration <a name = "configuration"></a>

There is an already configured yml file under confs/template_conf.yml with the following structure:

spotify:
  - config:  # Spotify API credentials
      username: !ENV ${SPOTIFY_USERNAME}
      client_id: !ENV ${SPOTIFY_CLIENT_ID}
      client_secret: !ENV ${SPOTIFY_SECRET}
      scope: user-read-playback-state  # leave it as is
      redirect_uri: 'http://localhost:8081'  # leave it as is
      target_device: raspotify  # name of target device
    type: spotipy_lib  # leave it as is
switchbot:
  - config:
      src_path: ./Switchbot/switchbot.py  # leave it as is
      mac_address: !ENV ${SWITCHBOT_MAC} # MAC address of Switchbot (you can view it from the android app)
    type: spotipy_folder  # leave it as is

The !ENV flag indicates that a environmental value follows. You can change the values/environmental var names as you wish. If a yaml variable name is changed/added/deleted, the corresponding changes should be reflected on the Configuration class and the yml_schema.json too.

Execution Options <a name = "execution_options"></a>

First, make sure you are in the created virtual environment:

$ source venv/bin/activate
(venv) 
~/drkostas/Projects/SpotiClick  dev 

$ which python
~/drkostas/Projects/SpotiClick/venv/bin/python
(venv) 

Now, in order to run the code you can either call the main.py directly, or the spoticlick_run console script.

To print the help message:

$ python main.py --help
usage: main.py -m {press_on_start,skip_first_press} -c CONFIG_FILE [-l LOG]
               [-d] [-h]

A software designed to click a button when music starts playing.

Required Arguments:
  -m {press_on_start,skip_first_press}, --run-mode {press_on_start,skip_first_press}
                        Whether to press button when starting.
  -c CONFIG_FILE, --config-file CONFIG_FILE
                        The configuration yml file
  -l LOG, --log LOG     Name of the output log file

Optional Arguments:
  -d, --debug           Enables the debug log messages
  -h, --help            Show this help message and exit
(venv) 


# Or

$ SpotiClick --help
usage: main.py -m {press_on_start,skip_first_press} -c CONFIG_FILE [-l LOG]
               [-d] [-h]

A software designed to click a button when music starts playing.

Required Arguments:
  -m {press_on_start,skip_first_press}, --run-mode {press_on_start,skip_first_press}
                        Whether to press button when starting.
  -c CONFIG_FILE, --config-file CONFIG_FILE
                        The config

Related Skills

View on GitHub
GitHub Stars33
CategoryDevelopment
Updated9mo ago
Forks0

Languages

Python

Security Score

87/100

Audited on Jun 19, 2025

No findings