-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtify.py
162 lines (125 loc) · 5 KB
/
youtify.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
import spotipy
import spotipy.util as util
import httplib2
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import argparser
from oauth2client import tools
import requests
REDIRECT_URI = "http://localhost"
GOOGLEAPI_KEY = "" # Your Google API Key
CLIENT_SECRETS_FILE = "client_secret.json" # Path to the client_secret JSON file for Google API
MISSING_CLIENT_SECRETS_MESSAGE = "MISSING client_secret.json FILE"
YOUTUBE_SCOPE = "https://www.googleapis.com/auth/youtube"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
CLIENT_ID = "" # Your Spotify App Client ID
CLIENT_SECRET = "" # Your Spotify App Client Secret
scope = 'user-library-read playlist-read-private playlist-read-collaborative'
username = input("\nYour spotify username: ")
def Show_Songs(playlist_dict):
c = 0
for item in playlist_dict["items"]:
try:
song_name = str(item["track"]["name"]) + " "
c += 1
except:
return c
for artist in item["track"]["artists"]:
try:
song_name += str(artist["name"]) + " "
except:
return c
print(song_name)
return c
def Get_SongId_From_Youtube(song_name):
url = "https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q=" + song_name.replace(" " , "+") + "&key=" + GOOGLEAPI_KEY
res = requests.get(url)
data = res.json()
try:
song_id = (data['items'][0]['id']['videoId'])
except:
return None
return song_id
def Process_Songs(playlist_dict, youtube, playlist_id):
for item in playlist_dict["items"]:
song_id = ""
try:
song_name = str(item["track"]["name"]) + " "
except:
return
for artist in item["track"]["artists"]:
try:
song_name += str(artist["name"]) + " "
except:
return
song_id = Get_SongId_From_Youtube(song_name)
if song_id != None:
Add_to_Playlist(youtube, song_id, playlist_id)
def get_authenticated_service():
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_SCOPE,
message=MISSING_CLIENT_SECRETS_MESSAGE)
storage = Storage("oauth2.json")
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = tools.run_flow(flow, storage, tools.argparser.parse_args())
return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
http=credentials.authorize(httplib2.Http()))
def Add_to_Playlist(youtube,videoID,playlistID):
add_video_request=youtube.playlistItems().insert(
part="snippet",
body={
'snippet': {
'playlistId': playlistID,
'resourceId': {
'kind': 'youtube#video',
'videoId': videoID
}
}
}
).execute()
def Get_SongsInPlaylist(username, playlist_id):
c = 0
playlist_dict = sp.user_playlist(user=username, playlist_id=playlist_id, fields = ["tracks","next"])
tracks = playlist_dict["tracks"]
c += Show_Songs(tracks)
while(tracks["next"]):
tracks = sp.next(tracks)
c += Show_Songs(tracks)
return c
def Process_SongsInPlaylist(username, playlist_id, youtube_id):
youtube = get_authenticated_service()
playlist_dict = sp.user_playlist(user=username, playlist_id=playlist_id, fields = ["tracks","next"])
tracks = playlist_dict["tracks"]
Process_Songs(tracks, youtube,youtube_id)
while(tracks["next"]):
tracks = sp.next(tracks)
Process_Songs(tracks, youtube, youtube_id)
token = util.prompt_for_user_token(username, scope, client_id=CLIENT_ID , client_secret=CLIENT_SECRET, redirect_uri=REDIRECT_URI)
if token:
sp = spotipy.Spotify(auth=token)
print("Looking for available playlists...\n")
results = sp.user_playlists(username)
print("Found these playlists: \n")
for elem in results["items"]:
print(elem['name'])
playlist_id = ""
playlist_name = input("\nWhat playlist should i copy? \n\n>>")
print("OK...")
for elem in results["items"]:
if elem["name"].find(playlist_name) != -1:
try:
playlist_id = elem["id"]
except:
break
print("\nLooking for songs in the playlist...")
count = Get_SongsInPlaylist(username, playlist_id)
print("\n\nFound " + str(count) + " songs!\n")
SONGREQUEST_PLAYLIST_ID = input("Insert the Youtube Playlist ID, which you can find in the Youtube URL of the Playlist\n\n>>")
print("\nI'm processing the songs, this may take a while...\n")
Process_SongsInPlaylist(username, playlist_id, SONGREQUEST_PLAYLIST_ID)
print("\nEnded!\n")
else:
print("Can't get token for " + username)