-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathspotifyToYoutube.py
executable file
·77 lines (65 loc) · 3.03 KB
/
spotifyToYoutube.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
#coding: utf-8
# Spotify library.
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
# google library
from ytmusicapi import YTMusic
import requests
import traceback
import functools
class SpotifyToYoutube():
def login_to_google(self, ytmusic_headers):
session = requests.Session()
session.request = functools.partial(session.request, timeout=60)
ytmusic = YTMusic(ytmusic_headers, requests_session=session)
return ytmusic
def add_to_playlist(self, ytmusic, video_name, target_playlist):
search_results = ytmusic.search(video_name, "songs") or ytmusic.search(video_name, "videos")
if len(search_results) > 0:
retries = 3
while retries != 0:
try:
ytmusic.add_playlist_items(target_playlist, [search_results[0]['videoId']])
retries = 0
except Exception as e:
print("An exception occurred:", e)
traceback.print_exc()
retries -= 1
def get_tracks(self, playlist_url, spotify_client_id, spotify_client_secret):
# Creating and authenticating our Spotify app.
client_credentials_manager = SpotifyClientCredentials(spotify_client_id, spotify_client_secret)
spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
track_list = []
# Getting a playlist.
results = spotify.user_playlist_tracks(user="", playlist_id=playlist_url)
tracks = results['items']
while results['next']:
results = spotify.next(results)
tracks.extend(results['items'])
# For each track in the playlist.
for track in tracks:
try:
if(track == None or track["track"] == None):
print(track)
elif(track["track"]["artists"] == None):
print(track["track"])
track_list.append(track["track"]["name"])
# In case there's only one artist.
elif (len(track["track"]["artists"]) == 1):
# We add trackName - artist.
track_list.append(track["track"]["name"] + " - " + track["track"]["artists"][0]["name"])
# In case there's more than one artist.
else:
name_string = ""
# For each artist in the track.
for index, artist in enumerate(track["track"]["artists"]):
name_string += (artist["name"])
# If it isn't the last artist.
if (len(track["track"]["artists"]) - 1 != index):
name_string += ", "
# Adding the track to the list.
track_list.append(track["track"]["name"] + " - " + name_string)
except Exception as e:
print("An exception occurred:", e)
traceback.print_exc()
return track_list