Ejemplo n.º 1
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"
Ejemplo n.º 2
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))
Ejemplo n.º 3
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

    if "popularity" in [sort_col1, sort_col2]:
        db, unfound_tracks = sptfy.pullSpotifyTracks('../input', 'input.txt')
        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")
    pd.set_option('display.max_rows', None)
    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))
Ejemplo n.º 4
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")