コード例 #1
0
def main():

    if len(sys.argv) > 1:
        in_file = sys.argv[1]
    else:
        in_file = "../input/input.txt"

    token = sptfy.authSpotipy()

    for f in glob.glob("../output/*.txt"):
        os.remove(f)

    config = hlpr.loadFile("../config", "config.csv", True)
    token = sptfy.authSpotipy()

    for line in fileinput.input(in_file):
        track_id = line.split("/")[-1].strip()
        track_info = sptfy.pullSpotifyTrack(track_id, token=token)
        artist_id = track_info['spotify_artist_id']
        artist_name = track_info['artist']
        track_name = track_info['title']
        secondary_artist = track_info['secondary_artist']
        hlpr.sortGenres(artist_name,
                        artist_id,
                        track_name,
                        track_id,
                        secondary_artist,
                        token,
                        makePlaylists=True)
コード例 #2
0
ファイル: popularity.py プロジェクト: xpducks/music-learning
def main():
    if len(sys.argv) > 1:
        in_file = sys.argv[1]
    else:
        in_file = "../input/input.txt"
    track_list = []
    local_tracks = []

    config = loadFile("../config", "config.csv", True)
    token = sptfy.authSpotipy()

    for line in fileinput.input(in_file):
        if "local" in line:
            local_tracks.append(line)
            continue
        track_id = sptfy.stripSpotifyLink(line)
        track_data = sptfy.pullSpotifyTrack(track_id, token = token)
        track_list.append(track_data)
    sorted_tracks = sorted(
        track_list,
        key = lambda x: float(x['popularity']),
        reverse = True
    )
    print "\n"
    for item in sorted_tracks:
        print str(item['popularity']) + " :: " + item['artist'] + \
            " - " + item['title']
    print "\n"
    for item in sorted_tracks:
        print "spotify:track:{}".format(item['spotify_id'].strip())
    for item in local_tracks:
        print item
    print "\n"
コード例 #3
0
def main():

    if len(sys.argv) > 1:
        in_file = sys.argv[1]
    else:
        in_file = "../input/input.txt"

    token = sptfy.authSpotipy()

    for f in glob.glob("../output/*.txt"):
        os.remove(f)

    config = hlpr.loadFile("../config", "config.csv", True)
    token = sptfy.authSpotipy()

    for line in fileinput.input(in_file):
        track_id = line.split("/")[-1].strip()
        track_info = sptfy.pullSpotifyTrack(track_id, token = token)
        artist_id = track_info['spotify_artist_id']
        artist_name = track_info['artist']
        track_name = track_info['title']
        secondary_artist = track_info['secondary_artist']
        hlpr.sortGenres(artist_name, artist_id, track_name, track_id,
                        secondary_artist, token, makePlaylists = True)
コード例 #4
0
ファイル: rate_albums.py プロジェクト: xpducks/music-learning
def main():

    # ## load tracks in playlist
    fives = loadFile("input", "fives.txt")
    fours = loadFile("input", "fours.txt")
    threes = loadFile("input", "threes.txt")
    twos = loadFile("input", "twos.txt")
    ones = loadFile("input", "ones.txt")

    ## load database of song ratings
    db = loadFile("../Databases", "song_ratings_db.csv")
    album_ratings = loadFile("../Databases", "album_ratings_db.csv")

    config = loadFile("../config", "config.csv", True)
    token = sptfy.authSpotipy()

    rating = 0
    for ls in [ones, twos, threes, fours, fives]:
        ## playlists are looped in this order such that if a song is in multiple lists it's rating will end up being the highest one
        rating = rating + 1
        for song in ls:
            track_id, spotify_uri = sptfy.getSpotifyTrackIDs(song)
            if not lookupSongBySpotifyID(track_id, db):
                track = sptfy.pullSpotifyTrack(track_id, token=token)
                db = db.append([{
                    'spotify_id': track_id,
                    'artist': track['artist'],
                    'album': track['album'],
                    'spotify_album_id': track['spotify_album_id'],
                    'song': track['title'],
                    'rating': rating
                }])

    db = db.sort(['artist', 'album', 'rating'])
    saveDataFrame(db, "../Databases", "song_ratings_db.csv")

    for album in pd.unique(zip(db.artist, db.album)):
        artist = album[0]
        album = album[1]
        if not lookupAlbumBySpotifyID(album, album_ratings):
            album_id = pd.unique(db[db.artist == artist][db.album == album]
                                 ['spotify_album_id'])[0]
            album_data = sptfy.pullSpotifyAlbum(album_id, token=token)
            ratings = db[db.artist == artist][db.album ==
                                              album]['rating'].tolist()
            if len(ratings) < 3:
                ## don't make album ratings for singles
                continue
            score = 0
            countNot3 = len(np.where(r != 3))
            countMoreThan2 = len(np.where(r > 2))
            countMoreThan3 = len(np.where(r > 3))
            for r in ratings:
                if r == 5:
                    score = score + 100 * 1.0
                    scoreNot3 = score + 100 * 1.0
                elif r == 4:
                    score = score + 80 * 1.2
                    scoreNot3 = score + 80 * 1.2
                elif r == 3:
                    score = score + 60 * 1.0
                elif r == 2:
                    score = score + 40 * 1.2
                    scoreNot3 = score + 40 * 1.2
                elif r == 1:
                    score = score + 20 * 1.0
                    scoreNot3 = score + 20 * 1.0
            std = np.std(ratings)
            if std == 0.0:
                std = 0.25

            if countNot3 == 0:
                adjMean = 3
            else:
                adjMean = scoreNot3 / countNot3
            prop4or5 = countMoreThan3 / len(r)
            adj1 = (adjMean - 3) * prop4or5
            adj2 = adj1 + countMoreThan2 * 0.03

            score = np.mean(ratings) + adj2

            if prop4or5 == 0:
                adjSD = std * 0.05
            else:
                adjSD = std * prop4or5 / len(r)

            score = score - adjSD

            ## min possible score: (mean of 1-star)
            min1 = 1.0
            ## max possible score: Radiohead "OK Computer"
            max1 = 5.662521
            min2 = -1.0
            max2 = -0.125

            scaledScore = (score - min1) / (max1 - min1)
            ## transform (curves the linear scores to inflate higher scores and reduce lower)
            transformedScore = -1 * (8 ^ (-1 * scaledScore))
            scaledScore = (transformedScore - min2) / (max2 - min2)
            album_score = (round(scaledScore * 1000)) / 1.0

            if album_score > 1000:
                album_score = 1000

            if album_score >= 965:
                album_rating = 5.0
            elif album_score >= 890:
                album_rating = 4.5
            elif album_score >= 750:
                album_rating = 4.0
            elif album_score >= 690:
                album_rating = 3.5
            elif album_score >= 625:
                album_rating = 3.0
            elif album_score >= 420:
                album_rating = 2.5
            elif album_score > 325:
                album_rating = 2.0
            elif album_score > 235:
                album_rating = 1.5
            elif album_score >= 100:
                album_rating = 1.0
            elif album_score < 100:
                album_rating = 0.5
            album_ratings = album_ratings.append([{
                'spotify_album_id': album_id,
                'artist': artist,
                'album': album,
                'year': album_data['year'],
                'album_rating': album_rating,
                'album_score': album_score
            }])

    saveDataFrame(album_ratings, "../Databases", "album_ratings_db.csv")
コード例 #5
0
def main(ags, explicit=False):

    if args.ultimatechart is not None:
        with open(args.ultimatechart, 'r') as f:
            ultimatechart = f.readlines()
    else:
        ultimatechart = None
    if args.billboardchart is not None:
        with open(args.billboardchart, 'r') as f:
            billboardchart = [line.rstrip() for line in f]
    else:
        billboardchart = None

    cleans = []
    explicits = []
    local_tracks = []

    config = loadFile("../config", "config.csv", True)
    token = sptfy.authSpotipy()

    cleans, local_tracks = sptfy.pullSpotifyTracks('../input',
                                                   'cleans.txt',
                                                   token=token)
    explicits, local_tracks = sptfy.pullSpotifyTracks(
        '../input',
        'explicits.txt',
        tracks=explicits,
        local_tracks=local_tracks,
        token=token)

    if explicit:
        # sort explicit list
        sorted_tracks = sorted(explicits,
                               key=lambda x: float(x['popularity']),
                               reverse=True)
    else:
        # sort explicit list
        sorted_tracks = sorted(explicits,
                               key=lambda x: float(x['popularity']),
                               reverse=True)
        # now replace explicits with cleans
        for e in explicits:  # replace explicit track href by the clean version's href
            for c in cleans:
                if e['artist'] in c['artist'] and e['title'][:3] in c[
                        'title'][:3]:  # compare artist name and first 4 characters of track name
                    e['spotify_id'] = c['spotify_id']

    print "\n"
    for item in sorted_tracks:
        print str(item['popularity']
                  ) + " :: " + item['artist'] + " - " + item['title']
    print "\n"
    for item in sorted_tracks:
        print "spotify:track:{}".format(item['spotify_id'].strip())
    for item in local_tracks:
        print item
    print "\n"

    if ultimatechart is not None or billboardchart is not None:
        print "Look into keeping the following songs in Airplay this week...\n"

    if ultimatechart is not None:
        nums = ('01', '02', '03', '04', '05', '06', '07', '08', '09') + tuple(
            str(x) for x in range(10, 101))
        uc = [item for item in ultimatechart if item.startswith(nums)]
        for item in sorted_tracks:
            if item['popularity'] < 75:
                artist = item['artist']
                title = item['title']
                artist = re.sub("&", "and",
                                re.sub(r'([^\s\w]|_)+', '', artist).lower())
                title = re.sub("&", "and",
                               re.sub(r'([^\s\w]|_)+', '', title).lower())
                for u in uc:
                    x = re.sub(r'([^\s\w]|_)+', '', re.sub("&", "and",
                                                           u)).lower()
                    if artist + " " in x:
                        artist_match = True
                    else:
                        artist_match = False
                    if title + " by" in x:
                        title_match = True
                    else:
                        title_match = False
                    xs = [i for i in x.split(" ")]
                    if title_match or (artist_match and any(
                        [True for t in title.split(" ") if t in xs])):
                        print str(
                            item['popularity']
                        ) + " :: " + item['artist'] + " - " + item['title']
                        break
    else:
        print "\nIf you want to compare to Ultimate Chart, please provide a txt version as an arg to --ultimatechart"

    if billboardchart is not None:
        for item in sorted_tracks:
            if item['popularity'] < 75:
                artist = item['artist']
                title = item['title']
                artist = re.sub("&", "and",
                                re.sub(r'([^\s\w]|_)+', '', artist).lower())
                title = re.sub("&", "and",
                               re.sub(r'([^\s\w]|_)+', '', title).lower())
                for u in billboardchart:
                    x = re.sub(r'([^\s\w]|_)+', '', re.sub("&", "and",
                                                           u)).lower()
                    if artist in x:
                        artist_match = True
                    else:
                        artist_match = False
                    if title in x:
                        title_match = True
                    else:
                        title_match = False
                    xs = [i for i in x.split(" ")]
                    if title_match or (artist_match and any(
                        [True for t in title.split(" ") if t in xs])):
                        print str(
                            item['popularity']
                        ) + " :: " + item['artist'] + " - " + item['title']
                        break
    else:
        print "\nIf you want to compare to Billboard Chart, please provide a txt version as an arg to --billboardchart\n"

    print "\n"
コード例 #6
0
def main():

    track_list = hlpr.loadFile("../input", "input.txt")
    desired_len = int(
        input("\nHow many tracks would you like the playlist to be? "))
    num_to_drop = len(track_list) - desired_len
    sort_col1 = input("\nEnter one of the following features to sort on: \
        \nacousticness\ndanceability\nenergy\nvalence\npopularity \
        \nrelease_date\nyear\n\n")
    ascending1 = int(
        input("\nChoose sort order for this feature: \
        \n(0) descending\n(1) ascending\n\n"))
    sort_col2 = input(
        "\nEnter another feature to sort on (blank to sort on one feature only): \
        \nacousticness\ndanceability\nenergy\nvalence\npopularity \
        \nrelease_date\nyear\n\n")
    if sort_col2 != '':
        ascending2 = int(
            input("\nChoose sort order for this feature: \
            \n(0) descending\n(1) ascending\n\n"))
    else:
        sort_col2 = None
        ascending2 = 1

    config = hlpr.loadFile("../config", "config.csv", True)
    token = sptfy.authSpotipy()

    if "popularity" in [sort_col1, sort_col2]:
        db, unfound_tracks = sptfy.pullSpotifyTracks('../input',
                                                     'input.txt',
                                                     token=token)
        df = pd.DataFrame.from_dict(db)
        db, unfound_tracks = hlpr.processInput(input_playlist="input.txt")
        db = db.merge(df[["spotify_id", "popularity"]], on="spotify_id")
    else:
        ## get subset of db based on input.txt
        db, unfound_tracks = hlpr.processInput(input_playlist="input.txt")

    if sort_col1 not in db.columns:
        print("\nSort column '{}' not in database!").format(sort_col1)
        sys.exit()
    if sort_col2 is not None and sort_col2 not in db.columns:
        print("\nSort column '{}' not in database!").format(sort_col2)
        sys.exit()

    sorted_db1 = db.sort_values(by=sort_col1, ascending=ascending1)
    if sort_col2 is not None:
        sorted_db2 = db.sort_values(by=sort_col2, ascending=ascending2)

    lst1 = sorted_db1.index.values
    lst2 = sorted_db2.index.values

    i = len(track_list) - num_to_drop
    remove = np.intersect1d(lst1[i:], lst2[i:])

    while len(remove) < num_to_drop:
        i -= 1
        remove = np.intersect1d(lst1[i:], lst2[i:])

    keep = [i for i in db.index if i not in remove]

    remove_db = db.loc[remove, ]
    keep_tracks = [
        "spotify:track:{}".format(x[1]['spotify_id']).rstrip() \
        if len(x[1]['spotify_id']) < 36 else x[1]['spotify_id'].rstrip() \
        for x in db.loc[keep, ].iterrows()
    ]
    remove_db = remove_db.sort_values(by=[sort_col1, sort_col2],
                                      ascending=[ascending1, ascending2])
    remove_tracks = [
        "spotify:track:{}".format(x[1]['spotify_id']).rstrip() \
        if len(x[1]['spotify_id']) < 36 else x[1]['spotify_id'].rstrip() \
        for x in remove_db.iterrows()
    ]

    print("\nThese songs will be removed:\n")
    print(remove_db[['artist', 'title', sort_col1, sort_col2]])
    print("")
    print("\n".join(remove_tracks))

    print("\n\nThese songs remain:\n")
    print("\n".join(keep_tracks))

    if len(unfound_tracks) > 0:
        print("\nAdd these local tracks wherever you want in the playlist:\n")
        print("\n".join(unfound_tracks))
コード例 #7
0
def main():

    ## which feature to sort on
    sort_col1 = raw_input(
        "\nEnter one of the following features to sort on: \
        \ntitle\nartist\nalbum\nduration\ntempo\ntime_signature\nkey\nmode \
        \nloudness\nacousticness\ndanceability\nenergy\ninstrumentalness \
        \nliveness\nspeechiness\nvalence\npopularity\nrelease_date\nyear\n\n"
    )
    ## ascending or descending (1 = ascending, 0 = descending)
    ascending1 = int(raw_input(
        "\nChoose sort order for this feature: \
        \n(0) descending\n(1) ascending\n\n"
    ))
    while ascending1 != 0 and ascending1 != 1:
        ascending1 = int(raw_input(
            "\nEnter either 0 or 1: \
            \n(0) descending\n(1) ascending\n\n"
        ))

    if sort_col1 in ["album", "artist", "tempo", "time_signature", "key",
                     "mode", "popularity", "release_date", "year"]:
        sort_col2 = raw_input(
            "\nIn case of ties what second feature would you like to sort on: \
            \ntitle\nartist\nalbum\nduration\ntempo\ntime_signature\nkey\nmode \
            \nloudness\nacousticness\ndanceability\nenergy\ninstrumentalness \
            \nliveness\nspeechiness\nvalence\npopularity\nrelease_date\nyear\n\n"
        )
        ## ascending or descending (1 = ascending, 0 = descending)
        ascending2 = int(raw_input(
            "\nChoose sort order for this feature: \
            \n(0) descending\n(1) ascending\n\n"
        ))
        while ascending2 != 0 and ascending2 != 1:
            ascending2 = int(raw_input(
            "\nEnter either 0 or 1: \
            \n(0) descending\n(1) ascending\n\n"
        ))
    else:
        sort_col2 = None
        ascending2 = 1
    print("")

    config = hlpr.loadFile("../config", "config.csv", True)
    token = sptfy.authSpotipy()

    if "popularity" in [sort_col1, sort_col2]:
        db, unfound_tracks = sptfy.pullSpotifyTracks('../input',
                                                     'input.txt',
                                                     token = token)
        df = pd.DataFrame.from_dict(db)
        db, unfound_tracks = hlpr.processInput(input_playlist = "input.txt")
        db = db.merge(df[["spotify_id", "popularity"]], on = "spotify_id")
    else:
        ## get subset of db based on input.txt
        db, unfound_tracks = hlpr.processInput(input_playlist = "input.txt")

    if sort_col1 not in db.columns:
        print("\nSort column '{}' not in database!").format(sort_col1)
        sys.exit()
    if sort_col2 is not None and sort_col2 not in db.columns:
        print("\nSort column '{}' not in database!").format(sort_col2)
        sys.exit()

    if sort_col2 is None:
        sorted_db = db.sort_values(by = sort_col1, ascending = ascending1)
    else:
        sorted_db = db.sort_values(by = [sort_col1, sort_col2],
                                   ascending = [ascending1, ascending2])
    sorted_tracks = [
        "spotify:track:{}".format(x[1]['spotify_id']).rstrip() \
        if len(x[1]['spotify_id']) < 36 else x[1]['spotify_id'].rstrip() \
        for x in sorted_db.iterrows()
    ]

    print("\n")
    print(sorted_db[["artist", "title"]])

    hlpr.writeTextFile(sorted_tracks, "../output", "sorted_playlist.txt")
    print("\n")
    print("\n".join(sorted_tracks))

    if len(unfound_tracks) > 0:
        print("\nAdd these local tracks wherever you want in the playlist:\n")
        print("\n".join(unfound_tracks))
コード例 #8
0
ファイル: helpers.py プロジェクト: jkroening/music-learning
def processInput(terms = False, genres = False, input_playlist = None):
    ## set spotify auth
    config = loadFile("../config", "config.csv", True)
    try:
        token = sptfy.authSpotipy()
    except:
        token = None

    ## load tracks in playlist
    if input_playlist is not None:
        in_tracks = loadFile("../input", input_playlist)
    else:
        in_tracks = loadFile("../input", "input.txt")

    ## load database of metadata
    song_db = loadFile("../Databases", "song_db.csv")
    shutil.copyfile("../Databases/song_db.csv",
                    "../Databases/_Backup/song_db.csv")
    artist_db = loadFile("../Databases", "artist_db.csv")
    shutil.copyfile("../Databases/artist_db.csv",
                    "../Databases/_Backup/artist_db.csv")

    unfound_tracks = []
    for track in in_tracks:
        if 'local' in track:
            artist, title, album = sptfy.formatLocalTrack(track)
            song_uri = sptfy.searchSpotifyTrack(
                artist,
                title,
                album,
                first = True,
                token = token
            )
            if song_uri is not None:
                if not dbm.lookupSongBySpotifyID(song_uri, song_db):
                    song = sptfy.getAudioFeatures(song_uri, token = token)
                    if song is not None:
                        song_db = song_db.append(song, ignore_index = True)
            else:
                print "{} not found.".format(track)
                unfound_tracks.append(track)
        else:
            if not dbm.lookupSongBySpotifyID(track, song_db):
                song = sptfy.getAudioFeatures(track, token = token)
                if song is not None:
                    song_db = song_db.append(song, ignore_index = True)
                else:
                    print "{} not found.".format(track)
                    unfound_tracks.append(track)
    song_db = song_db.drop_duplicates('spotify_id')
    dbm.saveDataFrame(song_db, "../Databases", "song_db.csv")

    ## subset song database on tracks in playlist
    db_subset = dbm.subsetDataFrame(song_db, in_tracks)

    if genres:
        ## build dict of artists with genres
        artist_db = dbm.buildArtistDataFrame(
            db_subset,
            artist_db,
            token = token
        )
        dbm.saveDataFrame(artist_db, "../Databases", "artist_db.csv")

        ## add artist genres to songs subset db
        ## and because capitalization might be different
        ## drop 'artist' from one of the dataframes before merging
        artist_db = artist_db.drop('artist', 1)
        db_out = dbm.addArtistDataToSongs(db_subset, artist_db)
    else:
        db_out = db_subset

    return db_out, unfound_tracks
コード例 #9
0
def processInput(terms=False, genres=False, input_playlist=None):
    ## set spotify auth
    config = loadFile("../config", "config.csv", True)
    try:
        token = sptfy.authSpotipy()
    except:
        token = None

    ## load tracks in playlist
    if input_playlist is not None:
        in_tracks = loadFile("../input", input_playlist)
    else:
        in_tracks = loadFile("../input", "input.txt")

    ## load database of metadata
    song_db = loadFile("../Databases", "song_db.csv")
    shutil.copyfile("../Databases/song_db.csv",
                    "../Databases/_Backup/song_db.csv")
    artist_db = loadFile("../Databases", "artist_db.csv")
    shutil.copyfile("../Databases/artist_db.csv",
                    "../Databases/_Backup/artist_db.csv")

    unfound_tracks = []
    for track in in_tracks:
        if 'local' in track:
            artist, title, album = sptfy.formatLocalTrack(track)
            song_uri = sptfy.searchSpotifyTrack(artist,
                                                title,
                                                album,
                                                first=True,
                                                token=token)
            if song_uri is not None:
                if not dbm.lookupSongBySpotifyID(song_uri, song_db):
                    song = sptfy.getAudioFeatures(song_uri, token=token)
                    if song is not None:
                        song_db = song_db.append(song, ignore_index=True)
            else:
                print("{} not found.".format(track))
                unfound_tracks.append(track)
        else:
            if not dbm.lookupSongBySpotifyID(track, song_db):
                song = sptfy.getAudioFeatures(track, token=token)
                if song is not None:
                    song_db = song_db.append(song, ignore_index=True)
                else:
                    print("{} not found.".format(track))
                    unfound_tracks.append(track)
    song_db = song_db.drop_duplicates('spotify_id')
    dbm.saveDataFrame(song_db, "../Databases", "song_db.csv")

    ## subset song database on tracks in playlist
    db_subset = dbm.subsetDataFrame(song_db, in_tracks)

    if genres:
        ## build dict of artists with genres
        artist_db = dbm.buildArtistDataFrame(db_subset, artist_db, token=token)
        dbm.saveDataFrame(artist_db, "../Databases", "artist_db.csv")

        ## add artist genres to songs subset db
        ## and because capitalization might be different
        ## drop 'artist' from one of the dataframes before merging
        artist_db = artist_db.drop('artist', 1)
        db_out = dbm.addArtistDataToSongs(db_subset, artist_db)
    else:
        db_out = db_subset

    return db_out, unfound_tracks
コード例 #10
0
def main():

    # ## load tracks in playlist
    fives = loadFile("input", "fives.txt")
    fours = loadFile("input", "fours.txt")
    threes = loadFile("input", "threes.txt")
    twos = loadFile("input", "twos.txt")
    ones = loadFile("input", "ones.txt")

    ## load database of song ratings
    db = loadFile("../Databases", "song_ratings_db.csv")
    album_ratings = loadFile("../Databases", "album_ratings_db.csv")

    config = loadFile("../config", "config.csv", True)
    token = sptfy.authSpotipy()

    rating = 0
    for ls in [ones, twos, threes, fours, fives]:
        ## playlists are looped in this order such that if a song is in multiple lists it's rating will end up being the highest one
        rating = rating + 1
        for song in ls:
            track_id, spotify_uri = sptfy.getSpotifyTrackIDs(song)
            if not lookupSongBySpotifyID(track_id, db):
                track = sptfy.pullSpotifyTrack(track_id, token = token)
                db = db.append([{'spotify_id' : track_id, 'artist' : track['artist'], 'album' : track['album'], 'spotify_album_id' : track['spotify_album_id'], 'song' : track['title'], 'rating' : rating}])

    db = db.sort(['artist', 'album', 'rating'])
    saveDataFrame(db, "../Databases", "song_ratings_db.csv")

    for album in pd.unique(zip(db.artist, db.album)):
        artist = album[0]
        album = album[1]
        if not lookupAlbumBySpotifyID(album, album_ratings):
            album_id = pd.unique(db[db.artist == artist][db.album == album]['spotify_album_id'])[0]
            album_data = sptfy.pullSpotifyAlbum(album_id, token = token)
            ratings = db[db.artist == artist][db.album == album]['rating'].tolist()
            if len(ratings) < 3:
                ## don't make album ratings for singles
                continue
            score = 0
            countNot3 = len(np.where(r != 3))
            countMoreThan2 = len(np.where(r > 2))
            countMoreThan3 = len(np.where(r > 3))
            for r in ratings:
                if r == 5:
                    score = score + 100 * 1.0
                    scoreNot3 = score + 100 * 1.0
                elif r == 4:
                    score = score + 80 * 1.2
                    scoreNot3 = score + 80 * 1.2
                elif r == 3:
                    score = score + 60 * 1.0
                elif r == 2:
                    score = score + 40 * 1.2
                    scoreNot3 = score + 40 * 1.2
                elif r == 1:
                    score = score + 20 * 1.0
                    scoreNot3 = score + 20 * 1.0
            std = np.std(ratings)
            if std == 0.0:
                std = 0.25

            if countNot3 == 0:
                adjMean = 3
            else:
                adjMean = scoreNot3 / countNot3
            prop4or5 = countMoreThan3 / len(r)
            adj1 = (adjMean - 3) * prop4or5
            adj2 = adj1 + countMoreThan2 * 0.03

            score = np.mean(ratings) + adj2

            if prop4or5 == 0:
                adjSD = std * 0.05
            else:
                adjSD = std * prop4or5 / len(r)

            score = score - adjSD

            ## min possible score: (mean of 1-star)
            min1 = 1.0
            ## max possible score: Radiohead "OK Computer"
            max1 = 5.662521
            min2 = -1.0
            max2 = -0.125

            scaledScore = (score - min1) / (max1 - min1)
            ## transform (curves the linear scores to inflate higher scores and reduce lower)
            transformedScore = -1 * (8 ^ (-1 * scaledScore))
            scaledScore = (transformedScore - min2) / (max2 - min2)
            album_score = (round (scaledScore * 1000)) / 1.0

            if album_score > 1000:
	        album_score = 1000

            if album_score >= 965:
                album_rating = 5.0
            elif album_score >= 890:
                album_rating = 4.5
            elif album_score >= 750:
                album_rating = 4.0
            elif album_score >= 690:
                album_rating = 3.5
            elif album_score >= 625:
                album_rating = 3.0
            elif album_score >= 420:
                album_rating = 2.5
            elif album_score > 325:
                album_rating = 2.0
            elif album_score > 235:
                album_rating = 1.5
            elif album_score >= 100:
                album_rating = 1.0
            elif album_score < 100:
                album_rating = 0.5
            album_ratings = album_ratings.append([{'spotify_album_id' : album_id, 'artist' : artist, 'album' : album, 'year' : album_data['year'], 'album_rating' : album_rating, 'album_score' : album_score}])

    saveDataFrame(album_ratings, "../Databases", "album_ratings_db.csv")
コード例 #11
0
def main(ags, explicit=False):

    if args.billboardchart is not None:
        with open(args.billboardchart, 'r') as f:
            billboardchart = [line.rstrip() for line in f]
    else:
        billboardchart = None

    cleans = []
    explicits = []
    local_tracks = []

    config = loadFile("../config", "config.csv", True)
    try:
        token = sptfy.authSpotipy()
    except:
        token = None
    sptpy = sptfy.getSpotifyCred()

    cleans, local_tracks = sptfy.pullSpotifyTracks('../input',
                                                   'cleans.txt',
                                                   sptpy=sptpy)
    explicits, local_tracks = sptfy.pullSpotifyTracks(
        '../input',
        'explicits.txt',
        tracks=explicits,
        local_tracks=local_tracks,
        sptpy=sptpy)

    if explicit:
        # sort explicit list
        sorted_tracks = sorted(explicits,
                               key=lambda x: float(x['popularity']),
                               reverse=True)
    else:
        # sort explicit list
        sorted_tracks = sorted(explicits,
                               key=lambda x: float(x['popularity']),
                               reverse=True)
        # now replace explicits with cleans
        for e in explicits:  # replace explicit track href by the clean version's href
            for c in cleans:
                if e['artist'] in c['artist'] and e['title'][:3] in c[
                        'title'][:3]:  # compare artist name and first 4 characters of track name
                    e['spotify_id'] = c['spotify_id']

    print("\n")
    for item in sorted_tracks:
        print(
            str(item['popularity']) + " :: " + item['artist'] + " - " +
            item['title'])
    print("\n")
    for item in sorted_tracks:
        print("spotify:track:{}".format(item['spotify_id'].strip()))
    for item in local_tracks:
        print(item)
        print("\n")

    if billboardchart is not None:
        print(
            "\nLook into keeping the following songs in Airplay this week...\n"
        )
        for item in sorted_tracks:
            if item['popularity'] < 75:
                artist = item['artist']
                title = item['title']
                artist = re.sub("&", "and",
                                re.sub(r'([^\s\w]|_)+', '', artist).lower())
                title = re.sub("&", "and",
                               re.sub(r'([^\s\w]|_)+', '', title).lower())
                for u in billboardchart:
                    x = re.sub(r'([^\s\w]|_)+', '', re.sub("&", "and",
                                                           u)).lower()
                    if artist in x:
                        artist_match = True
                    else:
                        artist_match = False
                    if title in x:
                        title_match = True
                    else:
                        title_match = False
                    xs = [i for i in x.split(" ")]
                    if title_match or (artist_match and any(
                        [True for t in title.split(" ") if t in xs])):
                        print(
                            str(item['popularity']) + " :: " + item['artist'] +
                            " - " + item['title'])
                        break
    else:
        print(
            "\nIf you want to compare to Billboard Chart, please provide a txt version as an arg to --billboardchart\n"
        )

    print("\n")