Skip to content

CLI written in Python for transferring your playlists bidirectionally between Youtube and Spotify

License

Notifications You must be signed in to change notification settings

brandonpeebles/YoutubeSpotifyMigration

Repository files navigation

Actions Status

YouTubeSpotifyMigration 🎵

Transfer your playlists between YouTube and Spotify

User-friendly command line interface written in Python for migrating playlists between YouTube and Spotify, bidirectionally. Makes use of OAuth 2.0 authentication to connect the program to both Google and Spotify's respective APIs and stores the refresh tokens locally so logging in should only need to be done once. Once authenticated, it can then make API calls to fetch the user's current playlists and match songs across platforms.

Table of Contents

Overview

Walkthrough

  1. Choose a transfer direction: Upon initializing, YouTubeSpotifyMigration will ask whether you want to transfer playlists from YouTube to Spotify or the other way around.

  1. Authentication: It will then attempt to automatically log you in (if it has valid credentials stored from a previous run)

  1. Select playlist to transfer: You will be prompted to select a playlist to transfer songs from.
  2. Automatic song matching: The app will then attempt to match these songs against the other service's library using their APIs. It will flag any it couldn't find a match for. The app parses the YouTube title and first tries to search for songs in the format Artist - Track and will attempt the reverse if no matches are found.

  1. Confirm song matches: You will then be prompted to confirm the songs to transfer. All are selected by default. It may have made a mistake matching, so this is your chance to remove those before continuing. Press space to add/remove a song; press enter to continue.

  1. Add to new or existing playlist: You'll then be asked whether you want to add to an existing playlist or create a new one. If adding, you'll be presented with a list of your playlists. If creating a new playlist, you'll be prompted to provide a name.

  1. App automatically adds songs: The app will then add all of the chosen songs into the playlist and return a URL to it.

Installation

In order to run the app, you'll first need to clone the repository and install the necessary dependencies.

Clone the repo:

git clone https://github.com/peeblesbrandon/YoutubeSpotifyMigration.git

Switch into the application directory:

cd YoutubeSpotifyMigration

Create and enable a virtual environment (recommended):

python3 -m venv env
source env/bin/activate

Install required packages (using pip):

pip install -r requirements.txt

Authentication Setup

The app authenticates its API clients using OAuth 2.0. On the YouTube (Google) side, this is implemented using the google-auth-oauthlib package (docs). On the Spotify side, this has been implemented through a custom built set of classes (Spotify.py and SpotifyCredentials.py) which performs their Authorization Code Flow. With either platform, once we've successfully authenticated and received tokens, we pickle these and store them locally to easily authenticate in the future or refresh with new tokens.

To get started, you'll need to obtain the varous API keys, client IDs, secrets, etc. required for both platforms in order to connect to their APIs.

YouTube

You will need to obtain two main pieces of information, an API key and a client secrets JSON file. To get both of these, follow all of the instructions for Step 1 on Google's documentation here. You can name your project YouTubeSpotifyMigration or anything you'd like really. The application type Other might not be available as the instructions say, so you can choose Other UI, Other non-UI, or installed instead. When creating your OAuth client id, select Desktop app.

OAuth Consent Screen Setup Credentials Screen

When selecting your application's OAuth consent screen, you'll be asked to select scopes. Search for YouTube and select the following:

Once you have all of that information, you can create a file called youtube_client_secret.json in the root directory of your program and populate it with the credentials you receieved. It should have exactly the following format (you can also reference the youtube_client_secret_example.json file as a template too):

{
  "installed": {
    "client_id": "your_client_id",
    "project_id": "your_project_id",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_secret": "your_client_secret",
    "redirect_uris": ["urn:ietf:wg:oauth:2.0:oob", "http://localhost"],
    "YOUR_API_KEY": "your_api_key_that_you_add_yourself_to_this_json_file"
  }
}

Spotify

You will need a similar file for Spotify.

{
    "client_id": "your_client_id",
    "client_secret": "your_client_secret",
    "response_type": "code",
    "redirect_uri": "https://example.com/callback",
    "scope": "user-read-private user-read-email playlist-read-collaborative playlist-modify-public playlist-read-private playlist-modify-private"
}
  1. Create a file called spotify_client_secret.json file in the root directory of your program. Copy and paste the template in the code block above.
  2. Login into the Spotify Developer Dashboard.
  3. Click Create an app and give it a name (e.g. YouTubeSpotifyMigration)
  4. Click show client secret and copy and paste the client ID and secret into your spotify_client_secret.json file in their respective fields.
  5. Click edit settings and enter https://example.com/callback into the Redirect URIs input field. Click add and then click save to close the window.

Usage

  1. To start the app, cd into the root directory of the program using your terminal and execute the run.py file using python run.py.
  2. If it's your first time using the app (and you have the necessary client secrets json files added), then you should be prompted or redirected to visit a link in your browser to authenticate and grant the app permission to access your data.
  3. For YouTube, once you open the link you'll likely not have a verified app so it will warn you it's unsafe. To continue, click Advanced and then you'll see a link to proceed that's labeled (unsafe). It's technically your app and you're only giving yourself access to your data, so there's nothing to worry about.
  4. For Spotify, the app should automatically open your browser and direct you to a Spotify authentication page. Once you login and give permission, it should redirect you to a page that says Example Site. Copy the URL of this site from your browser and paste it back into the terminal.
  5. Assuming the previous two steps were successful, the app will store the credentials on your local machine and use the refresh_tokens to automatically reauthenticate in the future.
  6. At any point, if you need to quit the program and restart, just type: ctrl + c

Built with

  • Python 3
  • YouTube API
  • googleapiclient
  • google-auth-oauthlib
  • Spotify API
  • OAuth 2.0
  • Pickle
  • Requests
  • PyInquirer

Author

Brandon Peebles

About

CLI written in Python for transferring your playlists bidirectionally between Youtube and Spotify

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages