-
-
Save raphaelsaunier/12d5f41cd0ca3eba82a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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