<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Grant Ammons</title>
    <description>The latest articles on Forem by Grant Ammons (@gammons).</description>
    <link>https://forem.com/gammons</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F89004%2Fa514aea0-96ee-44ca-98e2-817d90ec10a9.jpeg</url>
      <title>Forem: Grant Ammons</title>
      <link>https://forem.com/gammons</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/gammons"/>
    <language>en</language>
    <item>
      <title>What I learned creating 12inch.reviews, a mashup of Spotify and Pitchfork</title>
      <dc:creator>Grant Ammons</dc:creator>
      <pubDate>Wed, 22 Jul 2020 14:56:57 +0000</pubDate>
      <link>https://forem.com/gammons/what-i-learned-creating-12inch-reviews-a-mashup-of-spotify-and-pitchfork-59oi</link>
      <guid>https://forem.com/gammons/what-i-learned-creating-12inch-reviews-a-mashup-of-spotify-and-pitchfork-59oi</guid>
      <description>&lt;p&gt;I'm a huge music nerd.  I've played in many bands in my teens 20s, and music is a big part of my life.  I'm also a big fan of &lt;a href="https://pitchfork.com/"&gt;Pitchfork&lt;/a&gt; music reviews.&lt;/p&gt;

&lt;p&gt;There was a mashup site I was using called &lt;a href="http://pitchify.com"&gt;Pitchify&lt;/a&gt;, which was no longer updating, and it eventually got taken down.  So I did what any engineer would do and I created my own mashup!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://12inch.reviews"&gt;12inch.reviews&lt;/a&gt; is a mashup of Pitchfork's &lt;a href="https://pitchfork.com/reviews/albums/"&gt;album reviews&lt;/a&gt; with Spotify's &lt;a href="https://developer.spotify.com/documentation/web-playback-sdk/quick-start/"&gt;web playback SDK&lt;/a&gt;.  It utilizes the browser's &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API"&gt;IndexedDB API&lt;/a&gt; to allow for fast, responsive searching and sorting of 17k+ album reviews, and allows you to play the full album right in the browser!&lt;/p&gt;

&lt;p&gt;As with any side project, I had a few learning goals in mind that I wanted to bake in:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Continue to invest in learning React, specifically &lt;a href="https://reactjs.org/docs/hooks-intro.html"&gt;React hooks&lt;/a&gt;, and progressive web apps.&lt;/li&gt;
&lt;li&gt;Learn &lt;a href="https://tailwindcss.com/"&gt;tailwind.css&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;I wanted it to include &lt;em&gt;all&lt;/em&gt; of Pitchfork's reviews, and have them be easily searchable.  There are a lot of seminal albums that I just haven't had exposure to.  Being able to find them easily would be a requirement.&lt;/li&gt;
&lt;li&gt;I wanted to leverage different and interesting browser technologies to keep the main functionality of this site all on the frontend.&lt;/li&gt;
&lt;li&gt;I wanted it to be completely &lt;a href="https://github.com/gammons/12inch.reviews"&gt;open source&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Pitchfork has over 20k reviews on their site, so being able to store that many records on the frontend, specifically in Javascript, would be a challenge.  Each browser has different &lt;a href="https://developers.google.com/web/tools/workbox/guides/storage-quota"&gt;storage quotas&lt;/a&gt; that aren't particularly well-documented.  So I needed to think about how to work around these quotas in a seamless and transparent way.&lt;/p&gt;

&lt;h2&gt;
  
  
  The backend
&lt;/h2&gt;

&lt;p&gt;12inch.reviews uses a simple(ish) &lt;a href="https://github.com/gammons/12inch.reviews/blob/master/retriever/retrieve.rb"&gt;retriever script&lt;/a&gt; script that does the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;utilizes Pitchfork's &lt;a href="https://pitchfork.com/api/v2/search/?types=reviews"&gt;undocumented API&lt;/a&gt; to find new albums since the last time the script was run&lt;/li&gt;
&lt;li&gt;Attempts to find that album using Spotify's &lt;a href="https://developer.spotify.com/documentation/web-api/reference/search/search/"&gt;search API&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;If found, add that album to a simple &lt;a href="https://www.sqlite.org/index.html"&gt;SQLite&lt;/a&gt; DB&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There is another backend function that will take the contents of the SQLite DB and to create a series of JSON files, which includes all the data the frontend needs.  The structure of each JSON album looks like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;13501&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"pitchfork_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"5929e2d1eb335119a49ef060"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Out of Tune"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"artist"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Mojave 3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"rating"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"6.3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"bnm"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"bnr"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"label"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"4AD"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"https://pitchfork.com/reviews/albums/5376-out-of-tune/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Out of Tune is a Steve Martin album. Yes, I'll explain: Once upon a time, there was ..."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"genre"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Rock"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"spotify_album_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2TLUvacBePI5753CqHPpxF"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"spotify_artist_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"4jSYHcSo85heWskYvAULio"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"image_url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"https://i.scdn.co/image/ab67616d0000b27360b1fa1c0a15bcb97f9544a2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"page"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"created_at"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1999-01-12 06:00:00 UTC"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"updated_at"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2019-10-25 12:33:03 UTC"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"timestamp"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;916120800&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Once the JSON files are created, they are uploaded to S3 for the frontend to use.  Each album entry has all the info needed in order for Spotify's web SDK to use them on the frontend, and to be searchable.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://github.com/gammons/12inch.reviews/blob/master/retriever/Rakefile"&gt;retriever Rakefile&lt;/a&gt; also has functions to backfill all albums (takes multiple hours!) and has some utility functions to be able to create a new SQLite DB and other functions to massage the data into the correct format.&lt;/p&gt;

&lt;p&gt;The main task, &lt;code&gt;refresh_and_upload&lt;/code&gt; runs hourly.  Currently it's running as a &lt;a href="https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/"&gt;Kubernetes CronJob&lt;/a&gt; on my homelab Kube cluster (I'll talk more about that in another post).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Netlify Lambda functions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;12inch.reviews is hosted on &lt;a href="https://netlify.com"&gt;Netlify&lt;/a&gt;, mainly because Netlify is amazing.  It provides a seamless CI/CD pipeline, SSL, and &lt;a href="https://www.netlify.com/products/functions/"&gt;AWS Lambda-like functions&lt;/a&gt; - all for free.&lt;/p&gt;

&lt;p&gt;I use these functions to "log in" a user via Spotify Oauth.  I would have done this all on the frontend, except for the fact that Oauth requires a secret token and that can't be exposed on the frontend.  &lt;/p&gt;

&lt;p&gt;Since Spotify's access tokens are short-lived (1 hour max!) there is also a secondary function that will renew an access token seamlessly upon playback.&lt;/p&gt;

&lt;p&gt;Once an access token is obtained, state is stored via the browser's &lt;code&gt;LocalStorage&lt;/code&gt; API.&lt;/p&gt;

&lt;h3&gt;
  
  
  The frontend
&lt;/h3&gt;

&lt;p&gt;The frontend of 12inch.reviews is a relatively simple &lt;code&gt;create-react-app&lt;/code&gt; single-page app, that provides search and sort functionality, as well as the ability to play any album using Spotify's &lt;a href="https://developer.spotify.com/documentation/web-playback-sdk/quick-start/"&gt;web playback SDK&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Getting all the album data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you visit 12inch.reviews the first time, it will show the albums from a small JSON file called &lt;code&gt;initial.json&lt;/code&gt;.  This file includes only the first 25 most recent albums, so we have something to paint on the screen.&lt;/p&gt;

&lt;p&gt;Then, the rest of the album data will be backfilled in via a series of &lt;code&gt;fetch&lt;/code&gt;es to retrieve all of the JSON files.  I decided to partition each JSON file with 1000 albums, so there are 17 files altogether.  Each album JSON file is at least 600k uncompressed, so there is probably room for more optimization here.&lt;/p&gt;

&lt;p&gt;After each JSON file is retrieved, they are stored in an IndexedDB on the frontend.  Subsequent visits to 12inch.reviews don't require the large JSON payload  - it will only load the delta payloads into the DB.  I'm taking advantage of the fact that these reviews are immutable - once they are written they will never change.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Searching albums&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Although IndexedDB is great for &lt;em&gt;storing&lt;/em&gt; this data, there is currently no functionality to actually &lt;em&gt;query&lt;/em&gt; IndexedDB like a regular database.  So in order for 12inch.reviews to do searching and sorting, all of the data must be loaded into a &lt;a href="https://github.com/gammons/12inch.reviews/blob/master/src/app.js#L53"&gt;simple javascript array&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Playing albums&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I utilized Spotify's &lt;a href="https://developer.spotify.com/documentation/web-playback-sdk/quick-start/"&gt;Web playback SDK&lt;/a&gt; to do the actual playing.  It provides a series of hooks to use in order to initialize the player, and to do the actual playing.&lt;/p&gt;

&lt;p&gt;I wrapped the actual playing into a &lt;a href="https://github.com/gammons/12inch.reviews/blob/master/src/models/spotifyPlayer.js"&gt;simple class&lt;/a&gt; that ensures a refreshed access token is always provided.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://github.com/gammons/12inch.reviews/blob/master/src/components/player.js"&gt;player component&lt;/a&gt; is one of the most complex react components in the app.  Although the web playback SDK has its own state, I had to essentially "sync up" the player component's state with the SDK's state.  As with any type of synchronization, there are &lt;em&gt;probably&lt;/em&gt; bugs keeping these 2 things in sync with each other.&lt;/p&gt;

&lt;p&gt;The progress bar is clickable and utilizes some simple CSS animations to look and feel like a regular music progress bar.  &lt;/p&gt;

&lt;p&gt;IANA designer, but I was heavily influenced by Tesla's UX when designing the player component.&lt;/p&gt;

&lt;h3&gt;
  
  
  Criticism of Spotify's Web Playback SDK
&lt;/h3&gt;

&lt;p&gt;My experience with Spotify's web playback SDK has been sub-par.  The SDK does not have a &lt;code&gt;package.json&lt;/code&gt; file, and therefore is not in the npm universe.  There isn't an easy way to hook the SDK into a React or Vue app.  This required a lot of &lt;a href="https://github.com/gammons/12inch.reviews/blob/master/src/components/player.js#L52-L58"&gt;manual syncing&lt;/a&gt; code that is probably hiding bugs.&lt;/p&gt;

&lt;p&gt;They have a &lt;a href="https://github.com/spotify/web-playback-sdk"&gt;public issue tracker&lt;/a&gt; on Github, but many of the issues don't have answered questions.&lt;/p&gt;

&lt;p&gt;It's hard for me to understand who the target audience was for this SDK.  I think it would benefit greatly from being open source and part of the NPM ecosystem.  This would allow others to create wrappers for popular frameworks, which would allow me to remove my terrible syncing code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lessons learned + planned optimizations
&lt;/h3&gt;

&lt;p&gt;This was a really fun project to work on.  It took me about 6 weeks of coding, utilizing my "side project hours" (roughly 5:30am - 7am on weekdays).&lt;/p&gt;

&lt;p&gt;I learned a bunch of things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tailwind.css&lt;/li&gt;
&lt;li&gt;IndexedDB&lt;/li&gt;
&lt;li&gt;Netlify Lambda Functions&lt;/li&gt;
&lt;li&gt;Spotify's APIs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;12inch.reviews is a toy.  It has &lt;em&gt;enormous&lt;/em&gt; shortcomings, mainly the fact that it needs to backfill nearly 20Mb of album review data in order to work well.  This is insanely inefficient and wouldn't be appropriate for anything other than a personal side project site coded for educational purposes.&lt;/p&gt;

&lt;p&gt;Other shortcomings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I don't feel like I leaned into Flow types as much as I could.&lt;/li&gt;
&lt;li&gt;Overall performance is still not great, as measured by Chromes Dev Tools.&lt;/li&gt;
&lt;li&gt;I should utilize a service worker to populate the indexeddb.&lt;/li&gt;
&lt;li&gt;Could create a GraphQL backend to do the data serving, to alleviate the need to bring all 20MB of data to the frontend.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>sideprojects</category>
      <category>netlify</category>
      <category>react</category>
      <category>spotify</category>
    </item>
  </channel>
</rss>
