Skip to main content

src/examples@wrapped.erl

-module(examples@wrapped).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/examples/wrapped.gleam").
-export([main/0]).

-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.

?MODULEDOC(
    " Fetch Wrapped-style stats and pull out the most-active hour and the\n"
    " total scrobble count.\n"
    "\n"
    " Run with:\n"
    "   gleam run -m examples/wrapped\n"
).

-file("src/examples/wrapped.gleam", 13).
-spec main() -> {ok, gleam@dynamic:dynamic_()} |
    {error, rocksky@error:rocksy_error()}.
main() ->
    Client = rocksky:new(),
    Payload@1 = case begin
        _pipe = rocksky@stats:get_wrapped(<<"tsiry-sandratraina.com"/utf8>>),
        _pipe@1 = rocksky:year(_pipe, 2025),
        rocksky:send(_pipe@1, Client)
    end of
        {ok, Payload} -> Payload;
        _assert_fail ->
            erlang:error(#{gleam_error => let_assert,
                        message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                        file => <<?FILEPATH/utf8>>,
                        module => <<"examples/wrapped"/utf8>>,
                        function => <<"main"/utf8>>,
                        line => 16,
                        value => _assert_fail,
                        start => 304,
                        'end' => 435,
                        pattern_start => 315,
                        pattern_end => 326})
    end,
    Summary_decoder = begin
        gleam@dynamic@decode:optional_field(
            <<"totalScrobbles"/utf8>>,
            0,
            {decoder, fun gleam@dynamic@decode:decode_int/1},
            fun(Total) ->
                gleam@dynamic@decode:optional_field(
                    <<"totalListeningTimeMinutes"/utf8>>,
                    0,
                    {decoder, fun gleam@dynamic@decode:decode_int/1},
                    fun(Minutes) ->
                        gleam@dynamic@decode:optional_field(
                            <<"mostActiveHour"/utf8>>,
                            -1,
                            {decoder, fun gleam@dynamic@decode:decode_int/1},
                            fun(Most_active_hour) ->
                                gleam@dynamic@decode:success(
                                    {Total, Minutes, Most_active_hour}
                                )
                            end
                        )
                    end
                )
            end
        )
    end,
    {Total@2, Minutes@2, Hour@1} = case gleam@dynamic@decode:run(
        Payload@1,
        Summary_decoder
    ) of
        {ok, {Total@1, Minutes@1, Hour}} -> {Total@1, Minutes@1, Hour};
        _assert_fail@1 ->
            erlang:error(#{gleam_error => let_assert,
                        message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                        file => <<?FILEPATH/utf8>>,
                        module => <<"examples/wrapped"/utf8>>,
                        function => <<"main"/utf8>>,
                        line => 36,
                        value => _assert_fail@1,
                        start => 817,
                        'end' => 894,
                        pattern_start => 828,
                        pattern_end => 855})
    end,
    gleam_stdlib:println(
        <<"scrobbles: "/utf8, (erlang:integer_to_binary(Total@2))/binary>>
    ),
    gleam_stdlib:println(
        <<"listening minutes: "/utf8,
            (erlang:integer_to_binary(Minutes@2))/binary>>
    ),
    gleam_stdlib:println(
        <<"most active hour: "/utf8, (erlang:integer_to_binary(Hour@1))/binary>>
    ),
    _ = begin
        _pipe@2 = rocksky@stats:get_wrapped(<<"tsiry.bsky.social"/utf8>>),
        rocksky:send(_pipe@2, Client)
    end.