Better requests, code needs cleaning

This commit is contained in:
Thomas Forgione 2020-10-04 23:03:38 +02:00
parent 65c8e5935b
commit 72b0e8775c
4 changed files with 107 additions and 37 deletions

View File

@ -19,6 +19,7 @@
var lastId, player; var lastId, player;
if (app.ports !== undefined && app.ports.registerVideo !== undefined) {
app.ports.registerVideo.subscribe(function(args) { app.ports.registerVideo.subscribe(function(args) {
var time = parseInt(args[2], 10) || undefined; var time = parseInt(args[2], 10) || undefined;
@ -35,6 +36,7 @@
} }
}); });
}); });
}
</script> </script>
</body> </body>
</html> </html>

View File

@ -27,16 +27,18 @@ type alias Model =
type Page type Page
= Home = Home
| Playlist Twitch.Playlist | Playlist Twitch.PlaylistWithVideos
| Video Twitch.Playlist Twitch.Video | Video Twitch.PlaylistWithVideos Twitch.Video
type Msg type Msg
= Noop = Noop
| PlaylistsReceived ( List Twitch.Playlist, Time.Zone ) | PlaylistsReceived ( List Twitch.Playlist, Time.Zone )
| HomeClicked | HomeClicked
| PlaylistClicked Twitch.Playlist | PlaylistClicked String
| VideoClicked Twitch.Playlist Twitch.Video | PlaylistReceived Twitch.PlaylistWithVideos
| PlaylistReceivedVideo String (Maybe Int) Twitch.PlaylistWithVideos
| VideoClicked Twitch.PlaylistWithVideos Twitch.Video
| UrlReceived Url.Url | UrlReceived Url.Url
| SizeReceived Int Int | SizeReceived Int Int
@ -76,7 +78,7 @@ update msg model =
( PlaylistClicked playlist, Loaded m ) -> ( PlaylistClicked playlist, Loaded m ) ->
( model ( model
, Nav.pushUrl m.key ("#" ++ playlist.url) , Nav.pushUrl m.key ("#" ++ playlist)
) )
( VideoClicked playlist video, Loaded m ) -> ( VideoClicked playlist video, Loaded m ) ->
@ -92,10 +94,14 @@ update msg model =
( split, args ) = ( split, args ) =
case splits of case splits of
h1 :: h2 :: _ -> h1 :: h2 :: _ ->
( String.split "/" h1, parseQueryString h2 ) ( String.split "/" h1 |> List.filter (not << String.isEmpty)
, parseQueryString h2
)
h1 :: _ -> h1 :: _ ->
( String.split "/" h1, Dict.empty ) ( String.split "/" h1 |> List.filter (not << String.isEmpty)
, Dict.empty
)
_ -> _ ->
( [], Dict.empty ) ( [], Dict.empty )
@ -123,31 +129,73 @@ update msg model =
( Nothing, Nothing ) ( Nothing, Nothing )
playlist = playlist =
List.head (List.filter (\x -> Just x.url == playlistName) m.playlists) case m.page of
Playlist p ->
Just p
video = Video p _ ->
Just p
_ ->
Nothing
realVideo =
case playlist of case playlist of
Just p -> Just p ->
List.head (List.filter (\x -> Just (Twitch.videoName x) == videoName) p.videos) case List.head (List.filter (\x -> Just (Twitch.videoName x) == videoName) p.videos) of
Just v ->
Just v
_ ->
Nothing
_ -> _ ->
Nothing Nothing
( page, cmd ) = ( page, cmd ) =
case ( playlist, video ) of case ( ( playlist, realVideo ), ( playlistName, videoName ) ) of
( Just p, Just v ) -> ( ( Just p, _ ), ( Just _, Nothing ) ) ->
( Video p v ( Playlist p
, Ports.registerVideo ( Twitch.videoId v, v.url, time ) , Cmd.none
) )
( Just p, Nothing ) -> ( ( Just p, Just video ), ( Just _, Just _ ) ) ->
( Playlist p, Cmd.none ) ( Video p video
, Ports.registerVideo ( Twitch.videoId video, video.url, time )
)
( ( _, _ ), ( Just name, Nothing ) ) ->
( m.page
, Task.perform
PlaylistReceived
(Twitch.fetchPlaylistWithVideos name)
)
( ( _, _ ), ( Just name, Just video ) ) ->
( m.page
, Task.perform
(PlaylistReceivedVideo video time)
(Twitch.fetchPlaylistWithVideos name)
)
_ -> _ ->
( Home, Cmd.none ) ( Home, Cmd.none )
in in
( Loaded { m | page = page }, cmd ) ( Loaded { m | page = page }, cmd )
( PlaylistReceived p, Loaded m ) ->
( Loaded { m | page = Playlist p }, Cmd.none )
( PlaylistReceivedVideo video time playlist, Loaded m ) ->
case List.head (List.filter (\x -> Twitch.videoName x == video) playlist.videos) of
Just v ->
( Loaded { m | page = Video playlist v }
, Ports.registerVideo ( Twitch.videoId v, v.url, time )
)
_ ->
( Loaded { m | page = Home }, Cmd.none )
_ -> _ ->
( model, Cmd.none ) ( model, Cmd.none )

View File

@ -1,6 +1,8 @@
module Twitch exposing module Twitch exposing
( Playlist ( Playlist
, PlaylistWithVideos
, Video , Video
, fetchPlaylistWithVideos
, fetchPlaylists , fetchPlaylists
, playlistMiniatureUrl , playlistMiniatureUrl
, videoId , videoId
@ -17,6 +19,13 @@ import Time
type alias Playlist = type alias Playlist =
{ url : String
, name : String
, videos : List String
}
type alias PlaylistWithVideos =
{ url : String { url : String
, name : String , name : String
, videos : List Video , videos : List Video
@ -57,7 +66,7 @@ playlistMiniatureUrl : Playlist -> String
playlistMiniatureUrl playlist = playlistMiniatureUrl playlist =
case List.head playlist.videos of case List.head playlist.videos of
Just v -> Just v ->
v.url ++ "miniature-050.png" "videos/" ++ playlist.url ++ v ++ "miniature-050.png"
_ -> _ ->
"" ""
@ -70,7 +79,7 @@ videoMiniatureUrl video =
sortPlaylist : Playlist -> Playlist sortPlaylist : Playlist -> Playlist
sortPlaylist playlist = sortPlaylist playlist =
{ playlist | videos = List.sortBy .url playlist.videos |> List.reverse } { playlist | videos = List.sort playlist.videos |> List.reverse }
sortPlaylists : List Playlist -> List Playlist sortPlaylists : List Playlist -> List Playlist
@ -105,11 +114,22 @@ fetchPlaylist name =
|> Task.andThen |> Task.andThen
(\a -> (\a ->
fetchPlaylistVideoPaths name fetchPlaylistVideoPaths name
|> Task.andThen (\c -> fetchVideos name c) -- |> Task.andThen (\c -> fetchVideos name c)
|> Task.map (Playlist name a) |> Task.map (Playlist name a)
) )
fetchPlaylistWithVideos : String -> Task x PlaylistWithVideos
fetchPlaylistWithVideos name =
fetchPlaylistName name
|> Task.andThen
(\a ->
fetchPlaylistVideoPaths name
|> Task.andThen (\c -> fetchVideos name c)
|> Task.map (PlaylistWithVideos name a)
)
fetchVideo : String -> String -> Task x Video fetchVideo : String -> String -> Task x Video
fetchVideo playlist video = fetchVideo playlist video =
let let

View File

@ -171,13 +171,13 @@ playlistView playlist =
button = button =
Input.button [ Element.width Element.fill, Element.alignTop ] Input.button [ Element.width Element.fill, Element.alignTop ]
{ label = display { label = display
, onPress = Just (Core.PlaylistClicked playlist) , onPress = Just (Core.PlaylistClicked playlist.url)
} }
in in
button button
videoMiniaturesView : Element.Device -> Time.Zone -> Twitch.Playlist -> Element Core.Msg videoMiniaturesView : Element.Device -> Time.Zone -> Twitch.PlaylistWithVideos -> Element Core.Msg
videoMiniaturesView device zone playlist = videoMiniaturesView device zone playlist =
let let
empty = empty =
@ -231,7 +231,7 @@ videoMiniature video =
image image
videoMiniatureView : Time.Zone -> Twitch.Playlist -> Twitch.Video -> Element Core.Msg videoMiniatureView : Time.Zone -> Twitch.PlaylistWithVideos -> Twitch.Video -> Element Core.Msg
videoMiniatureView zone playlist video = videoMiniatureView zone playlist video =
let let
display = display =
@ -249,7 +249,7 @@ videoMiniatureView zone playlist video =
button button
videoInList : Time.Zone -> Twitch.Playlist -> Twitch.Video -> Twitch.Video -> Element Core.Msg videoInList : Time.Zone -> Twitch.PlaylistWithVideos -> Twitch.Video -> Twitch.Video -> Element Core.Msg
videoInList zone playlist activeVideo video = videoInList zone playlist activeVideo video =
let let
( msg, attr ) = ( msg, attr ) =
@ -276,7 +276,7 @@ videoInList zone playlist activeVideo video =
Input.button attr { label = label, onPress = msg } Input.button attr { label = label, onPress = msg }
videoView : Element.Device -> Time.Zone -> Twitch.Playlist -> Twitch.Video -> Element Core.Msg videoView : Element.Device -> Time.Zone -> Twitch.PlaylistWithVideos -> Twitch.Video -> Element Core.Msg
videoView device zone playlist video = videoView device zone playlist video =
let let
( builder, contentPadding ) = ( builder, contentPadding ) =