< / > Manuel Aguilera

Add SomaFM to VLC

2020-12-14

I am a fan of SomaFM, I have listened to them for a long time and my coding sessions are always improved by their many selections of good music. They even have a stream for hacking! how cool is that?

Their web player is very convenient but personally I prefer to run VLC to play music and keep my browser tabs only for coding.

I looked up the code of an existing internet channel add-on, vlc-radiru and it turns out the relevant VLC functions have fair documentation.

Most of the fun happens in the function add_item, (or add_subitem in this case)

--[[
 My SomaFM playlist thingie
 based on https://gitlab.com/rad164/vlc-radiru
--]]

lazily_loaded = false
dkjson        = nil

json_url = "https://somafm.com/channels.json"

function lazy_load()
  if lazily_loaded then return nil end
  dkjson = require("dkjson")
  lazily_loaded = true
end

function descriptor()
  return { title = "SomaFM Channels" }
end

function main()
  lazy_load()
  local fp, err = vlc.stream(json_url)
  if not fp then
    if not err then err = "Unknown error when opening stream" end
    vlc.msg.err(string.format("Oopsies: %s, %s", err, json_url))
    return
  end

  local index, _, err = dkjson.decode(fp:read(fp:getsize()))
  if not index then
    if not err then err = "Unknown error when parsing json" end
    vlc.msg.err(string.format("Oopsies: %s, %s", err, json_url))
    return
  end

  soma_node = vlc.sd.add_node( {title="SomaFM"} )
  for _, channel in ipairs(index.channels) do
    local name = channel.title

    soma_node:add_subitem({
      path = channel.playlists[1].url,
      title = channel.title,
      description = channel.description,
      author = channel.dj,
      genre = channel.genre,
      arturl = channel.xlimage,
      nowplaying = channel.lastPlaying,
    })

  end
end

This file goes in ~/Library/Application Support/org.videolan.vlc/lua/sd/soma.lua or the equivalent location in your platform

If you can show SomaFM some love ❤️