-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconvert_playlist.py
182 lines (151 loc) · 5.86 KB
/
convert_playlist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# Spotify to YouTube Playlist Converter
# Example usage:
# --------------
# - Get your Spotify OAuth Token > paste into secrets.py
# - python convert_playlist.py spotify:playlist:7kkDlPfIUHynUuLvjKNwIZ
# - Authenticate with Google
import os
import requests
import argparse
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
from secrets import spotify_token, spotify_user_id
class ConvertPlaylist():
def __init__(self):
self.youtube = self.get_youtube_client()
self.playlist = {'tracks': []}
# sarch for song in spotify
def get_spotify_song(self, track):
query = "https://api.spotify.com/v1/search?query=track%3A{}+artist%3A{}&type=track&offset=0&limit=20".format(
track['name'],
track['artist']
)
response = requests.get(
query,
headers={
"Content-Type": "application/json",
"Authorization": "Bearer {}".format(spotify_token)
}
)
response_json = response.json()
songs = response_json["tracks"]["items"]
# only use the first song
uri = songs[0]["uri"]
return uri
# get spotify playlist
def get_spotify_playlist(self, spotify_uri):
*prefix, playlist_id = spotify_uri.split(':')
query = "https://api.spotify.com/v1/playlists/{}".format(playlist_id)
response = requests.get(
query,
headers={
"Content-Type": "application/json",
"Authorization": "Bearer {}".format(spotify_token)
}
)
response_json = response.json()
# set title, description, tracks (with name and artist)
self.playlist['title'] = response_json['name']
self.playlist['description'] = response_json['description']
items = response_json['tracks']['items']
for item in items:
track_name = item['track']['name']
main_artist = item['track']['artists'][0]['name']
track = {'name': track_name,
'artist': main_artist}
self.playlist['tracks'].append(track)
# create spotify playlist
def create_spotify_playlist(self, title, description=""):
pass
# add song to a playlist
def add_song_to_spotify_playlist(self):
pass
# Log into Youtube
def get_youtube_client(self):
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
client_secrets_file = "client_secret.json"
# Get credentials and create an API client
scopes = ["https://www.googleapis.com/auth/youtube.force-ssl"]
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes)
credentials = flow.run_console()
youtube = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
return youtube
# search for song in youtube
def get_youtube_video(self, track):
query = "{} - {}".format(track['artist'], track['name'])
# get response
request = self.youtube.search().list(
part="snippet",
q=query
)
response = request.execute()
# get video id (and title)
for item in response['items']:
if item['id']['kind'] == 'youtube#video':
video_title = item['snippet']['title']
video_id = item['id']['videoId']
print(video_title)
# print("https://www.youtube.com/watch?v={}".format(video_id))
break
return video_id
# get youtube playlist
def get_youtube_playlist(self):
# does sth. with self.playlist
pass
# create youtube playlist
def create_youtube_playlist(self, title, description=""):
request = self.youtube.playlists().insert(
part="snippet",
body={
"snippet": {
"title": title,
"description": description
}
}
)
response = request.execute()
playlist_id = response['id']
playlist_title = response['snippet']['localized']['title']
print('-' * 30)
print(playlist_title)
print("https://www.youtube.com/playlist?list={}".format(playlist_id))
print('-' * 30)
return playlist_id
# add video to a playlist
def add_video_to_youtube_playlist(self, video_id, playlist_id):
request = self.youtube.playlistItems().insert(
part="snippet",
body={
'snippet': {
'playlistId': playlist_id,
'resourceId': {
'kind': 'youtube#video',
'videoId': video_id
}
}
}
)
request.execute()
def convert(self, spotify_uri):
# get spotify playlist
self.get_spotify_playlist(spotify_uri)
# create youtube playlist
playlist_id = self.create_youtube_playlist(self.playlist['title'], self.playlist['description'])
for track in self.playlist['tracks']:
# search video on youtube [artist - song name]
video_id = self.get_youtube_video(track)
# add video to youtube playlist
self.add_video_to_youtube_playlist(video_id, playlist_id)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Convert a Spotify playlist to a YouTube playlist')
parser.add_argument('spotify_uri', type=str, help='Spotify Playlist > ... > Share > Copy Spotify URI')
args = parser.parse_args()
cp = ConvertPlaylist()
cp.convert(args.spotify_uri)