Skip to content

Commit

Permalink
changed port for Spotify api
Browse files Browse the repository at this point in the history
  • Loading branch information
GurungAman committed Jul 21, 2021
1 parent 0eace46 commit 57536d3
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
client_id = "71bc96707b89403ab7e79eced1e1886e"
playlist_url = "https://open.spotify.com/playlist/1owo7Arl3OQCQCA8z6v5O5"
playlist_url = "https://open.spotify.com/playlist/4PLHO6uSgtw9GiUSYBMfYN"
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.env
client_secret.json
client_secret.json
__pycache__
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.linting.enabled": true
}
4 changes: 2 additions & 2 deletions SpotifyAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class SpotifyApi:
def __init__(self) -> None:
self.client_id = config("client_id")
self.base_url = "https://accounts.spotify.com"
self.redirect_uri = "http://127.0.0.1:8888/"
self.redirect_uri = "http://127.0.0.1:8000/"
self.spotify_api= f"https://api.spotify.com/v1/playlists"

def create_code_verifier_challenge(self):
Expand Down Expand Up @@ -71,7 +71,7 @@ def get_track_names_from_playlist_items(self):
artists = [artist['name'] for artist in playlist_item['track']['artists']]
for artist in artists:
track_name = playlist_item['track']['name'] + " " + artist
print(f"Fetching {playlist_item['track']['name']} from spotify.")
print(f"Fetching {playlist_item['track']['name']} from spotify.")
track_names.append(track_name)
return track_names

Expand Down
71 changes: 35 additions & 36 deletions YoutubeAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,68 +6,67 @@ class YoutubeApi:
def __init__(self) -> None:
self.base_url = "https://www.googleapis.com/youtube/v3"
flow = InstalledAppFlow.from_client_secrets_file(
'client_secret.json',
scopes=['https://www.googleapis.com/auth/youtube'])
"client_secret.json", scopes=["https://www.googleapis.com/auth/youtube"]
)

flow.run_local_server(host="127.0.0.1", port=8888, prompt="consent", authorization_prompt_message="")
flow.run_local_server(
host="127.0.0.1",
port=8888,
prompt="consent",
authorization_prompt_message="",
)

credentials = flow.credentials
self.youtube = build("youtube", "v3", credentials=credentials)

def create_playlist(self, playlist_name):
make_playlist = self.youtube.playlists().insert(
part="snippet, status",
body={
"snippet": {
"title": playlist_name,
},
"status": {
"privacyStatus": "public"
}
}
)
},
"status": {"privacyStatus": "public"},
},
)
response = make_playlist.execute()
self.playlist_id = response['id']
self.playlist_id = response["id"]
print(f"Playlist {playlist_name} created.")
return True


def get_tracks_from_youtube(self, tracks):
# searches fot youtube video which has title similar to
# searches for youtube video which has title similar to
# spotify track and returns the most relevant search result
self.video_ids = []
self.videos = []
for track in tracks:
search_request = self.youtube.search().list(
part="snippet",
maxResults=1,
order="relevance",
q=track
part="snippet", maxResults=1, order="relevance", q=track
)
response = search_request.execute()
search_result = response['items'][0]
video_id = search_result['id']['videoId']
self.title = search_result['snippets']['title']
self.video_ids.append(video_id)
search_result = response["items"][0]
video_id = search_result["id"]["videoId"]
self.title = search_result["snippet"]["title"]

self.videos.append({
'id': video_id,
'title': self.title
})
return True

def add_track_to_playlist(self):
video_ids = self.video_ids
for video_id in video_ids:
videos = self.videos
playlist_id = self.playlist_id
for video in videos:
add_video = self.youtube.playlistItems().insert(
part="snippet",
body={
"snippet": {
"playlistId": "PLyE5TKnn6IC0iJH-zYD1Af0Da6zxH1Y0t",
"resourceId": {
"kind": "youtube#video",
"videoId": video_id
}
}
"playlistId": playlist_id,
"resourceId": {"kind": "youtube#video", "videoId": video['id']},
}
)
print(f"Adding {self.title} to playlist.")
response = add_video.execute()
},
)
print(f"Adding {video['title']} to playlist.")
add_video.execute()

return True


6 changes: 3 additions & 3 deletions client_secret.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"web": {
"client_id": "",
"project_id": "",
"client_secret": "",
"client_id": "284940628116-qj89pirmdnbnoqrtdcbrb9a5nfbht0i7.apps.googleusercontent.com",
"project_id": "spotify-to-youtube-320214",
"client_secret": "GyO2cFX-x4VXLJ8-KkHPmtnX",
"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",
Expand Down
4 changes: 4 additions & 0 deletions playlist-converter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from requests.api import patch
from SpotifyAPI import SpotifyApi
from YoutubeAPI import YoutubeApi

Expand All @@ -7,11 +8,14 @@
spotify.open_authorize_url()
auth_redirect_uri = input("Copy and paste the url you were redirected to here.\n")
spotify.perform_authorization(auth_redirect_uri=auth_redirect_uri)
print("\n")
spotify_tracks = spotify.get_track_names_from_playlist_items()
playlist_name = spotify.get_playlist_name()


youtube = YoutubeApi()
youtube.create_playlist(playlist_name=playlist_name)
print("\n")
youtube.get_tracks_from_youtube(tracks=spotify_tracks)
youtube.add_track_to_playlist()

0 comments on commit 57536d3

Please sign in to comment.