Skip to content

Instantly share code, notes, and snippets.

@raphaelsaunier
Last active February 9, 2016 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raphaelsaunier/12d5f41cd0ca3eba82a2 to your computer and use it in GitHub Desktop.
Save raphaelsaunier/12d5f41cd0ca3eba82a2 to your computer and use it in GitHub Desktop.
(ns paradise.api
(:require [clojure.string :as str])
(:import [org.apache.commons.lang3 StringEscapeUtils]))
(def *now-playing-url* "http://www.radioparadise.com/ajax_nowplaying_data.php")
(def *cover-url* "http://www.radioparadise.com/graphics/covers/{{size}}/{{id}}.jpg")
(def *lyrics-url* "http://radioparadise.com/lyrics/{{id}}.txt")
(def *fields-separator* #"\|")
(def *album-artist-separator* #"—")
(defn slurp-decode [url]
(StringEscapeUtils/unescapeHtml4 (slurp url)))
(defn album-cover-url
"Retrieve the cover of a track/album"
([id] (album-cover-url id "m"))
([id size]
(-> *cover-url*
(.replace "{{size}}" size)
(.replace "{{id}}" id))))
(defn lyrics
"Retrieve the lyrics of a track"
[id]
(let [url (str/replace *lyrics-url* "{{id}}" (str id))]
(slurp-decode url) ))
(defn now-playing
"Retrieve the information about the track that is currently playing"
[url]
(let [response (slurp-decode url)
[artist-and-track track-id _ cover-id] (str/split response *fields-separator*)
[artist track] (str/split artist-and-track *album-artist-separator*)]
{
:artist (str/trim artist)
:track (str/trim track)
:cover (album-cover-url cover-id)
:lyrics (lyrics track-id)
}))
(now-playing *now-playing-url*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment