This is a Python script that uses Firefox browser with YouTube and Spotify to automatically search for and play YouTube videos based on the currently playing song on Spotify.
- The script opens a Firefox browser.
- It installs four addons for Firefox.
- The script loads a configuration file into the YouTube Enhancer addon.
- It retrieves the name and artist of the currently playing song on Spotify.
- The script searches YouTube for the song.
- It retrieves the first result and opens the URL in the browser.
- Steps 3-5 are repeated if the song on Spotify changes.
Below is an example of how the script looks when running:
To set up the script, follow these steps:
- Go to https://developer.spotify.com/dashboard/.
- Log in to your Spotify account.
- Click the "Create an App" button.
- Give the app a name and description, click "I understand", and then click "Create".
- Click "Edit Settings".
- Put http://localhost:8888 in the redirect URL field.
- Click "Save".
- Copy your client id and put it in the first line of secret.txt.
- Click show secret copy your client secret and put it in the second line of secret.txt.
- Open your editor and run
pip3 install -r requirements.txt
. - Run the script.
Since the original version, the script has undergone several changes to improve its functionality:
- Added a configuration file.
- Sped up the code by not waiting for the play button to appear.
- Made the Spotify keys stored in a text file.
- Added an age restriction bypass extension.
- Added a YouTube enhancer configuration so quality is set to 4K or closest automatically.
- so quality is set to 4k or closest automatically
- Added auto fullscreen into the browser.
- Made the scope only what is required and removed extra testing code.
- Made the fullscreen feature less clunky and slow.
- Made the song search quicker and in the background so it appears faster.
- Made the script try to set the volume to 0% on Spotify to avoid hearing it in two places.
- Made it so the script controls Spotify and not the other way around, i.e., the playback length is based on the YouTube video, not the Spotify one.
- Made the finding of elements more accurate by using CSS selectors instead of XPaths.
While this script provides convenient automation for playing YouTube videos based on the currently playing song on Spotify, there are a few known issues to keep in mind:
- The script does not work if the song on Spotify is not found on YouTube.
- Sometimes, the script may select the wrong video due to the wrong video appearing first in the search results.
- The script cannot mute phone audio on Spotify.
- The script can be too fast for some older computers. In such cases, adding a few sleep statements in the code may help to slow down the script and avoid any issues.
While the current version of the script provides a useful set of features, there are some plans to improve it further in the future:
- Implement multi-threading to make the script even faster and more efficient.
- Create a web-based GUI that would allow users to log in and control the script from a remote device.
- Improve the search algorithm to make it more accurate, potentially by using keywords to weigh search results and improve the chances of selecting the correct video.