-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_playlist.py
More file actions
105 lines (83 loc) · 2.82 KB
/
Copy pathcreate_playlist.py
File metadata and controls
105 lines (83 loc) · 2.82 KB
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
import json
import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
import requests
import youtube_dl
from exceptions import ResponseException
from secrets import spotify_token, spotify_user_id
class CreatePlaylist:
def __init__(self):
#self.youtube_client = self.get_youtube_client()
#self.all_song_info = {}
client = "mitch"
def create_playlist(self):
request_body = json.dumps({
"name": "Apple Music",
"description": "All liked Youtube videos",
"public": True
})
query = "https://api.spotify.com/v1/users/{}/playlists".format(
spotify_user_id)
response = requests.post(
query,
data=request_body,
headers={
"Content-Type": "application/json",
"Authorization": "Bearer {}".format(spotify_token)
}
)
response_json = response.json()
return response_json["id"]
def get_spotify_uri(self, song_name, artist):
query = "https://api.spotify.com/v1/search?query=track%3A{}+artist%3A{}&type=track&offset=0&limit=20".format(
song_name,
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"]
print(uri)
return uri
def add_song_to_playlist2(self):
# Add all liked songs into a new Spotify playlist
# populate dictionary with our liked songs
# collect all of uri
song_name = "Gipsy"
artist = "Moksi"
spotify_URI = self.get_spotify_uri(song_name, artist)
# create a new playlist
playlist_id = self.create_playlist()
print(playlist_id)
# add all songs into new playlis
request_data = json.dumps(spotify_URI)
print(request_data)
query = "https://api.spotify.com/v1/playlists/{}/tracks".format(
playlist_id)
response = requests.post(
query,
data=request_data,
headers={
"Content-Type": "application/json",
"Authorization": "Bearer {}".format(spotify_token)
}
)
print(response)
# check for valid response status
if response.status_code != 200:
raise ResponseException(response.status_code)
response_json = response.json()
return response_json
if __name__ == '__main__':
cp = CreatePlaylist()
#cp.get_spotify_uri("Gipsy", "Moksi")
cp.add_song_to_playlist2()