Something went wrong. Try again.
A music player that connects to your cloud/distributed storage. forked from tokono.ma/diffuse
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201module LastFm exposing (..)
import Commonimport Httpimport Json.Decode as Jsonimport List.Ext as Listimport MD5import String.Ext as Stringimport Tracks exposing (Track)import Tuple.Ext as Tupleimport Url exposing (Url)import Url.Ext as Url
-- 🏔
apiKey = "4f0fe85b67baef8bb7d008a8754a95e5"
apiUrl = "https://ws.audioscrobbler.com/2.0"
notSoSecret = "0cec3ca0f58e04a5082f1131aba1e0d3"
-- 🌳
type alias Model = { authenticating : Bool , sessionKey : Maybe String }
initialModel : ModelinitialModel = { authenticating = False , sessionKey = Nothing }
authenticationCommand : (Result Http.Error String -> msg) -> Url -> Cmd msgauthenticationCommand msg url = case Url.extractQueryParam "token" url of Just token -> Http.get { url = authenticatedUrl [ ( "method", "auth.getSession" ) , ( "token", token ) ] , expect = Json.string |> Json.at [ "session", "key" ] |> Http.expectJson msg }
Nothing -> Cmd.none
-- 📣
disconnect : Model -> Modeldisconnect model = { model | sessionKey = Nothing }
failedToAuthenticate : Model -> ModelfailedToAuthenticate model = { model | authenticating = False }
gotSessionKey : String -> Model -> ModelgotSessionKey sessionKey model = { model | sessionKey = Just sessionKey }
-- 🎵
nowPlaying : Model -> { duration : Int, msg : msg, track : Track } -> Cmd msgnowPlaying model { duration, msg, track } = case model.sessionKey of Just sessionKey -> Http.post { url = apiUrl , body = [ ( "duration", String.fromInt duration ) , ( "track", track.tags.title ) , ( "trackNumber", String.fromInt track.tags.nr )
-- , ( "method", "track.updateNowPlaying" ) , ( "sk", sessionKey ) ] |> addAlbum track |> addArtist track |> authenticatedBody , expect = Http.expectWhatever (always msg) }
Nothing -> Cmd.none
scrobble : Model -> { duration : Int, msg : msg, timestamp : Int, track : Track } -> Cmd msgscrobble model { duration, msg, timestamp, track } = case model.sessionKey of Just sessionKey -> Http.post { url = apiUrl , body = [ ( "duration", String.fromInt duration ) , ( "track", track.tags.title ) , ( "trackNumber", String.fromInt track.tags.nr )
-- , ( "method", "track.scrobble" ) , ( "sk", sessionKey ) , ( "timestamp", String.fromInt timestamp ) ] |> addAlbum track |> addArtist track |> authenticatedBody , expect = Http.expectWhatever (always msg) }
Nothing -> Cmd.none
addAlbum track list = case track.tags.album of Just album -> ( "album", album ) :: list
Nothing -> list
addArtist track list = case track.tags.artist of Just artist -> ( "artist", artist ) :: list
Nothing -> list
-- 🔱
authenticatedBody : List ( String, String ) -> Http.BodyauthenticatedBody params = params |> authenticatedParams |> Common.queryString |> String.dropLeft 1 |> Http.stringBody "application/x-www-form-urlencoded"
authenticatedUrl : List ( String, String ) -> StringauthenticatedUrl params = params |> authenticatedParams |> Common.queryString |> String.append apiUrl
authenticatedParams : List ( String, String ) -> List ( String, String )authenticatedParams params = let extendedParams = ( "api_key", apiKey ) :: params in extendedParams |> List.sortBy Tuple.first |> List.map (Tuple.uncurry String.append) |> String.concat |> String.addSuffix notSoSecret |> MD5.hex |> Tuple.pair "api_sig" |> List.addTo extendedParams |> (::) ( "format", "json" ) |> List.sortBy Tuple.first