Skip to content

Latest commit

 

History

History
313 lines (239 loc) · 11.6 KB

README.md

File metadata and controls

313 lines (239 loc) · 11.6 KB

SpotiClick

GitHub license

Table of Contents

About

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

Real case: 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.

demo

The app consists of 3 parts:

  • 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

If the project is setup correctly the flow is the following:

  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

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

  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

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

All the installation steps are being handled by the Makefile.

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

$ make install

If you executed the previous command, you can skip through to the Running locally section.

Check the available make commands

$ 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

$ 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

$ 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

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

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

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

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 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)

Example Run:

spoticlick_run -m skip_first_press -c confs/raspotify_conf.yml -l logs/raspotify.log

Deployment

The deployment is being done to a device that has bluetooth and is in close proximity with the Switchbot.

  • In this device you should install tmux
  • Clone the project and create a .env file that contains your environmental variables
  • Create a crontab by executing $ crontab -e and add a line like the one in crontab.txt
  • Edit spoticlick_default.sh appropriately and give it execution rights

Don't forget to set the above-mentioned environmental variables.

TODO

Read the TODO to see the current task list.

Built With

License

This project is licensed under the GNU License - see the LICENSE file for details.

Acknowledgments