Extracting the tracks via the playlist ID, and extracting their data:
User names, customized via a lookup table.
Track names, with ” (feat. …)” and ” - Remastered …” type comments removed, via the "(.+) (\(.+\)|- .+)$" regex.
Artist (the main one) and popularity index.
Duration, converted to seconds.
Added date, removing the HH:MM:SS part with string indexing.
Code
tracks = sp.playlist_tracks("2dDtStk8Nk1PP0JxvdgPEK")["items"]ids_dict = {"wh946a5syk0vfqe3538avp6xd": "Lau 🤠","21m2c7t3edlhzjpcoifnnca6y": "Ric 😝","arthur81u": "Atu 🥴"}data = pl.DataFrame({"User": [ids_dict[x] for x in [x["added_by"]["id"] for x in tracks]],"Track": [re.sub(r"(.+) (\(.+\)|- .+)$", r"\1", x["track"]["name"]) for x in tracks],"Artist": [x["track"]["artists"][0]["name"] for x in tracks],"Popularity": [x["track"]["popularity"] for x in tracks],"Duration": [x["track"]["duration_ms"] /1000for x in tracks],"Added Date": [x["added_at"][0:10] for x in tracks]})
Data visualizations
For each user, getting aggregated stats, via Polars’ group_by + agg methods:
Loading ITables v2.2.4 from the init_notebook_mode cell...
(need help?)
Reminders
Now, onto the automatic email reminders.
The first step is to randomize the subject line, and the content (a picture of us and a music-related quote).
Code
def get_random_color():returnf"rgba({rd.randint(0, 256)}, {rd.randint(0, 256)}, {rd.randint(0, 256)}, 0.8)"subject = rd.choice(['Ponha, ponha a musga imediatamente 🔪','Cadê as músicas, seus vacilões? 🎶','A playlist não vai se encher sozinha! 🚀','Vamos lá, DJ! Solta o som! 🎧','Sem música, sem festa! 🎉','Não seja tímido, queremos te ouvir! 🎵','A megacorporação Spotfy TM exige mais músicas! 🤖',])img_path = rd.choice(listdir('pictures/'))withopen('quotes.txt', 'r', encoding="utf-8") asfile: quote = rd.choice([quote.strip() for quote infile.readlines()])components = {"background-color": get_random_color(),"foreground-color": get_random_color(),"quote": quote}
Then, these contents are injected in a HTML template, that has tags of the form "@@tag@@". The code is partially taken from the template_injector Python package that I created.
Code
withopen('email_template.html', 'r', encoding="utf-8") asfile: template =file.read()# From https://github.com/ricardo-semiao/ricardo-semiao/tree/main/packages/template_injectorfor key, value in components.items(): pattern = re.compile('@@'+ re.escape(key) +'@@') match = re.search(pattern, template) template = re.sub(f'@@{key}@@', value, template)
Lastly, the email is sent via SMTP. The user data is stored in the environment variables, and the content is build with MIME, specially the image attachment via CID.