elm-twitch/src/Twitch.elm

215 lines
5.3 KiB
Elm
Raw Normal View History

2020-10-03 18:44:16 +02:00
module Twitch exposing (Playlist, Video, fetchPlaylists)
import Html.Parser
import Http
import Json.Decode as Decode
import Task exposing (Task)
type alias Playlist =
{ name : String
, videos : List Video
}
type alias Video =
{ name : String
, url : String
2020-10-03 19:39:45 +02:00
, duration : Float
2020-10-03 18:44:16 +02:00
}
get : { url : String, resolver : Http.Resolver x a } -> Task x a
get { url, resolver } =
Http.task
{ body = Http.emptyBody
, headers = []
, method = "GET"
, resolver = resolver
, timeout = Nothing
, url = url
}
fetchPlaylists : Task x (List Playlist)
fetchPlaylists =
fetchPlaylistPath |> Task.andThen fetchPlaylistsMapper
fetchPlaylistPath : Task x (List String)
fetchPlaylistPath =
get
{ url = "/videos"
, resolver = Http.stringResolver parsePlaylistPath
}
fetchPlaylist : String -> Task x Playlist
fetchPlaylist name =
get
{ url = "/videos/" ++ name ++ "/description.json"
2020-10-03 19:39:45 +02:00
, resolver = Http.stringResolver parsePlaylistName
2020-10-03 18:44:16 +02:00
}
2020-10-03 19:39:45 +02:00
|> Task.andThen
(\a ->
Task.map (\b -> Playlist a b)
(get
{ url = "/videos/" ++ name
, resolver = Http.stringResolver parsePlaylistVideoPaths
}
|> Task.andThen (\c -> fetchVideos name c)
)
)
fetchVideo : String -> String -> Task x Video
fetchVideo playlist video =
let
url =
"/videos/" ++ playlist ++ video
in
get
{ url = "/videos/" ++ playlist ++ "/" ++ video ++ "/description.json"
, resolver = Http.stringResolver (parseVideo url)
}
fetchVideos : String -> List String -> Task x (List Video)
fetchVideos playlist videos =
Task.sequence (List.map (fetchVideo playlist) videos)
parseVideo : String -> Http.Response String -> Result x Video
parseVideo url result =
case result of
Http.GoodStatus_ _ content ->
case Decode.decodeString (decodeVideo url) content of
Ok v ->
Ok v
_ ->
Ok { name = "", url = url, duration = 0 }
_ ->
Ok { name = "", url = url, duration = 0 }
2020-10-03 18:44:16 +02:00
fetchPlaylistsMapper : List String -> Task x (List Playlist)
fetchPlaylistsMapper names =
Task.sequence (List.map fetchPlaylist names)
2020-10-03 19:39:45 +02:00
parsePlaylistName : Http.Response String -> Result x String
parsePlaylistName result =
2020-10-03 18:44:16 +02:00
case result of
Http.GoodStatus_ _ content ->
2020-10-03 19:39:45 +02:00
case Decode.decodeString decodePlaylistName content of
2020-10-03 18:44:16 +02:00
Ok p ->
Ok p
_ ->
2020-10-03 19:39:45 +02:00
Ok ""
_ ->
Ok ""
parsePlaylistVideoPaths : Http.Response String -> Result x (List String)
parsePlaylistVideoPaths result =
case result of
Http.GoodStatus_ _ content ->
let
withoutDoctype =
if String.startsWith "<!doctype" (String.toLower content) then
String.lines content |> List.drop 1 |> String.join "\n"
else
content
decoded =
Html.Parser.run withoutDoctype
hrefs =
Result.map findHrefs decoded
in
case hrefs of
Ok h ->
Ok (List.filter (String.endsWith "/") h)
_ ->
Ok []
2020-10-03 18:44:16 +02:00
_ ->
2020-10-03 19:39:45 +02:00
Ok []
2020-10-03 18:44:16 +02:00
parsePlaylistPath : Http.Response String -> Result x (List String)
parsePlaylistPath result =
case result of
Http.GoodStatus_ _ content ->
let
withoutDoctype =
if String.startsWith "<!doctype" (String.toLower content) then
String.lines content |> List.drop 1 |> String.join "\n"
else
content
decoded =
Html.Parser.run withoutDoctype
hrefs =
Result.map findHrefs decoded
in
case hrefs of
Ok h ->
Ok h
_ ->
Ok []
_ ->
Ok []
findHrefsAux : List String -> Html.Parser.Node -> List String
findHrefsAux acc node =
case node of
Html.Parser.Element string (( key, value ) :: t) nodes ->
let
newAcc =
if key == "href" then
value :: acc
else
acc
in
findHrefsAux newAcc (Html.Parser.Element string t nodes)
Html.Parser.Element string [] (h :: t) ->
let
attrs =
findHrefsAux [] h
in
findHrefsAux (acc ++ attrs) (Html.Parser.Element string [] t)
_ ->
acc
findHrefs : List Html.Parser.Node -> List String
findHrefs x =
findHrefsAux [] (Html.Parser.Element "" [] x)
2020-10-03 19:39:45 +02:00
decodePlaylistName : Decode.Decoder String
decodePlaylistName =
Decode.field "title" Decode.string
decodeVideo : String -> Decode.Decoder Video
decodeVideo url =
Decode.map2 (\x y -> Video x url y)
(Decode.field "title" Decode.string)
(Decode.field "duration" Decode.float)