elm-twitch/src/Twitch.elm

86 lines
1.9 KiB
Elm

module Twitch exposing
( Playlist
, Video
, decodePlaylists
, fetchPlaylists
, playlistMiniatureUrl
, videoId
, videoMiniatureUrl
, videoName
)
import Http
import Iso8601
import Json.Decode as Decode
import Time
type alias Video =
{ name : String
, url : String
, duration : Int
, date : Maybe Time.Posix
}
type alias Playlist =
{ url : String
, name : String
, videos : List Video
}
decodeVideo : Decode.Decoder Video
decodeVideo =
Decode.map4 Video
(Decode.field "title" Decode.string)
(Decode.field "url" Decode.string)
(Decode.map Basics.round (Decode.field "duration" Decode.float))
(Decode.maybe (Decode.field "date" Iso8601.decoder))
decodePlaylist : Decode.Decoder Playlist
decodePlaylist =
Decode.map3 Playlist
(Decode.field "url" Decode.string)
(Decode.field "title" Decode.string)
(Decode.field "videos" (Decode.map (List.sortBy .url >> List.reverse) (Decode.list decodeVideo)))
decodePlaylists : Decode.Decoder (List Playlist)
decodePlaylists =
Decode.map (List.sortBy .url >> List.reverse) (Decode.list decodePlaylist)
fetchPlaylists : (Result Http.Error (List Playlist) -> msg) -> Cmd msg
fetchPlaylists resultToMsg =
Http.get
{ url = "videos/index.json"
, expect = Http.expectJson resultToMsg decodePlaylists
}
videoName : Video -> String
videoName video =
video.url
videoId : Video -> String
videoId video =
String.dropLeft 1 video.url |> String.replace "/" "-"
playlistMiniatureUrl : Playlist -> String
playlistMiniatureUrl playlist =
case List.head (List.reverse playlist.videos) of
Just v ->
"videos/" ++ playlist.url ++ v.url ++ "miniature-050.png"
_ ->
""
videoMiniatureUrl : Playlist -> Video -> String
videoMiniatureUrl playlist video =
"videos/" ++ playlist.url ++ video.url ++ "miniature-050.png"