<jupyter_start><jupyter_text>Connexion a spotify<jupyter_code>CRED_PATH_SPOTIFY = "credentials-spotify.json"
with open(CRED_PATH_SPOTIFY, 'r') as handle:
    data = json.load(handle)
spotify = spotipy.Spotify(auth_manager=SpotifyClientCredentials(
    **data
))<jupyter_output><empty_output><jupyter_text>Connexion a google<jupyter_code>df = load_from_api()
playlist = create_playlist()
playlist<jupyter_output><empty_output><jupyter_text>DF preprocessing<jupyter_code>df.set_index(['genre', 'sub_genre', 'artist', 'album', 'song'], inplace=True)
df = df.apply(lambda s: s.str.replace(",", "."))
df = df.apply(lambda s: pd.to_numeric(s, errors='coerce'))
df.describe()
search = "artist:\"Black Sabbath\" track:\"Evil Woman\" album:\"Black Sabbath\""
search = "artist:\"Creedence Clearwater Revival\" album:\"Cosmos Factory\" track:\"I Heard It Through The Grapevine\""
# search = "artist:\"Creedence Clearwater Revival\" album:\"Co\" track:\"I Heard It Through The Grapevine\""
# search = "artist:\"Creedence Clearwater Revival\" track:\"I Heard It Through The Grapevine\""
# search = "artist:\"Black\%20Sabbath\"%20track:\"Evil%20Woman\"%20album:\"Black%20Sabbath\""
pprint.pp(spotify.search(search, market="US"))
# pprint.pp(spotify.search(search, market="FR"))
searches = ['artist:"The Who" album:"Who\'s Next" track:"Baba O Riley"',
  'artist:"The Who" album:"Whos Next" track:"Baba O Riley"']
i = 0
search = searches[i]
pprint.pp(spotify.search(search, market="FR"))
markets = ["FR", "US"]
def search_string(row):
    search = ""
    # artists
    artists = list(map(str.strip, row.artist.split(",")))
    if len(artists) > 1:
        sep = '" AND "'
        search += f"artist:\"{sep.join(artists)}\""
    else:
        search += f"artist:\"{artists[0]}\""
    # album
    if row.album != "N/A":
        search += f" album:\"{row.album}\""
    # track name
    search += f" track:\"{row.song}\""
    # dealing with "'""
    # sometimes it will work with the "'" and sometimes not
    if "'" in search:
        searches_s = [search, search.replace("'", "")]
    else:
        searches_s = [search]
    searches = []
    for market in markets:
        for search in searches_s:
            searches.append((search, market))
    return searches
ids = pd.Series(index=df.index, dtype=str, name="ids")
bad_formats = []
for idx, (_, content) in enumerate(df.index.to_frame().iterrows()):
    # search = f"artist:\"{content.artist}\" track:\"{content.song}\" album:\"{content.album}\""
    searches = search_string(content)
    bad_format = []
    for search, market in searches:
        try:
            res = spotify.search(search, market=market)
            track = res['tracks']['items'][0]
        except IndexError as e:
            print(f"{search} not in spotify")
            bad_format.append((search, market))
        else:
            break
    else:
        bad_formats.append(bad_format)
        ids.iloc[idx] = None
        continue
    album = track['album']['name']
    name = track['name']
    artist = track['artists'][0]['name']
    id = track['id']
    ids.iloc[idx] = id
    print(f"{idx:<4}/{len(df)} : {id} {name} {artist} {album}")
ids = ids.rename("ids")
ids.to_pickle("cache/ids.pkl")
ids[ids.isnull()]
bad_formats<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17859327217125381, 'fraction_numerical': 0.004892966360856269, 'mean_word_length': 3.7821637426900585, 'pattern_counts': {'":': 1, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '626315', 'license': 'MIT', 'n_tokens_mistral': 1180, 'n_tokens_neox': 1161, 'n_words': 234, 'path': 'test_connect_spotify.ipynb', 'repo_name': 'QuVil/mon-bot-le-dj-old', 'size': '3270'}