def main(args): ''' Main method ''' if len(args) < 3: print( "Please provide the necessary parameters ie kexp.py [playlist_name] [start_date] [end_date] [playlist_description]" ) else: #The name of the playlist you want to use in Spotify #If this playlist does not exist a new one with this name will be created #If this playlist exists it will be used playlist_name = args[0] #The start date time of the tracks you want to return. #The KEXP API is in UTC format so make this date must be in the UTC format and timezone #Example: 2019-02-15T02:00:00Z start_date = args[1] #The end date time of the tracks you want to return. #The KEXP API is in UTC format so make this date must be in the UTC format and timezone #Example: 2019-02-15T05:00:00Z end_date = args[2] #The description of the playlist you want to appear in Spotify playlist_description = args[3] #Create new Playlist object #Set this particular playlist properties #Send the playlist object into Spotify to create/update the latest playlist = Playlist() spotify = Spotify() playlist.name = playlist_name playlist.description = playlist_description temp_tracks = [] uri = f'https://api.kexp.org/v2/plays/?airdate_after={start_date}&airdate_before={end_date}&album=&album_exact=&artist=&artist_exact=&exclude_airbreaks=&has_comment=&host_ids=&label=&label_exact=&limit=2000&ordering=airdate&recording_id=&show_ids=&song=&song_exact=' temp_tracks = get_tracks(uri, start_date, end_date) for temp_track in temp_tracks: if not any(x.airdate == temp_track['airdate'] for x in playlist.tracks): track = Track() track.artist = temp_track['artist'] track.title = temp_track['song'] track.airdate = temp_track['airdate'] playlist.tracks.append(track) playlist.tracks.sort(key=extract_time, reverse=False) spotify.create_playlist(playlist)
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 main(args): ''' Main method ''' if len(args) < 4: print( "Please provide the necessary parameters ie thecurrent.py [playlist_name] [start_date] [end_date] [playlist_description]" ) else: #The name of the playlist you want to use in Spotify #If this playlist does not exist a new one with this name will be created #If this playlist exists it will be used playlist_name = args[0] #The start date time of the tracks you want to return. #Example: 2019-02-15T02:00:00 start_date = args[1] #The end date time of the tracks you want to return. #Example: 2019-02-15T05:00:00 end_date = args[2] #The description of the playlist you want to appear in Spotify playlist_description = args[3] #Create new Playlist object #Set this particular playlist properties #Send the playlist object into Spotify to create/update the latest playlist = Playlist() spotify = Spotify() playlist.name = playlist_name playlist.description = playlist_description start = datetime.datetime.strptime(start_date, '%Y-%m-%dT%H:%M:%S') end = datetime.datetime.strptime(end_date, '%Y-%m-%dT%H:%M:%S') playlist.tracks = get_tracks(start, end) playlist.tracks.sort(key=extract_time, reverse=False) spotify.create_playlist(playlist)
def main(): sp = Spotify() yt = Youtube() yt_playlist_id = input("Enter youtube playlist id: ") spotify_playlist_name = input("Enter a name for your spotify playlist: ") songs = yt.get_songs_from_playlist(yt_playlist_id) spotify_playlist_id = sp.create_playlist(spotify_playlist_name) for song in songs: song_uri = sp.get_song_uri(song.artist, song.title) was_added = sp.add_song_to_playlist(song_uri, spotify_playlist_id) if was_added: print(f'{song.artist} - {song.title} was added to playlist.')
def main(): playlist_url = input("Youtube playlist url: ") playlist_name = input("Playlist name: ") yt = Youtube(playlist_url) song_titles = yt.get_songs_title() print(len(song_titles)) songs_info = yt.get_songs_info(song_titles) print(len(songs_info)) spotify = Spotify() playlst_id = spotify.create_playlist(playlist_name) for song_name, artist in songs_info.items(): uri = spotify.get_spotify_uri(artist, song_name) status = spotify.add_songs_to_playlist(playlst_id, {"uris": [uri]}) if status: print(f"{artist}-{song_name} was added to playlist.") else: print(f"\nERROR!! {artist}-{song_name} could not be added.\n")
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 main(): sp = Spotify() yt = Youtube() yt_playlist_id = input("Enter youtube playlist id: ") spotify_playlist_name = input("Enter a name for your spotify playlist: ") spotify_playlist_id = sp.create_playlist(spotify_playlist_name) songs = yt.get_songs_from_playlist(yt_playlist_id) for song in songs: song_uri = sp.get_song_uri(song.artist, song.title) if not song_uri: print(f"{song.artist} - {song.title} was not found!") continue was_added = sp.add_song_to_playlist(song_uri, spotify_playlist_id) if was_added: print(f'{song.artist} - {song.title} was added to playlist.') total_songs_added = sp._num_playlist_songs(spotify_playlist_id) print(f'Added {total_songs_added} songs out of {len(songs)}')
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! --------" )
#!/usr/bin/env python from top100 import Top100 from spotify import Spotify date_in = input("Which year do you want to travel to? " "Type the date in this format: YYYY-MM-DD: ") song_list = Top100(date_in) spotify = Spotify() spotify.create_playlist( name=f"{date_in} Billboard Top 100ish", description="100DaysofCode created playlist", song_list=song_list.top_100, )
from billboard import BillBoard from spotify import Spotify if __name__ == '__main__': date = input('Which date do you want to travel to? (date format YYYY-MM-DD): ') bb = BillBoard(date) bb.request_website() bb.get_data() songs = bb.parse_data() sp = Spotify() sp.search_songs(songs) sp.create_playlist(date) sp.add_songs_to_playlist()
def main(args): ''' Main method ''' if len(args) < 4: print ("Please provide the necessary parameters ie kexp.py [playlist_name] [start_date] [end_date] [playlist_description]") else: #The name of the playlist you want to use in Spotify #If this playlist does not exist a new one with this name will be created #If this playlist exists it will be used playlist_name = args[0] #The start date time of the tracks you want to return. #The KEXP API is in UTC format so make this date must be in the UTC format and timezone #Example: 2019-02-15T02:00:00Z start_date = args[1] #The end date time of the tracks you want to return. #The KEXP API is in UTC format so make this date must be in the UTC format and timezone #Example: 2019-02-15T05:00:00Z end_date = args[2] #The description of the playlist you want to appear in Spotify playlist_description = args[3] days_to_add = 0 if len(args) > 4: days_to_add = args[4] #Create new Playlist object #Set this particular playlist properties #Send the playlist object into Spotify to create/update the latest playlist = Playlist() spotify = Spotify() playlist.name = playlist_name playlist.description = playlist_description start = datetime.datetime.strptime(start_date, '%Y-%m-%dT%H:%M:%SZ') end = datetime.datetime.strptime(end_date, '%Y-%m-%dT%H:%M:%SZ') temp_tracks = [] if days_to_add > 0: days_count = 0 while days_count <= days_to_add: #Walk back for each day day_start = start + timedelta(days=-days_count) day_end = end + timedelta(days=-days_count) #Go to the end date and then come back #This is a terrible method but I have not figured out how the KEXP API really works yet uri = 'https://legacy-api.kexp.org/play/?limit=200&end_time=' + day_end.strftime("%Y-%m-%dT%H:%M:%SZ") + '&ordering=-airdate' temp_tracks.extend(get_tracks(uri, day_start, day_end)) days_count += 1 else: #Go to the end date and then come back #This is a terrible method but I have not figured out how the KEXP API really works yet uri = 'https://legacy-api.kexp.org/play/?limit=200&end_time=' + end.strftime("%Y-%m-%dT%H:%M:%SZ") + '&ordering=-airdate' temp_tracks = get_tracks(uri, start, end) for temp_track in temp_tracks: if not any(x.airdate == temp_track['airdate'] for x in playlist.tracks): track = Track() track.artist = temp_track['artist'] track.title = temp_track['song'] track.airdate = temp_track['airdate'] playlist.tracks.append(track) playlist.tracks.sort(key=extract_time, reverse=False) spotify.create_playlist(playlist)