Dirty Code
Twizzle • 2 Dec 2022 •
Dirty code saves the day!
Yesterday, my son was making a Christmas present for his girlfriend. He had built a 365 track playlist in Spotify and wanted to print a Spotify Code for each track and have them in a glass jar for her to pick from each day. For those who don’t know, a Spotify Code is like a QR code you can scan from within the app and it takes you to that song.
Using the SpotifyCodes.com website, you can enter a song link, such as spotify:track:4QAVzoQziO6ZobIjZtTFwo and it will create a JPG for you of the link:
The problem was, that my son would have to repeat this process 365 times, copying and pasting the song link from Spotify, creating the code image and then copying and pasting it into a Word document, resizing it to fit in a table and then eventually print and cut them all out.
Once I looked at the URL for one of the codes, I knew it could be automated:
https://scannables.scdn.co/uri/plain/jpeg/000000/white/640/spotify:track:4QAVzoQziO6ZobIjZtTFwo
It was the same URL each time, with the song link appended on the end. Ideal for a quick and dirty code solution.
The first thing was to get a CSV of the playlist, in which there would be the song links I could work with. I happened upon Exportify and quickly had the data I needed.
I dropped it into Excel and deleted the columns I didn’t need, then surrounded the song link in quotations and a comma at the end of each line. I then spun up a quick PHP environment on my MacBook using Local and started building the PHP code.
Firstly, I dropped the CSV data from Excel into a PHP array, then added a foreach
block to iterate through each line.
$list = ["spotify:track:4QAVzoQziO6ZobIjZtTFwo","spotify:track:1QAVz1QziO6ZobIjZtTF23"];
foreach ($list as $item)
{
// do code here;
}
The code then output anecho '<img src="https://scannables.scdn.co/uri/plain/jpeg/FFFFFF/black/256/' . $item . ">
which took the song link as the $item
variable and showed the images one by one.
This worked perfectly and I soon had a whole screen full of codes to print out.
I added some CSS to include a 1px solid black border around each image, making it easier for us to cut them out once printed and I also resized the image slightly to make it small enough to fit in the jar.
What would have taken him several hours to do only took me 10 minutes in the end. Most of the work was him cutting them all out from the 7 pages of A4 we printed.
Sometimes, all you need is a little bit of a dirty code solution.
Of course, I could have written code to use the original CSV file, loading it into a variable and iterating through it, but that would have taken more time.
Comments
That’s so brilliant! Hope your son gets inspired by this and taking up some coding!

@tao Sorry if this ends up being depressing. I just asked ChatGPT to do this. “Can you write a PHP script that would display an images of Spotify codes from a list of codes in a CSV file?”
I don’t know if the code will come through or not, but this is what it came up with in about two seconds. It doesn’t look like the URL is quite right, but it seems like a reasonable program.
Yes, it is possible to write a PHP script that would display images of Spotify codes from a list of codes in a CSV file. Here is an example of such a script:
Copy code
<?php
// Read the CSV file
$codes = array_map('str_getcsv', file('codes.csv'));
// Loop through the codes
foreach ($codes as $code) {
// Get the code value from the CSV file
$codeValue = $code[0];
// Generate the Spotify code image URL
$codeImageUrl = "https://www.scannables.spotify.com/codes/v1/" . $codeValue . "/150";
// Display the Spotify code image
echo "<img src='" . $codeImageUrl . "' />";
}
This script would read the codes from the CSV file and generate the URL for the corresponding Spotify code image. It would then display the images on the page using the
HTML tag. Please note that this script is provided for illustrative purposes only and may need to be adjusted or modified depending on the specific requirements of your project.

@Winkletter wow 😧 Didn’t know ChatGPT can do code too, like Github Copilot

Bloody amazing @Winkletter - very clever stuff and yes, it almost works as-is. Shame I didn’t think of that earlier!
Fantastic solution, but the girlfriend should still think it was all done manually over several hours for full effect.