def commonSongsTopSongs(*userids: str) -> list[tuple[str, int]]: commonsongs = userFile['commonsongs'] playlistid = None for playlist in commonsongs: if set(userids) == set(playlist[0]): playlistid = playlist[1] break if playlistid is None: print('Could not find existing playlist. Creating one...') playlistid = commonSongsUsers(*userids) print('Retrieving common songs...') playlist = sp.getPlaylistFromId(playlistid) tracks = getTracksFromItem(playlist) tracks = {i['track']['id']: i['track']['name'] for i in tracks} trackids = [j for i in userids for j in topSongsInPlaylistsSub(i)[0]] trackids = list(filter(None.__ne__, trackids)) count = {i: trackids.count(i) for i in list(tracks.keys())} if None in list(count.keys()): del count[None] result = [(tracks[i], j) for i, j in list(count.items())] sortedResult = sorted(result, key=operator.itemgetter(1), reverse=True) return sortedResult
def convert(playlistString: str) -> None: user = getUserFromString('firiusbob') rposplaylist = sp.getPlaylistFromId('5WYRn0FxSUhVsOQpQQ0xBV') print('Pulling from RPOS') rpostracks = getTracksFromItem(rposplaylist) rposdata = [(track['track']['name'], [artist['name'] for artist in track['track']['artists'] ], track['track']['uri']) for track in rpostracks] print('Finished pulling from RPOS. Searching...') playlist = getPlaylist(getUserPlaylists(user), playlistString) tracks = getTracksFromItem(playlist) uris = [] for track in tracks: if track is None or track['track'] is None: continue name = track['track']['name'] artists = [artist['name'] for artist in track['track']['artists']] convertThread(track, name, artists, rposdata, uris) print('Finished search. Creating playlist') userobj = getAuthUser(user) newplaylist = userobj.createPlaylist('Convert of ' + playlist['name']) print('Finished. Adding to playlist') userobj.addSongsToPlaylist(newplaylist['id'], uris)
def searchRPOS(song: str) -> list[tuple[str, list[str], str]]: song = song.lower() song = song.translate({ord(i): None for i in ':&()'}) song = song.split('feat.', 1)[0] song = song.split('-', 1)[0] playlist = sp.getPlaylistFromId('5WYRn0FxSUhVsOQpQQ0xBV') tracks = getTracksFromItem(playlist) results = [] print('Searching RPOS...') for track in tracks: if track is None or track['track'] is None: continue test = track['track']['name'].lower() test = test.translate({ord(i): None for i in ':&()'}) test = test.split('feat.', 1)[0] test = test.split('-', 1)[0] if fuzz.partial_ratio(song, test) > 98 and fuzz.ratio(song, test) > 45: diff = len(test) - len(song) if diff < 100 and diff >= 0: results.append( (track['track']['name'], [artist['name'] for artist in track['track']['artists'] ], track['track']['id'])) return results
def notinRPOS(playlistString: str) -> list[tuple[str, list[str], str]]: user = getUser('firiusbob') rposplaylist = sp.getPlaylistFromId('5WYRn0FxSUhVsOQpQQ0xBV') print('Pulling from RPOS') rpostracks = getTracksFromItem(rposplaylist) rposids = [track['track']['id'] for track in rpostracks] print('Finished pulling from RPOS. Comparing...') playlist = getPlaylist(getUserPlaylists(user), playlistString) tracks = getTracksFromItem(playlist) results = [ track for track in tracks if track['track']['id'] not in rposids ] return [(track['track']['name'], [artist['name'] for artist in track['track']['artists']], track['track']['id']) for track in results]
def commonSongsUsers(*userids: str) -> str: userids = [i.replace('spotify:user:'******'') for i in userids] if len(userids) < 2: print('You need at least 2 users bruh') return refreshtoken = 'AQAcsVCKBnutCWVRPih8OsU1ScZRFjPTePJNaY0GSehMefJmFdscQlGeGuIoU4fAfZ0rkOrx2SCOW3zVMEt3zKJG0mt2yBgKJwilvgCdoZ-ftJBh6AK1PjNVOPWlbbb6vFs' botuser = sp.getAuthUser(refreshtoken) commonsongs = userFile['commonsongs'] print('Pulling songs...') tracksss = [] executor = ThreadPoolExecutor() futures = [] for userid in userids: futures.append( executor.submit(commonSongsUsersThread, userid, tracksss, executor)) wait(futures) executor.shutdown() print('Finding common songs...') trackuri = [] for trackss in tracksss: trackuri.append([ track['track']['uri'] for tracks in trackss for track in tracks if track['track'] is not None ]) commonuri = set(trackuri[0]) for i in trackuri[1:]: commonuri.intersection_update(i) if None in commonuri: commonuri.remove(None) for playlist in commonsongs: if set(userids) == set(playlist[0]): playlistid = playlist[1] tempplaylist = sp.getPlaylistFromId(playlistid) playlisttracks = getTracksFromItem(tempplaylist) playlisttracksset = {i['track']['uri'] for i in playlisttracks} newtracksset = commonuri - playlisttracksset if newtracksset: print('Adding to playlist...') botuser.addSongsToPlaylist(playlistid, list(newtracksset)) else: print('Playlist already up to date') return playlistid commonuri = [i for i in commonuri if i is not None] names = [getUser(i)['display_name'] for i in userids] name = 'Common Songs between ' + ' and '.join(names) print('Creating playlist...') playlistid = botuser.createPlaylist(name)['id'] commonsongs.append((userids, playlistid)) userFile['commonsongs'] = commonsongs if __name__ == '__main__': with open(sys.path[0] + '/data.json', 'w') as f: json.dump(userFile, f, indent=4, separators=(',', ': ')) else: with open('./data.json', 'w') as f: json.dump(userFile, f, indent=4, separators=(',', ': ')) botuser.addSongsToPlaylist(playlistid, commonuri) return playlistid
def playlistFollowersThread(playlist: dict, followers: list) -> None: playlistdata = sp.getPlaylistFromId(playlist['id']) followers.append( (playlistdata['name'], playlistdata['followers']['total']))