-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api to create youtube playlist and add videos to it
- Loading branch information
1 parent
6c77dc4
commit 0eace46
Showing
6 changed files
with
149 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
.env | ||
.env | ||
client_secret.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from google_auth_oauthlib.flow import InstalledAppFlow | ||
from googleapiclient.discovery import build | ||
|
||
|
||
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']) | ||
|
||
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" | ||
} | ||
} | ||
) | ||
response = make_playlist.execute() | ||
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 | ||
# spotify track and returns the most relevant search result | ||
self.video_ids = [] | ||
for track in tracks: | ||
search_request = self.youtube.search().list( | ||
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) | ||
return True | ||
|
||
def add_track_to_playlist(self): | ||
video_ids = self.video_ids | ||
for video_id in video_ids: | ||
add_video = self.youtube.playlistItems().insert( | ||
part="snippet", | ||
body={ | ||
"snippet": { | ||
"playlistId": "PLyE5TKnn6IC0iJH-zYD1Af0Da6zxH1Y0t", | ||
"resourceId": { | ||
"kind": "youtube#video", | ||
"videoId": video_id | ||
} | ||
} | ||
} | ||
) | ||
print(f"Adding {self.title} to playlist.") | ||
response = add_video.execute() | ||
|
||
return True | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"web": { | ||
"client_id": "", | ||
"project_id": "", | ||
"client_secret": "", | ||
"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", | ||
"redirect_uris": [ | ||
"http://127.0.0.1:8888/" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from SpotifyAPI import SpotifyApi | ||
from YoutubeAPI import YoutubeApi | ||
|
||
if __name__ == "__main__": | ||
spotify = SpotifyApi() | ||
|
||
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) | ||
spotify_tracks = spotify.get_track_names_from_playlist_items() | ||
playlist_name = spotify.get_playlist_name() | ||
|
||
youtube = YoutubeApi() | ||
youtube.create_playlist(playlist_name=playlist_name) | ||
youtube.get_tracks_from_youtube(tracks=spotify_tracks) | ||
youtube.add_track_to_playlist() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,25 @@ | ||
cachetools==4.2.2 | ||
certifi==2021.5.30 | ||
charset-normalizer==2.0.3 | ||
google-api-core==1.31.0 | ||
google-api-python-client==2.13.0 | ||
google-auth==1.33.0 | ||
google-auth-httplib2==0.1.0 | ||
google-auth-oauthlib==0.4.4 | ||
googleapis-common-protos==1.53.0 | ||
httplib2==0.19.1 | ||
idna==3.2 | ||
oauthlib==3.1.1 | ||
packaging==21.0 | ||
protobuf==3.17.3 | ||
pyasn1==0.4.8 | ||
pyasn1-modules==0.2.8 | ||
pyparsing==2.4.7 | ||
python-decouple==3.4 | ||
pytz==2021.1 | ||
requests==2.26.0 | ||
requests-oauthlib==1.3.0 | ||
rsa==4.7.2 | ||
six==1.16.0 | ||
uritemplate==3.0.1 | ||
urllib3==1.26.6 |