Current section
Files
Jump to
Current section
Files
src/inlay@youtube.erl
-module(inlay@youtube).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/inlay/youtube.gleam").
-export([detect/1, render/2]).
-file("src/inlay/youtube.gleam", 112).
-spec parse_time(binary()) -> gleam@option:option(integer()).
parse_time(Value) ->
Cleaned = case gleam_stdlib:string_ends_with(Value, <<"s"/utf8>>) of
true ->
gleam@string:drop_end(Value, 1);
false ->
Value
end,
case gleam_stdlib:parse_int(Cleaned) of
{ok, N} ->
{some, N};
{error, _} ->
none
end.
-file("src/inlay/youtube.gleam", 134).
-spec find_param(list({binary(), binary()}), binary()) -> gleam@option:option(binary()).
find_param(Params, Key) ->
case gleam@list:find(
Params,
fun(Pair) -> erlang:element(1, Pair) =:= Key end
) of
{ok, {_, Value}} ->
{some, Value};
{error, _} ->
none
end.
-file("src/inlay/youtube.gleam", 104).
-spec find_start(list({binary(), binary()})) -> gleam@option:option(integer()).
find_start(Params) ->
case {find_param(Params, <<"t"/utf8>>),
find_param(Params, <<"start"/utf8>>)} of
{{some, T}, _} ->
parse_time(T);
{_, {some, S}} ->
parse_time(S);
{none, none} ->
none
end.
-file("src/inlay/youtube.gleam", 123).
-spec parse_query(gleam@uri:uri()) -> list({binary(), binary()}).
parse_query(Url) ->
case erlang:element(7, Url) of
{some, Q} ->
case gleam_stdlib:parse_query(Q) of
{ok, Params} ->
Params;
{error, _} ->
[]
end;
none ->
[]
end.
-file("src/inlay/youtube.gleam", 96).
-spec detect_short(gleam@uri:uri()) -> gleam@option:option(inlay@embed:embed()).
detect_short(Url) ->
Params = parse_query(Url),
case gleam@uri:path_segments(erlang:element(6, Url)) of
[Id] when Id =/= <<""/utf8>> ->
{some, {youtube_video, Id, find_start(Params), none}};
_ ->
none
end.
-file("src/inlay/youtube.gleam", 89).
-spec detect_playlist(list({binary(), binary()})) -> gleam@option:option(inlay@embed:embed()).
detect_playlist(Params) ->
case find_param(Params, <<"list"/utf8>>) of
{some, Id} ->
{some, {youtube_playlist, Id}};
none ->
none
end.
-file("src/inlay/youtube.gleam", 81).
-spec detect_watch(list({binary(), binary()})) -> gleam@option:option(inlay@embed:embed()).
detect_watch(Params) ->
case find_param(Params, <<"v"/utf8>>) of
{some, Id} ->
{some,
{youtube_video,
Id,
find_start(Params),
find_param(Params, <<"list"/utf8>>)}};
none ->
none
end.
-file("src/inlay/youtube.gleam", 68).
-spec detect_youtube(gleam@uri:uri()) -> gleam@option:option(inlay@embed:embed()).
detect_youtube(Url) ->
Segments = gleam@uri:path_segments(erlang:element(6, Url)),
Query_params = parse_query(Url),
case Segments of
[<<"watch"/utf8>>] ->
detect_watch(Query_params);
[<<"playlist"/utf8>>] ->
detect_playlist(Query_params);
[<<"embed"/utf8>>, Id] ->
{some, {youtube_video, Id, find_start(Query_params), none}};
[<<"shorts"/utf8>>, Id@1] ->
{some, {youtube_video, Id@1, none, none}};
_ ->
none
end.
-file("src/inlay/youtube.gleam", 11).
-spec detect(gleam@uri:uri()) -> gleam@option:option(inlay@embed:embed()).
detect(Url) ->
case erlang:element(4, Url) of
{some, <<"youtube.com"/utf8>>} ->
detect_youtube(Url);
{some, <<"www.youtube.com"/utf8>>} ->
detect_youtube(Url);
{some, <<"m.youtube.com"/utf8>>} ->
detect_youtube(Url);
{some, <<"youtu.be"/utf8>>} ->
detect_short(Url);
{some, <<"www.youtu.be"/utf8>>} ->
detect_short(Url);
_ ->
none
end.
-file("src/inlay/youtube.gleam", 20).
-spec render(inlay@embed:embed(), inlay@embed:config()) -> {ok,
lustre@vdom@vnode:element(any())} |
{error, nil}.
render(Embed, Config) ->
Domain = case erlang:element(2, Config) of
{some, {youtube_config, true, _}} ->
<<"https://www.youtube-nocookie.com"/utf8>>;
_ ->
<<"https://www.youtube.com"/utf8>>
end,
Aspect_ratio = case erlang:element(2, Config) of
{some, {youtube_config, _, {some, R}}} ->
R;
_ ->
<<"56.25%"/utf8>>
end,
case Embed of
{youtube_video, Id, Start_time, Playlist} ->
Base = <<<<Domain/binary, "/embed/"/utf8>>/binary, Id/binary>>,
Params = case {Start_time, Playlist} of
{{some, T}, {some, P}} ->
<<<<<<"?start="/utf8, (erlang:integer_to_binary(T))/binary>>/binary,
"&list="/utf8>>/binary,
P/binary>>;
{{some, T@1}, none} ->
<<"?start="/utf8, (erlang:integer_to_binary(T@1))/binary>>;
{none, {some, P@1}} ->
<<"?list="/utf8, P@1/binary>>;
{none, none} ->
<<""/utf8>>
end,
Src = <<Base/binary, Params/binary>>,
{ok,
inlay@iframe:responsive(
Src,
Aspect_ratio,
[lustre@attribute:attribute(
<<"allowfullscreen"/utf8>>,
<<"true"/utf8>>
),
lustre@attribute:attribute(
<<"allow"/utf8>>,
<<"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"/utf8>>
)]
)};
{youtube_playlist, Id@1} ->
Src@1 = <<<<Domain/binary, "/embed/videoseries?list="/utf8>>/binary,
Id@1/binary>>,
{ok,
inlay@iframe:responsive(
Src@1,
Aspect_ratio,
[lustre@attribute:attribute(
<<"allowfullscreen"/utf8>>,
<<"true"/utf8>>
),
lustre@attribute:attribute(
<<"allow"/utf8>>,
<<"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"/utf8>>
)]
)};
_ ->
{error, nil}
end.