def copy_playlist(self): spotify = Spotify(self.username) token = spotify.authenticate_spotify() driver = webdriver.Chrome(self.chrome_driver) driver.get(self.playlist_url) html = driver.page_source spotify_uris = [] soup = BeautifulSoup(html, 'html.parser') playlist_name = self.get_soundcloud_playlist_info(soup)[0] playlist_description = self.get_soundcloud_playlist_info(soup)[1] # start our beautiful soup search with the parent element results = soup.find_all( "li", class_="trackList__item sc-border-light-bottom") # traverse through the all the sub elements of our search to find all the song divs in a page then retrieve their links and song data for x in results: div = x.find_all( "div", class_="trackItem g-flex-row sc-type-small sc-type-light") for z in div: final_div = z.find_all("div", class_="trackItem__content sc-truncate") for ref in final_div: href = ref.find( "a", class_= "trackItem__trackTitle sc-link-dark sc-font-light", href=True) track_name = href.text.lower().replace(" ", "+") artist_name = ref.find( "a", class_="trackItem__username sc-link-light").text.lower( ).replace(" ", "+") # if spotify can find a uri for this song, then we append it to our list, else we send it to our dictionary which will download the song instead if spotify.get_spotify_uri(track_name, artist_name, token) is not None: spotify_uris.append( spotify.get_spotify_uri(track_name, artist_name, token)) else: link = "https://soundcloud.com" + href["href"] self.tracks.update({href.text: link}) driver.close() playlist_id = spotify.create_playlist(token, playlist_name, playlist_description) spotify.add_songs_to_playlist(spotify_uris, token, playlist_id) self.download_soundcloud(self.tracks) print( "-------- Succesfully copied your playlist on Soundcloud to Spotify! --------" )
def copy_playlist(self): # build our youtube client to list out the content in the given playlist youtube = googleapiclient.discovery.build( "youtube", "v3", developerKey=os.environ.get("DEVELOPER_KEY")) request = youtube.playlistItems().list( part="snippet", playlistId=self.search_for_playlist(), maxResults=50) response = request.execute() spotify = Spotify(self.username) token = spotify.authenticate_spotify() uris = [] while request is not None: response = request.execute() for item in response["items"]: try: video_id = item["snippet"]["resourceId"]["videoId"] youtube_url = "https://www.youtube.com/watch?v={}".format( video_id) # use youtube_dl to collect the song title and channel title (song name and artist) video = youtube_dl.YoutubeDL({}).extract_info( youtube_url, download=False) song_name = video["title"] artist = video["uploader"].replace(" - Topic", " ") print(song_name + " by " + artist) # if the spotify can find the song, then we add it our list which is sent to our add_playlist function if spotify.get_spotify_uri(song_name, artist, token) is not None: uris.append( spotify.get_spotify_uri(song_name, artist, token)) except: print("------- Video is unavailable -------") # allows us to iterate through all the items in the request request = youtube.playlistItems().list_next(request, response) playlist_name = self.get_playlist_info()[0] playlist_description = self.get_playlist_info()[1] spotify_playlist_id = spotify.create_playlist(token, playlist_name, playlist_description) spotify.add_songs_to_playlist(token, uris, spotify_playlist_id) print( "-------- Succesfully copied from your playlist on YouTube to Spotify! -------" )
def copy_playlist(self): apple_token = self.get_apple_key() apple_playlist_id = self.get_apple_music_id() query = 'https://api.music.apple.com/v1/catalog/{}/playlists/{}'.format( 'us', apple_playlist_id) response = requests.get(query, headers={ "Content-Type": "application/json", "Authorization": "Bearer {}".format(apple_token) }) playlist = response.json() spotify = Spotify(self.username) spotify_token = spotify.authenticate_spotify() uris = [] playlist_description = playlist['data'][0]['attributes'][ 'description']['short'] playlist_name = playlist['data'][0]['attributes']['name'] for i, songs in enumerate( playlist['data'][0]['relationships']['tracks']['data']): song_name = songs['attributes']['name'] artist_name = songs['attributes']['artistName'] if spotify.get_spotify_uri(song_name, artist_name, spotify_token) is not None: uris.append( spotify.get_spotify_uri(song_name, artist_name, spotify_token)) print(str(i) + ".) " + song_name + " by " + artist_name) spotify_paylist_id = spotify.create_playlist(spotify_token, playlist_name, playlist_description) spotify.add_songs_to_playlist(uris, spotify_token, spotify_paylist_id) print( "-------- Succesfully copied your playlist on Apple Music to Spotify! --------" )