This repository has been archived by the owner on May 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspotifyToYoutube.py
83 lines (72 loc) · 3.31 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
78
79
80
81
82
83
# Main script from https://github.com/saulojoab/Spotify-To-Youtube, with modifications to make it work with
# the latest spotify API and to make it run with less calls to the YT API.
def spotyplaylist(message):
# coding: utf-8
import json
# Spotify library.
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
# URL conversions.
import urllib.request
import bs4
# Youtube stuff.
import youtube
# Opening our JSON configuration file (which has our tokens).
with open("musicconfig.json", encoding='utf-8-sig') as json_file:
APIs = json.load(json_file)
api = youtube.API(client_id=APIs["youtube"]["client_id"],
client_secret=APIs["youtube"]["client_secret"],
api_key=APIs["youtube"]["api_key"]);
def getTracks(playlistURL):
# Creating and authenticating our Spotify app.
client_credentials_manager = SpotifyClientCredentials(APIs["spotify"]["client_id"],
APIs["spotify"]["client_secret"])
spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
# Getting a playlist.
results = spotify.user_playlist_tracks(user="", playlist_id=playlistURL)
trackList = [];
# For each track in the playlist.
for i in results["items"]:
# In case there's only one artist.
if (i["track"]["artists"].__len__() == 1):
# We add trackName - artist.
trackList.append(i["track"]["name"] + " - " + i["track"]["artists"][0]["name"])
# In case there's more than one artist.
else:
nameString = "";
# For each artist in the track.
for index, b in enumerate(i["track"]["artists"]):
nameString += (b["name"]);
# If it isn't the last artist.
if (i["track"]["artists"].__len__() - 1 != index):
nameString += ", ";
# Adding the track to the list.
trackList.append(i["track"]["name"] + " - " + nameString);
return trackList;
def searchYoutubeAlternative(songName):
# YouTube will block you if you query too many songs using this search.
# ALSO AGAINST TOS, lol, i dont use this one
textToSearch = songName
query = urllib.parse.quote(textToSearch)
url = "https://www.youtube.com/results?search_query=" + query
response = urllib.request.urlopen(url)
html = response.read()
soup = bs4(html, 'html.parser')
for vid in soup.findAll(attrs={'class': 'yt-uix-tile-link'}):
print('https://www.youtube.com' + vid['href'])
def searchYoutube(songName):
video = api.get('search', q=songName, maxResults=1, type='video', order='relevance');
return ("https://www.youtube.com/watch?v=" + video["items"][0]["id"]["videoId"]);
if __name__ == "spotifyToYoutube":
tracks = getTracks(str(message))
print("Searching songs...")
songs = []
for i in tracks:
songs.append(searchYoutube(i))
print("Search finished!")
print("URL LIST: ")
final = []
for i in songs:
#print(i)
final.append(i)
return (final)