├── .gitignore ├── .vscode └── settings.json ├── LICENSE ├── README.md ├── icon.png ├── main.py ├── playlists.json ├── requirements.txt ├── spotify_client.py ├── sync_manager.py ├── tune2tube.png └── youtube_client.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Spyder project settings 132 | .spyderproject 133 | .spyproject 134 | 135 | # Rope project settings 136 | .ropeproject 137 | 138 | # mkdocs documentation 139 | /site 140 | 141 | # mypy 142 | .mypy_cache/ 143 | .dmypy.json 144 | dmypy.json 145 | 146 | # Pyre type checker 147 | .pyre/ 148 | 149 | # pytype static type analyzer 150 | .pytype/ 151 | 152 | # Cython debug symbols 153 | cython_debug/ 154 | 155 | # PyCharm 156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 158 | # and can be added to the global gitignore or merged into this file. For a more nuclear 159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 160 | #.idea/ 161 | 162 | client_secret.json 163 | credentials.json -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "python.analysis.typeCheckingMode": "off" 3 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 YOGESHWARAN R 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

Spot2Tube

2 | 3 |

Convert Spotify Playlist to YouTube Playlist

4 | 5 | ![image](https://github.com/Rexadev/spotify-playlist-to-youtube-playlist/assets/62152714/79e02f56-9405-4b69-84e1-9b73b190f6cf) 6 | 7 |

8 | GitHub stars 9 | 10 | GitHub forks 11 | 12 | GitHub license 13 | 14 | 15 | Code style 16 | 17 | GitHub Repo size 18 |

19 | 20 | ## About 21 | 22 | Python script to convert Spotify playlist to YouTube playlist. 23 | 24 | ## Features 25 | 26 | 1. Convert Spotify playlist to YouTube Playlist 27 | 2. Sync YouTube Playliset with Spotify playlist 28 | Sync Multiple Playlists 29 | 30 | ## Setup 31 | 32 | ### Requirements 33 | 34 | 1. Install Python (https://www.python.org/) 35 | 2. Install all required package (After cloning the repo, inside the folder) 36 | 37 | ```bash 38 | pip install -r requirements.txt 39 | ``` 40 | 41 | ### YouTube 42 | 43 | 1. Go to the Google Cloud Console, sign in with your Google account, and create a new project. 44 | 2. Once your project is created, select it from the project dropdown menu in the top navigation bar. 45 | 3. Go to the Credentials page in the API & Services section of the left sidebar. 46 | 4. Click on the "Create Credentials" button and select "OAuth client ID". 47 | 5. After creating select edit button in the OAuth 2.0 Client IDs, Select application type as Desktop App and then click create. 48 | 6. Click the download button to download the credentials in your project directory. Rename the file to `client_secret.json` 49 | 7. Go to the OAuth consent screen in the API & Services section of the left sidebar. Under test user add your Gmail id. 50 | 51 | ### Spotify 52 | 53 | 1. Go to the Spotify Developer Dashboard and log in with your Spotify account. 54 | 2. Click on the "Create an App" button and fill out the necessary information, such as the name and description of your application. 55 | 3. Once you've created the app, you'll be taken to the app dashboard. Here, you'll find your client ID and client secret, which are used to authenticate your application with the Spotify API. 56 | 4. Add you client id and secert in `.env` file 57 | 58 | ```env 59 | CLIENT_ID="xxxxxxxxxxxxxxxxxx" 60 | CLIENT_SECRET="xxxxxxxxxxxxxxxx" 61 | ``` 62 | ## Usage 63 | 64 | #### Quick Guide 65 | 66 | - [Create a new YouTube playlist from Spotify playlist](#create-a-youtube-playlist-from-spotify-playlist) 67 | - [Sync YouTube playlist with spotify playlist](#sync-your-youtube-playlist-with-your-spotify-playlist) 68 | - [Sync multiple playlists](#sync-multiple-playlist) 69 | 70 | ### Format 71 | 72 | ```txt 73 | python main.py create [OPTIONS] SPOTIFY_PLAYLIST_ID 74 | 75 | ``` 76 | Browser will open for authorization. Sign into google account. 77 | 78 | ``` 79 | Options: 80 | 81 | --public Create a public playlist 82 | --private Create a public playlist 83 | -n, --name TEXT Name of the YouTube playlist to be created 84 | -d, --description TEXT Description of the playlist 85 | -l, --only-link just only link of playlist, logs not appear 86 | -s, --save-to-sync Save to list of playlist to sync 87 | --help Show this message and exit. Refer sync-multiple-playlist section in ReadMe 88 | ``` 89 | 90 | #### Examples 91 | 92 | Create YouTube Playlist from Spotify Playlist 93 | 94 | ```bash 95 | python main.py create SPOTIFY_PLAYLIST_ID 96 | ``` 97 | 98 | - Create public YouTube playlist with custom Playlist Title and Description: 99 | 100 | ```bash 101 | python main.py create --public -n "My Playlist" -d "A collection of my favorite songs" SPOTIFY_PLAYLIST_ID 102 | ``` 103 | 104 | - Create private YouTube playlist and save it to the list of playlists to sync: 105 | 106 | ```bash 107 | python main.py create --private -s SPOTIFY_PLAYLIST_ID 108 | ``` 109 | 110 | - Get only the link of the YouTube playlist without displaying logs: 111 | 112 | ## Sync YouTube Playlist with Spotify playlist 113 | 114 | ### Usage 115 | 116 | ```txt 117 | main.py sync [OPTIONS] 118 | ``` 119 | 120 | ``` 121 | Options: 122 | 123 | -s, --spotify_playlist_id TEXT Spotify playlist ID 124 | -y, --youtube_playlist_id TEXT YouTube playlist ID 125 | -l, --only-link just only link of playlist, logs not appear 126 | --help Show this message and exit. 127 | ``` 128 | 129 | ```bash 130 | python main.py sync -s -y 131 | ``` 132 | 133 | - `-s`, `--spotify_playlist_id`: Specifies the Spotify playlist ID to sync. 134 | - `-y`, `--youtube_playlist_id`: Specifies the YouTube playlist ID to sync. 135 | - `-l`, `--only-link`: Retrieves only the link of the playlist without displaying logs. 136 | 137 | It also open browser for authorization. Sign into google account to sync playlist. 138 | 139 | Sync Spotify playlist with YouTube playlist and retrieve only the link: 140 | 141 | ```bash 142 | python main.py sync -s SPOTIFY_PLAYLIST_ID -y YOUTUBE_PLAYLIST_ID --only-link 143 | ``` 144 | 145 | ## Sync Multiple playlist 146 | 147 | When creating a playlist, just add `--save-to-sync` or `-s` flag to it. It save the Spotify and YouTube playlist id in [playlists.json](https://github.com/yogeshwaran01/spotify-playlist-to-youtube-playlist/blob/master/playlists.json) file. 148 | 149 | Sync all playlists in that file - 150 | 151 | ```bash 152 | python main.py sync 153 | ``` 154 | Clear file - 155 | 156 | ```bash 157 | python main.py clear 158 | ``` 159 | 160 | ### How this works 161 | 162 | Refer [this](https://dev.to/yogeshwaran01/from-spotify-to-youtube-how-i-built-a-python-script-to-convert-playlists-2h89) blog post for more info 163 | 164 | ### Contact Me 165 | 166 | If you need more info or any support please feel free to contact [me](mailto:yogeshin247@gmail.com) 167 | 168 | ### Contributing 169 | 170 | Contributions are welcome! If you have any ideas, suggestions, or bug reports, please open an issue or submit a pull request. 171 | 172 | ### License 173 | 174 | This project is licensed under the MIT License. See the [LICENSE](https://github.com/yogeshwaran01/spotify-playlist-to-youtube-playlist/blob/master/LICENSE) file for details. 175 | -------------------------------------------------------------------------------- /icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yogeshwaran01/spotify-playlist-to-youtube-playlist/23bf0b00fbb2dd95121b6d654a53e16606bac86d/icon.png -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | import json 3 | import time 4 | 5 | import click 6 | 7 | from spotify_client import SpotifyClient 8 | from sync_manager import SyncManager 9 | from youtube_client import YouTubeClient 10 | 11 | manager = SyncManager() 12 | 13 | 14 | @click.group() 15 | def cli(): 16 | """ 17 | Tune2Tube \n 18 | From Spotify's Groove to YouTube's Show: Seamless Conversion! \n 19 | GitHub: https://github.com/yogeshwaran01/spotify-playlist-to-youtube-playlist 20 | """ 21 | 22 | 23 | @click.command() 24 | @click.argument("spotify_playlist_id") 25 | @click.option("--public", is_flag=True, help="Create a public playlist") 26 | @click.option("--private", is_flag=True, help="Create a public playlist") 27 | @click.option("--name", "-n", help="Name of the YouTube playlist to be created") 28 | @click.option("--description", "-d", help="Description of the playlist") 29 | @click.option( 30 | "--only-link", 31 | "-l", 32 | default=False, 33 | help="just only link of playlist, logs not appear", 34 | is_flag=True, 35 | ) 36 | @click.option( 37 | "--save-to-sync", "-s", is_flag=True, help="Save to list of playlist to sync" 38 | ) 39 | def create( 40 | spotify_playlist_id: str, 41 | public: bool, 42 | private: bool, 43 | name: str, 44 | description: str, 45 | only_link: bool, 46 | save_to_sync: bool, 47 | ): 48 | """Create a YouTube Playlist from Spotify Playlist""" 49 | 50 | spotify = SpotifyClient() 51 | youtube = YouTubeClient() 52 | 53 | # Get Songlist from Spotify 54 | 55 | spotify_playlist = spotify.get_playlist(spotify_playlist_id) 56 | 57 | if public: 58 | privacy_status = "public" 59 | elif private: 60 | privacy_status = "private" 61 | else: 62 | privacy_status = "private" 63 | 64 | # Generate YouTube Playlist 65 | 66 | if name and description: 67 | youtube_playlist_id = youtube.create_playlist( 68 | name, 69 | description, 70 | privacy_status=privacy_status, 71 | )["id"] 72 | elif description: 73 | youtube_playlist_id = youtube.create_playlist( 74 | spotify_playlist.name, 75 | description, 76 | privacy_status=privacy_status, 77 | )["id"] 78 | elif name: 79 | youtube_playlist_id = youtube.create_playlist( 80 | name, 81 | spotify_playlist.description, 82 | privacy_status=privacy_status, 83 | )["id"] 84 | else: 85 | youtube_playlist_id = youtube.create_playlist( 86 | spotify_playlist.name, 87 | spotify_playlist.description, 88 | privacy_status=privacy_status, 89 | )["id"] 90 | 91 | # SearchSongonYouTUbe 92 | 93 | for track in spotify_playlist.tracks: 94 | if not only_link: 95 | click.secho(f"Searching for {track}", fg="blue") 96 | video = youtube.search_video(track) 97 | if not only_link: 98 | click.secho(f"Song found: {video.title}", fg="green") 99 | youtube.add_song_playlist(youtube_playlist_id, video.video_id) 100 | if not only_link: 101 | click.secho("Song added", fg="green") 102 | time.sleep(1) 103 | 104 | if not only_link: 105 | click.secho(f"Playlist {privacy_status} playlist created", fg="blue") 106 | click.secho( 107 | f"Playlist found at https://www.youtube.com/playlist?list={youtube_playlist_id}", fg="blue" 108 | ) 109 | click.secho(f"Playlist ID: {youtube_playlist_id}", fg="blue") 110 | else: 111 | click.secho(f"https://www.youtube.com/playlist?list={youtube_playlist_id}", fg="blue") 112 | 113 | if save_to_sync: 114 | manager.add_playlist(spotify_playlist_id, youtube_playlist_id, spotify_playlist.name, name, f"https://open.spotify.com/playlist/{spotify_playlist_id}", f"https://www.youtube.com/playlist?list={youtube_playlist_id}") 115 | manager.commit() 116 | 117 | 118 | @click.command() 119 | @click.option("-s", "--spotify_playlist_id", help="Spotify playlist ID") 120 | @click.option("-y", "--youtube_playlist_id", help="YouTube playlist ID") 121 | @click.option( 122 | "--only-link", 123 | "-l", 124 | default=False, 125 | help="just only link of playlist, logs not appear", 126 | is_flag=True, 127 | ) 128 | def sync( 129 | spotify_playlist_id: str, 130 | youtube_playlist_id: str, 131 | only_link: bool, 132 | ): 133 | """Sync your YouTube playlist with Spotify Playlist""" 134 | 135 | playlists_to_be_synced = [] 136 | 137 | if spotify_playlist_id is None and youtube_playlist_id is None: 138 | click.secho("Syncing Playlists ..", fg="blue") 139 | playlists_to_be_synced = manager.playlists_to_be_synced 140 | 141 | else: 142 | playlists_to_be_synced.append( 143 | { 144 | "spotify_playlist_id": spotify_playlist_id, 145 | "youtube_playlist_id": youtube_playlist_id, 146 | } 147 | ) 148 | 149 | spotify = SpotifyClient() 150 | youtube = YouTubeClient() 151 | 152 | # Sync between Spotify to YouTUbe 153 | 154 | for playlist in playlists_to_be_synced: 155 | if not only_link: 156 | click.secho( 157 | f"Syncing YouTube: {playlist['youtube_playlist_id']} to Spotify: {playlist['spotify_playlist_id']}", fg="blue" 158 | ) 159 | 160 | spotify_playlist = spotify.get_playlist(playlist["spotify_playlist_id"]) 161 | youtube_playlist = youtube.get_playlist(playlist["youtube_playlist_id"]) 162 | youtube_playlist_ids = list( 163 | map( 164 | lambda x: { 165 | "id": x["snippet"]["resourceId"]["videoId"], 166 | "item": x["id"], 167 | }, 168 | youtube_playlist, 169 | ) 170 | ) 171 | yt_p_ids = list( 172 | map(lambda x: x["snippet"]["resourceId"]["videoId"], youtube_playlist) 173 | ) 174 | 175 | searched_playlist = [] 176 | for track in spotify_playlist.tracks: 177 | video = youtube.search_video(track) 178 | searched_playlist.append(video.video_id) 179 | 180 | songs_to_be_added = [] 181 | songs_to_be_removed = [] 182 | 183 | for song in youtube_playlist_ids: 184 | if song["id"] not in searched_playlist: 185 | songs_to_be_removed.append(song["item"]) 186 | 187 | for song in searched_playlist: 188 | if song not in yt_p_ids: 189 | songs_to_be_added.append(song) 190 | 191 | if not only_link: 192 | click.secho("Adding songs ...", fg="green") 193 | with click.progressbar(songs_to_be_added) as bar: 194 | for song in bar: 195 | youtube.add_song_playlist(playlist["youtube_playlist_id"], song) 196 | 197 | if not only_link: 198 | click.secho("Removing songs ...", fg="green") 199 | with click.progressbar(songs_to_be_removed) as bar: 200 | for song in bar: 201 | youtube.remove_song_playlist(playlist["youtube_playlist_id"], song) 202 | 203 | t = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") 204 | 205 | if not only_link: 206 | click.secho(f"Spotify playlist {spotify_playlist.name} Synced on {t}", fg="blue") 207 | click.secho( 208 | f"Playlist found at https://www.youtube.com/playlist?list={playlist['youtube_playlist_id']}", fg="blue" 209 | ) 210 | else: 211 | click.secho( 212 | f"https://www.youtube.com/playlist?list={playlist['youtube_playlist_id']}", fg="blue" 213 | ) 214 | 215 | # Clear playlists 216 | 217 | @click.command() 218 | def clear(): 219 | """Clear all the playlists thats to be synced""" 220 | with open(manager.playlist_file, "w") as file: 221 | json.dump([], file) 222 | 223 | click.secho("cleared", fg="blue") 224 | 225 | 226 | cli.add_command(create) 227 | cli.add_command(sync) 228 | cli.add_command(clear) 229 | 230 | if __name__ == "__main__": 231 | cli() 232 | -------------------------------------------------------------------------------- /playlists.json: -------------------------------------------------------------------------------- 1 | [] -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | async-timeout==4.0.2 2 | beautifulsoup4==4.12.2 3 | cachetools==5.3.0 4 | certifi==2022.12.7 5 | charset-normalizer==3.1.0 6 | click==8.1.3 7 | google-api-core==2.11.0 8 | google-api-python-client==2.86.0 9 | google-auth==2.17.3 10 | google-auth-httplib2==0.1.0 11 | google-auth-oauthlib==1.0.0 12 | googleapis-common-protos==1.59.0 13 | httplib2==0.22.0 14 | idna==3.4 15 | oauthlib==3.2.2 16 | protobuf==4.22.3 17 | pyasn1==0.5.0 18 | pyasn1-modules==0.3.0 19 | pyparsing==3.0.9 20 | python-dotenv==1.0.0 21 | pytube==12.1.3 22 | redis==4.5.4 23 | requests==2.28.2 24 | requests-oauthlib==1.3.1 25 | rsa==4.9 26 | six==1.16.0 27 | soupsieve==2.4.1 28 | spotipy==2.23.0 29 | uritemplate==4.1.1 30 | urllib3==1.26.15 31 | -------------------------------------------------------------------------------- /spotify_client.py: -------------------------------------------------------------------------------- 1 | import os 2 | from dataclasses import dataclass 3 | 4 | import spotipy 5 | from spotipy.oauth2 import SpotifyClientCredentials 6 | import dotenv 7 | 8 | dotenv.load_dotenv() 9 | 10 | 11 | @dataclass 12 | class Playlist: 13 | name: str 14 | description: str 15 | tracks: list[str] 16 | 17 | 18 | class SpotifyClient: 19 | def __init__(self) -> None: 20 | client_id = os.getenv("CLIENT_ID") 21 | client_secret = os.getenv("CLIENT_SECRET") 22 | auth_manager = SpotifyClientCredentials( 23 | client_id=client_id, client_secret=client_secret 24 | ) 25 | self.spotify = spotipy.Spotify(auth_manager=auth_manager) 26 | 27 | def get_playlist(self, id: str): 28 | playlist = self.spotify.playlist(id) 29 | queries = [] 30 | tracks = playlist["tracks"]["items"] 31 | for track in tracks: 32 | track_name = track["track"]["name"] 33 | artists = ", ".join( 34 | [artist["name"] for artist in track["track"]["artists"]] 35 | ) 36 | queries.append(f"{track_name} by {artists}") 37 | return Playlist(playlist["name"], playlist["description"], queries) 38 | -------------------------------------------------------------------------------- /sync_manager.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | 4 | class SyncManager: 5 | playlist_file = "playlists.json" 6 | 7 | def __init__(self) -> None: 8 | with open(self.playlist_file, "r") as file: 9 | self.playlists_to_be_synced: list = json.load(file) 10 | 11 | def add_playlist(self, spotify_playlist_id: str, youtube_playlist_id: str, spotify_name: str, youtube_name: str, spotify_link, youtube_link: str): 12 | load = { 13 | "spotify_playlist_id": spotify_playlist_id, 14 | "youtube_playlist_id": youtube_playlist_id, 15 | "spotify_name": spotify_name, 16 | "youtube_name": youtube_name, 17 | "spotify_link": spotify_link, 18 | "youtube_link": youtube_link 19 | } 20 | 21 | self.playlists_to_be_synced.append(load) 22 | 23 | def commit(self): 24 | with open(self.playlist_file, "w") as file: 25 | json.dump(self.playlists_to_be_synced, file, indent=2) 26 | -------------------------------------------------------------------------------- /tune2tube.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yogeshwaran01/spotify-playlist-to-youtube-playlist/23bf0b00fbb2dd95121b6d654a53e16606bac86d/tune2tube.png -------------------------------------------------------------------------------- /youtube_client.py: -------------------------------------------------------------------------------- 1 | import os 2 | import json 3 | 4 | from pytube import Search 5 | from pytube.contrib.search import logger 6 | 7 | from google_auth_oauthlib.flow import InstalledAppFlow 8 | from google.oauth2.credentials import Credentials 9 | from googleapiclient.discovery import build 10 | 11 | logger.disabled = True 12 | 13 | 14 | class YouTubeClient: 15 | def __init__(self): 16 | if os.path.exists("credentials.json"): 17 | self.load_credentials() 18 | else: 19 | self.authenticate_and_build() 20 | 21 | def authenticate_and_build(self): 22 | flow = InstalledAppFlow.from_client_secrets_file( 23 | "client_secret.json", 24 | scopes=["https://www.googleapis.com/auth/youtube.force-ssl"], 25 | ) 26 | 27 | creds = flow.run_local_server() 28 | 29 | self.save_credentials(creds) 30 | self.youtube = build("youtube", "v3", credentials=creds) 31 | 32 | def save_credentials(self, creds): 33 | credentials_data = creds.to_json() 34 | with open("credentials.json", "w") as credentials_file: 35 | json.dump(credentials_data, credentials_file) 36 | 37 | def load_credentials(self): 38 | with open("credentials.json", "r") as credentials_file: 39 | credentials_data = json.load(credentials_file) 40 | creds = Credentials.from_authorized_user_info(json.loads(credentials_data)) 41 | self.youtube = build("youtube", "v3", credentials=creds) 42 | def create_playlist( 43 | self, name: str, description: str, privacy_status: str = "private" 44 | ): 45 | playlist = ( 46 | self.youtube.playlists() 47 | .insert( 48 | part="snippet,status", 49 | body={ 50 | "snippet": { 51 | "title": name, 52 | "description": description, 53 | "defaultLanguage": "en", 54 | }, 55 | "status": {"privacyStatus": privacy_status}, 56 | }, 57 | ) 58 | .execute() 59 | ) 60 | 61 | return playlist 62 | 63 | def add_song_playlist(self, playlist_id: str, video_id: str): 64 | request = self.youtube.playlistItems().insert( 65 | part="snippet", 66 | body={ 67 | "snippet": { 68 | "playlistId": playlist_id, 69 | "resourceId": {"kind": "youtube#video", "videoId": video_id}, 70 | } 71 | }, 72 | ) 73 | playlist_item = request.execute() 74 | return playlist_item 75 | 76 | def remove_song_playlist(self, playlist_id: str, video_id: str): 77 | request = self.youtube.playlistItems().delete(id=video_id) 78 | response = request.execute() 79 | return response 80 | 81 | def search_video(self, query: str): 82 | return Search(query).results[0] 83 | 84 | def get_playlist(self, playlist_id): 85 | videos = [] 86 | next_page_token = None 87 | 88 | while True: 89 | request = self.youtube.playlistItems().list( 90 | part="snippet", 91 | playlistId=playlist_id, 92 | maxResults=50, 93 | pageToken=next_page_token, 94 | ) 95 | 96 | response = request.execute() 97 | 98 | videos.extend(response["items"]) 99 | next_page_token = response.get("nextPageToken") 100 | 101 | if not next_page_token: 102 | break 103 | 104 | return videos 105 | --------------------------------------------------------------------------------