Skip to content

Commit

Permalink
Updating deps and not using flask server for prod
Browse files Browse the repository at this point in the history
  • Loading branch information
pawKer committed Feb 15, 2022
1 parent dc160a9 commit f2c16c2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 15 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ __pycache__
*.pyc
*.ipynb
build
dist
dist
/test_env
gui_main.spec
25 changes: 20 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
# Vidify Web

### Automatically play the video for your currently playing song on Spotify in your browser

![](demo/vidify.gif)

## Running the app

## The plug-and-play way

Since the v1.0 release this is now very simple! You just have to download the executable file from [here](https://github.com/pawKer/Vidify/releases/tag/v1.0) if you're on Windows. On MacOS or Linux you will need to run it via python at the moment. You can find instructions for how to do that below.

## The customizable way

If you prefer, the app also supports using the Spotify and Youtube APIs instead of using the SwSpotify and youtube-dl libraries. However, for these you have to provide your own API keys and run it using python. The advantage of doing this is you get better Youtube video matches and you don't have to have the Spotify app open on the computer where you are running the app.

## Installing the Python version

### Requirements

To download all of the python libraries required run the following command. This project requires Python 3.x.

`pip install -r requirements.txt`
Expand All @@ -23,7 +29,8 @@ You can do that like so:
`pip install git+https://github.com/plamere/spotipy.git --upgrade`

### Configuration
If you're not using the APIs you can skip to the `Running the app` section.

Skip to `Running the app` if you're trying to just run the default app.
To run the app you will need to use your own API keys for Spotify and Youtube. You can find documentation on how to get these on Google.
After you have them, create a file `config.py` with the following content

Expand All @@ -34,24 +41,32 @@ youtube_client_secret="<YOUR-YOUTUBE-CLIENT-SECRET>"
redirect_uri="http://localhost:8888/callback/"
refresh_token="<YOUR-SPOTIFY-REFRESH-TOKEN>"
```

### Refresh token

The Spotify API will not allow access after 1h of continous access so we need to refresh our authorisation. For that we need a refresh token. To obtain that you can use the `python get_refresh_token.py <YOUR-SPOTIFY-USERNAME>` utility script. This will open a window in your browser and you will have to allow access to Spotify. You will then be redirected to an URL which you need to copy and paste back into the script. Your refresh token will then be printed. Grab that and place it in the `config.py` file you created above.

### Running the app

You can run the server by doing `python gui_main.py`. This will start the GUI and a new tab will be opened in your default browser. The main page should be there and you should now be able to play any song on Spotify and get the corresponding video to play.

You can select what services you want to use by passing the following command line arguments `python server.py <SPOTIFY-SERVICE> <YOUTUBE-SERVICE>`.
The possible values are SPOTIFY-SERVICE: `api` or `app` and YOUTUBE-SERVICE: `yt-api` or `yt-dl`. The defaults are `app` and `yt-dl` because these don't require API credentials.

# Raspberry Pi Version (not up-to-date)

I wanted to have this script run on a Pi all the time and play the videos on a monitor but unfortunately it is not very good at playing Youtube videos directly from the website so I had to adapt the script. Instead of running it as a web server, I made a script that uses OMXPlayer and youtube-dl.

It uses youtube-dl to get the youtube video download link and then passes that link to OMXPlayer. The rest of the functionality is the same as above.


# How it works
I use the spotipy library for the Spotify API to get the current playing song on Spotify and then look for the corresponding Youtube video for that song using the Youtube API. An endpoint that retrieves the video id in Flask is checked continously from the front-end in Javascript and the video is autoplayed in a video frame in HTML.

I use the spotipy library for the Spotify API to get the current playing song on Spotify and then look for the corresponding Youtube video for that song using the Youtube API. An endpoint that retrieves the video id in Flask is checked continously from the front-end in Javascript and the video is autoplayed in a video frame in HTML.
When the song is changed on Spotify, the video will automatically change on the webpage.
The newer version does the same thing but without using the APIs and instead making use of the SwSpotify library which reads the current playing status from the Spotify app running on your computer and the youtube-dl library which finds the youtube url for your search.

The song on Spotify and the video aren't synced since sometimes videos are longer than the songs and there is also a short delay in the matching process.

# Package command

`pyinstaller -w -F --add-data "templates;templates" --add-data "static;static" gui_main.py`
39 changes: 31 additions & 8 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
# Automatically generated by https://github.com/damnever/pigar.

Flask == 1.1.2
PyQt5_sip == 12.8.1
SwSpotify == 1.2.1
requests == 2.24.0
spotipy == 2.16.1
youtube_dl == 2020.11.1.1
certifi==2021.10.8
chardet==3.0.4
charset-normalizer==2.0.12
click==8.0.3
colorama==0.4.4
Deprecated==1.2.13
Flask==2.0.3
Flask-Cors==3.0.10
idna==2.10
importlib-metadata==4.11.1
itsdangerous==2.0.1
Jinja2==3.0.3
MarkupSafe==2.0.1
packaging==21.3
pyparsing==3.0.7
PyQt5==5.15.6
PyQt5-Qt5==5.15.2
PyQt5-sip==12.9.1
pywin32==303
redis==4.1.3
requests==2.27.1
six==1.16.0
spotipy==2.19.0
swspotify==1.2.3
typing_extensions==4.1.1
urllib3==1.26.8
waitress==2.0.0
Werkzeug==2.0.3
wrapt==1.13.3
youtube-dl==2021.12.17
zipp==3.7.0
13 changes: 12 additions & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import logging
from utils import Utils
from waitress import serve

# Logic for exposing the templates and static folders to PyInstaller
if getattr(sys, 'frozen', False):
Expand Down Expand Up @@ -53,6 +54,11 @@
previousId = ""
previousTitle = ""

@app.after_request
def set_headers(response):
response.headers["Referrer-Policy"] = 'no-referrer-when-downgrade'
return response

# Endpoint for front end to get current track that will be called from UI client
@app.route('/api/')
def api_get_name():
Expand Down Expand Up @@ -100,4 +106,9 @@ def index():
return render_template('index.html')

if __name__ == '__main__':
app.run(debug=True, port=PORT, host='0.0.0.0', use_reloader=False)
serve(
app,
host='0.0.0.0',
port=PORT,
threads=2
)

0 comments on commit f2c16c2

Please sign in to comment.