Skip to main content

src/examples@scrobble.erl

-module(examples@scrobble).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/examples/scrobble.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(
    " Record a play with the pipe-friendly builder API.\n"
    "\n"
    " Run with:\n"
    "   gleam run -m examples/scrobble\n"
).

-file("src/examples/scrobble.gleam", 12).
-spec main() -> nil.
main() ->
    Client = begin
        _pipe = rocksky:new(),
        rocksky:with_bearer_token(_pipe, <<"YOUR_BSKY_TOKEN"/utf8>>)
    end,
    Result = begin
        _pipe@1 = rocksky@scrobble:new_scrobble(
            <<"Karma Police"/utf8>>,
            <<"Radiohead"/utf8>>
        ),
        _pipe@2 = rocksky@scrobble:with_album(_pipe@1, <<"OK Computer"/utf8>>),
        _pipe@3 = rocksky@scrobble:with_duration_ms(_pipe@2, 263000),
        _pipe@4 = rocksky@scrobble:with_year(_pipe@3, 1997),
        _pipe@5 = rocksky@scrobble:with_track_number(_pipe@4, 6),
        _pipe@6 = rocksky@scrobble:with_spotify_link(
            _pipe@5,
            <<"https://open.spotify.com/track/63OQupATfueTdZMWTxW03A"/utf8>>
        ),
        _pipe@7 = rocksky@scrobble:create(_pipe@6),
        rocksky:send(_pipe@7, Client)
    end,
    case Result of
        {ok, View} ->
            gleam_stdlib:println(
                <<<<<<"scrobbled: "/utf8,
                            (gleam@option:unwrap(
                                erlang:element(6, View),
                                <<"?"/utf8>>
                            ))/binary>>/binary,
                        " — "/utf8>>/binary,
                    (gleam@option:unwrap(erlang:element(7, View), <<"?"/utf8>>))/binary>>
            );

        {error, E} ->
            gleam_stdlib:println_error(
                <<"scrobble failed: "/utf8, (gleam@string:inspect(E))/binary>>
            )
    end.