Current section

Files

Jump to
scrapbook src scrapbook@event.erl
Raw

src/scrapbook@event.erl

-module(scrapbook@event).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/scrapbook/event.gleam").
-export([get_event/1, construct_event_request_from_url/2, construct_event_request_from_id/2, scrape_event_response/1, to_json/1]).
-export_type([event_photo/0, event_video/0, event_parent/0, event_sibling/0, event_basic_data/0, online_type/0, offline_location/0, offline_type/0, offline_city/0, event_place/0, host_type/0, event_host/0, event_time_details/0, event_data/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(
" # Examples\n"
"\n"
" You can use this module to create HTTP requests and parse HTTP responses,\n"
" which allows you to use this module in all targets.\n"
"\n"
" ## Single event\n"
"\n"
" ```gleam\n"
" import gleam/httpc\n"
" import gleam/result\n"
" import scrapbook/event\n"
"\n"
" // define a const with user agent\n"
"\n"
" fn single_event() -> Result(event.EventData, Nil) {\n"
" use req <- result.try(result.replace_error(\n"
" event.construct_event_request_from_url(\n"
" \"https://www.facebook.com/events/2221600491537520\",\n"
" user_agent,\n"
" ),\n"
" Nil,\n"
" ))\n"
" use resp <- result.try(result.replace_error(httpc.send(req), Nil))\n"
" result.replace_error(event.scrape_event_response(resp), Nil)\n"
" }\n"
"\n"
" pub fn main() -> Nil {\n"
" let assert Ok(single_event) = single_event()\n"
" echo single_event\n"
" Nil\n"
" }\n"
" ```\n"
"\n"
" ### JavaScript target\n"
"\n"
" ```gleam\n"
" import gleam/fetch\n"
" import gleam/javascript/promise.{type Promise}\n"
" import gleam/result\n"
" import scrapbook/event\n"
"\n"
" // define a const with user agent\n"
"\n"
" fn single_event() -> Promise(Result(event.EventData, Nil)) {\n"
" use req <- promise.try_await(\n"
" promise.resolve(result.replace_error(\n"
" event.construct_event_request_from_url(\n"
" \"https://www.facebook.com/events/2221600491537520\",\n"
" user_agent,\n"
" ),\n"
" Nil,\n"
" )),\n"
" )\n"
" use resp <- promise.try_await(\n"
" promise.map(fetch.send(req), result.replace_error(_, Nil)),\n"
" )\n"
" use resp <- promise.try_await(\n"
" promise.map(fetch.read_text_body(resp), result.replace_error(_, Nil)),\n"
" )\n"
" promise.resolve(result.replace_error(event.scrape_event_response(resp), Nil))\n"
" }\n"
"\n"
" pub fn main() -> Promise(Nil) {\n"
" use single_event_result <- promise.await(single_event())\n"
" let assert Ok(single_event) = single_event_result\n"
" echo single_event\n"
" promise.resolve(Nil)\n"
" }\n"
" ```\n"
"\n"
" ## Single event with redirects\n"
"\n"
" ```gleam\n"
" import gleam/httpc\n"
" import gleam/result\n"
" import scrapbook/event\n"
"\n"
" // define a const with user agent\n"
"\n"
" fn single_event_redir() -> Result(event.EventData, Nil) {\n"
" use req <- result.try(result.replace_error(\n"
" event.construct_event_request_from_url(\n"
" \"https://www.facebook.com/events/1137956700212933/1137956706879599/\",\n"
" user_agent,\n"
" ),\n"
" Nil,\n"
" ))\n"
" use resp <- result.try(result.replace_error(\n"
" httpc.configure() |> httpc.follow_redirects(True) |> httpc.dispatch(req),\n"
" Nil,\n"
" ))\n"
" result.replace_error(event.scrape_event_response(resp), Nil)\n"
" }\n"
"\n"
" pub fn main() -> Nil {\n"
" let assert Ok(single_event_redir) = single_event_redir()\n"
" echo single_event_redir\n"
" Nil\n"
" }\n"
" ```\n"
).
-type event_photo() :: {event_photo,
binary(),
binary(),
gleam@option:option(binary())}.
-type event_video() :: {event_video,
binary(),
binary(),
gleam@option:option(binary())}.
-type event_parent() :: {event_parent, binary()}.
-type event_sibling() :: {event_sibling,
binary(),
integer(),
gleam@option:option(integer()),
event_parent()}.
-type event_basic_data() :: {event_basic_data,
binary(),
binary(),
integer(),
binary(),
gleam@option:option(event_photo()),
gleam@option:option(event_video()),
binary(),
boolean(),
gleam@option:option(event_parent()),
list(event_sibling())}.
-type online_type() :: messenger_room | third_party | facebook_live | other.
-type offline_location() :: {offline_location,
float(),
float(),
gleam@option:option(binary())}.
-type offline_type() :: text | place | city.
-type offline_city() :: {offline_city, binary(), binary()}.
-type event_place() :: {online, gleam@option:option(binary()), online_type()} |
{offline,
binary(),
binary(),
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(offline_location()),
offline_type(),
gleam@option:option(binary()),
gleam@option:option(offline_city())}.
-type host_type() :: user | page.
-type event_host() :: {event_host,
binary(),
binary(),
gleam@option:option(binary()),
host_type(),
binary()}.
-type event_time_details() :: {event_time_details,
integer(),
integer(),
binary()}.
-type event_data() :: {event_data,
event_basic_data(),
binary(),
gleam@option:option(event_place()),
list(event_host()),
event_time_details(),
gleam@option:option(binary()),
gleam@option:option(integer())}.
-file("src/scrapbook/event.gleam", 224).
-spec encode_event_photo(event_photo()) -> gleam@json:json().
encode_event_photo(Event_photo) ->
{event_photo, Url, Id, Image_uri} = Event_photo,
gleam@json:object(
[{<<"url"/utf8>>, gleam@json:string(Url)},
{<<"id"/utf8>>, gleam@json:string(Id)},
{<<"image_uri"/utf8>>, case Image_uri of
none ->
gleam@json:null();
{some, Value} ->
gleam@json:string(Value)
end}]
).
-file("src/scrapbook/event.gleam", 236).
-spec encode_event_video(event_video()) -> gleam@json:json().
encode_event_video(Event_video) ->
{event_video, Url, Id, Image_uri} = Event_video,
gleam@json:object(
[{<<"url"/utf8>>, gleam@json:string(Url)},
{<<"id"/utf8>>, gleam@json:string(Id)},
{<<"image_uri"/utf8>>, case Image_uri of
none ->
gleam@json:null();
{some, Value} ->
gleam@json:string(Value)
end}]
).
-file("src/scrapbook/event.gleam", 248).
-spec encode_event_parent(event_parent()) -> gleam@json:json().
encode_event_parent(Event_parent) ->
{event_parent, Id} = Event_parent,
gleam@json:object([{<<"id"/utf8>>, gleam@json:string(Id)}]).
-file("src/scrapbook/event.gleam", 253).
-spec encode_event_sibling(event_sibling()) -> gleam@json:json().
encode_event_sibling(Event_sibling) ->
{event_sibling, Id, Start_timestamp, End_timestamp, Parent} = Event_sibling,
gleam@json:object(
[{<<"id"/utf8>>, gleam@json:string(Id)},
{<<"start_timestamp"/utf8>>, gleam@json:int(Start_timestamp)},
{<<"end_timestamp"/utf8>>, case End_timestamp of
none ->
gleam@json:null();
{some, Value} ->
gleam@json:int(Value)
end},
{<<"parent"/utf8>>, encode_event_parent(Parent)}]
).
-file("src/scrapbook/event.gleam", 267).
-spec encode_event_basic_data(event_basic_data()) -> gleam@json:json().
encode_event_basic_data(Event_basic_data) ->
{event_basic_data,
Id,
Name,
Start_timestamp,
Formatted_date,
Photo,
Video,
Url,
Is_online,
Parent,
Siblings} = Event_basic_data,
gleam@json:object(
[{<<"id"/utf8>>, gleam@json:string(Id)},
{<<"name"/utf8>>, gleam@json:string(Name)},
{<<"start_timestamp"/utf8>>, gleam@json:int(Start_timestamp)},
{<<"formatted_date"/utf8>>, gleam@json:string(Formatted_date)},
{<<"photo"/utf8>>, case Photo of
none ->
gleam@json:null();
{some, Value} ->
encode_event_photo(Value)
end},
{<<"video"/utf8>>, case Video of
none ->
gleam@json:null();
{some, Value@1} ->
encode_event_video(Value@1)
end},
{<<"url"/utf8>>, gleam@json:string(Url)},
{<<"is_online"/utf8>>, gleam@json:bool(Is_online)},
{<<"parent"/utf8>>, case Parent of
none ->
gleam@json:null();
{some, Value@2} ->
encode_event_parent(Value@2)
end},
{<<"siblings"/utf8>>,
gleam@json:array(Siblings, fun encode_event_sibling/1)}]
).
-file("src/scrapbook/event.gleam", 303).
-spec encode_online_type(online_type()) -> gleam@json:json().
encode_online_type(Online_type) ->
case Online_type of
messenger_room ->
gleam@json:string(<<"messenger_room"/utf8>>);
third_party ->
gleam@json:string(<<"third_party"/utf8>>);
facebook_live ->
gleam@json:string(<<"facebook_live"/utf8>>);
other ->
gleam@json:string(<<"other"/utf8>>)
end.
-file("src/scrapbook/event.gleam", 312).
-spec encode_offline_location(offline_location()) -> gleam@json:json().
encode_offline_location(Offline_location) ->
{offline_location, Latitude, Longitude, Country_code} = Offline_location,
gleam@json:object(
[{<<"latitude"/utf8>>, gleam@json:float(Latitude)},
{<<"longitude"/utf8>>, gleam@json:float(Longitude)},
{<<"country_code"/utf8>>, case Country_code of
none ->
gleam@json:null();
{some, Value} ->
gleam@json:string(Value)
end}]
).
-file("src/scrapbook/event.gleam", 324).
-spec encode_offline_type(offline_type()) -> gleam@json:json().
encode_offline_type(Offline_type) ->
case Offline_type of
text ->
gleam@json:string(<<"text"/utf8>>);
place ->
gleam@json:string(<<"place"/utf8>>);
city ->
gleam@json:string(<<"city"/utf8>>)
end.
-file("src/scrapbook/event.gleam", 332).
-spec encode_offline_city(offline_city()) -> gleam@json:json().
encode_offline_city(Offline_city) ->
{offline_city, Name, Id} = Offline_city,
gleam@json:object(
[{<<"name"/utf8>>, gleam@json:string(Name)},
{<<"id"/utf8>>, gleam@json:string(Id)}]
).
-file("src/scrapbook/event.gleam", 337).
-spec encode_event_place(event_place()) -> gleam@json:json().
encode_event_place(Event_place) ->
case Event_place of
{online, Url, Type_} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"online"/utf8>>)},
{<<"url"/utf8>>, case Url of
none ->
gleam@json:null();
{some, Value} ->
gleam@json:string(Value)
end},
{<<"type_"/utf8>>, encode_online_type(Type_)}]
);
{offline,
Id,
Name,
Description,
Url@1,
Location,
Type_@1,
Address,
City} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"offline"/utf8>>)},
{<<"id"/utf8>>, gleam@json:string(Id)},
{<<"name"/utf8>>, gleam@json:string(Name)},
{<<"description"/utf8>>, case Description of
none ->
gleam@json:null();
{some, Value@1} ->
gleam@json:string(Value@1)
end},
{<<"url"/utf8>>, case Url@1 of
none ->
gleam@json:null();
{some, Value@2} ->
gleam@json:string(Value@2)
end},
{<<"location"/utf8>>, case Location of
none ->
gleam@json:null();
{some, Value@3} ->
encode_offline_location(Value@3)
end},
{<<"type_"/utf8>>, encode_offline_type(Type_@1)},
{<<"address"/utf8>>, case Address of
none ->
gleam@json:null();
{some, Value@4} ->
gleam@json:string(Value@4)
end},
{<<"city"/utf8>>, case City of
none ->
gleam@json:null();
{some, Value@5} ->
encode_offline_city(Value@5)
end}]
)
end.
-file("src/scrapbook/event.gleam", 378).
-spec encode_host_type(host_type()) -> gleam@json:json().
encode_host_type(Host_type) ->
case Host_type of
user ->
gleam@json:string(<<"user"/utf8>>);
page ->
gleam@json:string(<<"page"/utf8>>)
end.
-file("src/scrapbook/event.gleam", 385).
-spec encode_event_host(event_host()) -> gleam@json:json().
encode_event_host(Event_host) ->
{event_host, Id, Name, Url, Type_, Image_uri} = Event_host,
gleam@json:object(
[{<<"id"/utf8>>, gleam@json:string(Id)},
{<<"name"/utf8>>, gleam@json:string(Name)},
{<<"url"/utf8>>, case Url of
none ->
gleam@json:null();
{some, Value} ->
gleam@json:string(Value)
end},
{<<"type_"/utf8>>, encode_host_type(Type_)},
{<<"image_uri"/utf8>>, gleam@json:string(Image_uri)}]
).
-file("src/scrapbook/event.gleam", 399).
-spec encode_event_time_details(event_time_details()) -> gleam@json:json().
encode_event_time_details(Event_time_details) ->
{event_time_details, Start_timestamp, End_timestamp, Timezone} = Event_time_details,
gleam@json:object(
[{<<"start_timestamp"/utf8>>, gleam@json:int(Start_timestamp)},
{<<"end_timestamp"/utf8>>, gleam@json:int(End_timestamp)},
{<<"timezone"/utf8>>, gleam@json:string(Timezone)}]
).
-file("src/scrapbook/event.gleam", 409).
-spec encode_event_data(event_data()) -> gleam@json:json().
encode_event_data(Event_data) ->
{event_data,
Basic_data,
Description,
Place,
Hosts,
Time_details,
Ticket_url,
Users_responded} = Event_data,
gleam@json:object(
[{<<"basic_data"/utf8>>, encode_event_basic_data(Basic_data)},
{<<"description"/utf8>>, gleam@json:string(Description)},
{<<"place"/utf8>>, case Place of
none ->
gleam@json:null();
{some, Value} ->
encode_event_place(Value)
end},
{<<"hosts"/utf8>>, gleam@json:array(Hosts, fun encode_event_host/1)},
{<<"time_details"/utf8>>, encode_event_time_details(Time_details)},
{<<"ticket_url"/utf8>>, case Ticket_url of
none ->
gleam@json:null();
{some, Value@1} ->
gleam@json:string(Value@1)
end},
{<<"users_responded"/utf8>>, case Users_responded of
none ->
gleam@json:null();
{some, Value@2} ->
gleam@json:int(Value@2)
end}]
).
-file("src/scrapbook/event.gleam", 441).
-spec photo_image_uri_decoder() -> gleam@dynamic@decode:decoder(binary()).
photo_image_uri_decoder() ->
gleam@dynamic@decode:one_of(
gleam@dynamic@decode:at(
[<<"image"/utf8>>, <<"uri"/utf8>>],
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
[gleam@dynamic@decode:at(
[<<"full_image"/utf8>>, <<"uri"/utf8>>],
{decoder, fun gleam@dynamic@decode:decode_string/1}
)]
).
-file("src/scrapbook/event.gleam", 447).
-spec photo_decoder() -> gleam@dynamic@decode:decoder(event_photo()).
photo_decoder() ->
gleam@dynamic@decode:field(
<<"url"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Url) ->
gleam@dynamic@decode:field(
<<"id"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Id) ->
gleam@dynamic@decode:then(
begin
_pipe = photo_image_uri_decoder(),
scrapbook@internal@decoder:or_none(_pipe)
end,
fun(Image_uri) ->
gleam@dynamic@decode:success(
{event_photo, Url, Id, Image_uri}
)
end
)
end
)
end
).
-file("src/scrapbook/event.gleam", 455).
-spec video_image_uri_decoder() -> gleam@dynamic@decode:decoder(binary()).
video_image_uri_decoder() ->
gleam@dynamic@decode:at(
[<<"image"/utf8>>, <<"uri"/utf8>>],
{decoder, fun gleam@dynamic@decode:decode_string/1}
).
-file("src/scrapbook/event.gleam", 459).
-spec video_decoder() -> gleam@dynamic@decode:decoder(event_video()).
video_decoder() ->
gleam@dynamic@decode:field(
<<"url"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Url) ->
gleam@dynamic@decode:field(
<<"id"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Id) ->
gleam@dynamic@decode:then(
begin
_pipe = video_image_uri_decoder(),
scrapbook@internal@decoder:or_none(_pipe)
end,
fun(Image_uri) ->
gleam@dynamic@decode:success(
{event_video, Url, Id, Image_uri}
)
end
)
end
)
end
).
-file("src/scrapbook/event.gleam", 467).
-spec parent_decoder() -> gleam@dynamic@decode:decoder(event_parent()).
parent_decoder() ->
gleam@dynamic@decode:field(
<<"id"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Id) -> gleam@dynamic@decode:success({event_parent, Id}) end
).
-file("src/scrapbook/event.gleam", 473).
-spec sibling_decoder() -> gleam@dynamic@decode:decoder(event_sibling()).
sibling_decoder() ->
gleam@dynamic@decode:field(
<<"id"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Id) ->
gleam@dynamic@decode:field(
<<"start_timestamp"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Start_timestamp) ->
gleam@dynamic@decode:field(
<<"end_timestamp"/utf8>>,
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_int/1}
),
fun(End_timestamp) ->
gleam@dynamic@decode:field(
<<"parent_event"/utf8>>,
parent_decoder(),
fun(Parent) ->
gleam@dynamic@decode:success(
{event_sibling,
Id,
Start_timestamp,
End_timestamp,
Parent}
)
end
)
end
)
end
)
end
).
-file("src/scrapbook/event.gleam", 485).
-spec siblings_decoder() -> gleam@dynamic@decode:decoder(list(event_sibling())).
siblings_decoder() ->
gleam@dynamic@decode:list(sibling_decoder()).
-file("src/scrapbook/event.gleam", 489).
-spec get_basic_data(binary()) -> {ok, event_basic_data()} |
{error, list(scrapbook@error:property_error())}.
get_basic_data(Html) ->
Decoder = begin
gleam@dynamic@decode:field(
<<"id"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Id) ->
gleam@dynamic@decode:field(
<<"name"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Name) ->
gleam@dynamic@decode:field(
<<"start_timestamp"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Start_timestamp) ->
gleam@dynamic@decode:then(
begin
_pipe = gleam@dynamic@decode:at(
[<<"cover_media_renderer"/utf8>>,
<<"cover_photo"/utf8>>,
<<"photo"/utf8>>],
photo_decoder()
),
scrapbook@internal@decoder:or_none(
_pipe
)
end,
fun(Photo) ->
gleam@dynamic@decode:field(
<<"day_time_sentence"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Formatted_date) ->
gleam@dynamic@decode:then(
begin
_pipe@1 = gleam@dynamic@decode:at(
[<<"cover_media_renderer"/utf8>>,
<<"cover_video"/utf8>>],
video_decoder()
),
scrapbook@internal@decoder:or_none(
_pipe@1
)
end,
fun(Video) ->
gleam@dynamic@decode:field(
<<"url"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Url) ->
gleam@dynamic@decode:field(
<<"is_online"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_bool/1},
fun(
Is_online
) ->
gleam@dynamic@decode:field(
<<"parent_if_exists_or_self"/utf8>>,
begin
_pipe@2 = parent_decoder(
),
scrapbook@internal@decoder:or_none(
_pipe@2
)
end,
fun(
Parent
) ->
gleam@dynamic@decode:field(
<<"comet_neighboring_siblings"/utf8>>,
siblings_decoder(
),
fun(
Siblings
) ->
gleam@dynamic@decode:success(
{event_basic_data,
Id,
Name,
Start_timestamp,
Formatted_date,
Photo,
Video,
Url,
Is_online,
Parent,
Siblings}
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end,
scrapbook@internal@html:get_property(
Html,
<<"event"/utf8>>,
Decoder,
fun(_) -> true end
).
-file("src/scrapbook/event.gleam", 536).
-spec get_description(binary()) -> {ok, binary()} |
{error, list(scrapbook@error:property_error())}.
get_description(Html) ->
Decoder = begin
gleam@dynamic@decode:field(
<<"text"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Text) -> gleam@dynamic@decode:success(Text) end
)
end,
scrapbook@internal@html:get_property(
Html,
<<"event_description"/utf8>>,
Decoder,
fun(_) -> true end
).
-file("src/scrapbook/event.gleam", 545).
-spec online_type_decoder() -> gleam@dynamic@decode:decoder(online_type()).
online_type_decoder() ->
gleam@dynamic@decode:then(
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Type_) -> case Type_ of
<<"MESSENGER_ROOM"/utf8>> ->
gleam@dynamic@decode:success(messenger_room);
<<"THIRD_PARTY"/utf8>> ->
gleam@dynamic@decode:success(third_party);
<<"FB_LIVE"/utf8>> ->
gleam@dynamic@decode:success(facebook_live);
<<"OTHER"/utf8>> ->
gleam@dynamic@decode:success(other);
_ ->
gleam@dynamic@decode:failure(other, <<"OnlineType"/utf8>>)
end end
).
-file("src/scrapbook/event.gleam", 557).
-spec get_place_online(binary()) -> {ok, event_place()} |
{error, list(scrapbook@error:property_error())}.
get_place_online(Html) ->
Decoder = begin
gleam@dynamic@decode:field(
<<"third_party_url"/utf8>>,
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Url) ->
gleam@dynamic@decode:field(
<<"type"/utf8>>,
online_type_decoder(),
fun(Type_) ->
gleam@dynamic@decode:success({online, Url, Type_})
end
)
end
)
end,
scrapbook@internal@html:get_property(
Html,
<<"online_event_setup"/utf8>>,
Decoder,
fun(_) -> true end
).
-file("src/scrapbook/event.gleam", 570).
-spec offline_location_decoder() -> gleam@dynamic@decode:decoder(offline_location()).
offline_location_decoder() ->
gleam@dynamic@decode:field(
<<"latitude"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_float/1},
fun(Latitude) ->
gleam@dynamic@decode:field(
<<"longitude"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_float/1},
fun(Longitude) ->
gleam@dynamic@decode:then(
begin
_pipe = gleam@dynamic@decode:at(
[<<"reverse_geocode"/utf8>>,
<<"country_alpha_two"/utf8>>],
{decoder,
fun gleam@dynamic@decode:decode_string/1}
),
scrapbook@internal@decoder:or_none(_pipe)
end,
fun(Country_code) ->
gleam@dynamic@decode:success(
{offline_location,
Latitude,
Longitude,
Country_code}
)
end
)
end
)
end
).
-file("src/scrapbook/event.gleam", 581).
-spec offline_type_decoder() -> gleam@dynamic@decode:decoder(offline_type()).
offline_type_decoder() ->
gleam@dynamic@decode:then(
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Type_) -> case Type_ of
<<"TEXT"/utf8>> ->
gleam@dynamic@decode:success(text);
<<"PLACE"/utf8>> ->
gleam@dynamic@decode:success(place);
<<"CITY"/utf8>> ->
gleam@dynamic@decode:success(city);
_ ->
gleam@dynamic@decode:failure(text, <<"OfflineType"/utf8>>)
end end
).
-file("src/scrapbook/event.gleam", 592).
-spec offline_city_decoder() -> gleam@dynamic@decode:decoder(offline_city()).
offline_city_decoder() ->
gleam@dynamic@decode:field(
<<"contextual_name"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Name) ->
gleam@dynamic@decode:field(
<<"id"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Id) ->
gleam@dynamic@decode:success({offline_city, Name, Id})
end
)
end
).
-file("src/scrapbook/event.gleam", 601).
?DOC(
" Address does not exist for custom location events and is set to an empty\n"
" string for cities.\n"
).
-spec get_place_offline(binary()) -> {ok, event_place()} |
{error, list(scrapbook@error:property_error())}.
get_place_offline(Html) ->
Decoder = begin
gleam@dynamic@decode:field(
<<"id"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Id) ->
gleam@dynamic@decode:field(
<<"name"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Name) ->
gleam@dynamic@decode:then(
begin
_pipe = gleam@dynamic@decode:at(
[<<"best_description"/utf8>>,
<<"text"/utf8>>],
{decoder,
fun gleam@dynamic@decode:decode_string/1}
),
scrapbook@internal@decoder:or_none(_pipe)
end,
fun(Description) ->
gleam@dynamic@decode:optional_field(
<<"url"/utf8>>,
none,
gleam@dynamic@decode:map(
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Field@0) -> {some, Field@0} end
),
fun(Url) ->
gleam@dynamic@decode:field(
<<"location"/utf8>>,
gleam@dynamic@decode:optional(
offline_location_decoder()
),
fun(Location) ->
gleam@dynamic@decode:field(
<<"place_type"/utf8>>,
offline_type_decoder(),
fun(Type_) ->
gleam@dynamic@decode:then(
begin
_pipe@1 = gleam@dynamic@decode:at(
[<<"address"/utf8>>,
<<"street"/utf8>>],
{decoder,
fun gleam@dynamic@decode:decode_string/1}
),
scrapbook@internal@decoder:or_none(
_pipe@1
)
end,
fun(Address) ->
gleam@dynamic@decode:optional_field(
<<"city"/utf8>>,
none,
gleam@dynamic@decode:map(
offline_city_decoder(
),
fun(Field@0) -> {some, Field@0} end
),
fun(City) ->
gleam@dynamic@decode:success(
{offline,
Id,
Name,
Description,
Url,
Location,
Type_,
Address,
City}
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end,
scrapbook@internal@html:get_property(
Html,
<<"event_place"/utf8>>,
Decoder,
fun(_) -> true end
).
-file("src/scrapbook/event.gleam", 645).
-spec host_type_decoder() -> gleam@dynamic@decode:decoder(host_type()).
host_type_decoder() ->
gleam@dynamic@decode:then(
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Type_) -> case Type_ of
<<"User"/utf8>> ->
gleam@dynamic@decode:success(user);
<<"Page"/utf8>> ->
gleam@dynamic@decode:success(page);
_ ->
gleam@dynamic@decode:failure(page, <<"OfflineType"/utf8>>)
end end
).
-file("src/scrapbook/event.gleam", 655).
-spec host_photo_decoder() -> gleam@dynamic@decode:decoder(binary()).
host_photo_decoder() ->
gleam@dynamic@decode:field(
<<"uri"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Image_uri) -> gleam@dynamic@decode:success(Image_uri) end
).
-file("src/scrapbook/event.gleam", 661).
-spec host_decoder() -> gleam@dynamic@decode:decoder(event_host()).
host_decoder() ->
gleam@dynamic@decode:field(
<<"id"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Id) ->
gleam@dynamic@decode:field(
<<"name"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Name) ->
gleam@dynamic@decode:field(
<<"url"/utf8>>,
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Url) ->
gleam@dynamic@decode:field(
<<"__typename"/utf8>>,
host_type_decoder(),
fun(Type_) ->
gleam@dynamic@decode:field(
<<"profile_picture"/utf8>>,
host_photo_decoder(),
fun(Image_uri) ->
gleam@dynamic@decode:success(
{event_host,
Id,
Name,
Url,
Type_,
Image_uri}
)
end
)
end
)
end
)
end
)
end
).
-file("src/scrapbook/event.gleam", 671).
-spec hosts_decoder() -> gleam@dynamic@decode:decoder(list(event_host())).
hosts_decoder() ->
gleam@dynamic@decode:list(host_decoder()).
-file("src/scrapbook/event.gleam", 675).
-spec get_hosts(binary()) -> {ok, list(event_host())} |
{error, list(scrapbook@error:property_error())}.
get_hosts(Html) ->
Decoder = begin
gleam@dynamic@decode:then(
hosts_decoder(),
fun(Hosts) -> gleam@dynamic@decode:success(Hosts) end
)
end,
scrapbook@internal@html:get_property(
Html,
<<"event_hosts_that_can_view_guestlist"/utf8>>,
Decoder,
fun(_) -> true end
).
-file("src/scrapbook/event.gleam", 691).
-spec get_time_details(binary(), integer()) -> {ok, event_time_details()} |
{error, list(scrapbook@error:property_error())}.
get_time_details(Html, Expected_start_timestamp) ->
Decoder = begin
gleam@dynamic@decode:field(
<<"start_timestamp"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Start_timestamp) ->
gleam@dynamic@decode:field(
<<"end_timestamp"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(End_timestamp) ->
gleam@dynamic@decode:field(
<<"tz_display_name"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Timezone) ->
gleam@dynamic@decode:success(
{event_time_details,
Start_timestamp,
End_timestamp,
Timezone}
)
end
)
end
)
end
)
end,
scrapbook@internal@html:get_property(
Html,
<<"data"/utf8>>,
Decoder,
fun(Event_time_details) ->
erlang:element(2, Event_time_details) =:= Expected_start_timestamp
end
).
-file("src/scrapbook/event.gleam", 708).
-spec get_ticket_url(binary()) -> {ok, binary()} |
{error, list(scrapbook@error:property_error())}.
get_ticket_url(Html) ->
Decoder = begin
gleam@dynamic@decode:then(
gleam@dynamic@decode:at(
[<<"event"/utf8>>, <<"event_buy_ticket_url"/utf8>>],
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Url) -> gleam@dynamic@decode:success(Url) end
)
end,
scrapbook@internal@html:get_property(
Html,
<<"ticket_card_content_renderer"/utf8>>,
Decoder,
fun(_) -> true end
).
-file("src/scrapbook/event.gleam", 722).
-spec get_users_responded(binary()) -> {ok, integer()} |
{error, list(scrapbook@error:property_error())}.
get_users_responded(Html) ->
Decoder = begin
gleam@dynamic@decode:field(
<<"count"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Users_responded) ->
gleam@dynamic@decode:success(Users_responded)
end
)
end,
scrapbook@internal@html:get_property(
Html,
<<"event_connected_users_public_responded"/utf8>>,
Decoder,
fun(_) -> true end
).
-file("src/scrapbook/event.gleam", 738).
?DOC(" Get all possible event data from the given HTML string.\n").
-spec get_event(binary()) -> {ok, event_data()} |
{error, list(scrapbook@error:property_error())}.
get_event(Html) ->
gleam@result:'try'(
get_basic_data(Html),
fun(Basic_data) ->
gleam@result:'try'(
get_description(Html),
fun(Description) ->
gleam@result:'try'(case erlang:element(9, Basic_data) of
true ->
_pipe = get_place_online(Html),
gleam@result:map(
_pipe,
fun(Field@0) -> {some, Field@0} end
);
false ->
_pipe@1 = get_place_offline(Html),
scrapbook@internal@html:no_value_to_option(
_pipe@1
)
end, fun(Place) ->
gleam@result:'try'(
get_hosts(Html),
fun(Hosts) ->
gleam@result:'try'(
get_time_details(
Html,
erlang:element(4, Basic_data)
),
fun(Time_details) ->
gleam@result:'try'(
begin
_pipe@2 = get_ticket_url(
Html
),
scrapbook@internal@html:no_value_to_option(
_pipe@2
)
end,
fun(Ticket_url) ->
gleam@result:'try'(
begin
_pipe@3 = get_users_responded(
Html
),
scrapbook@internal@html:no_prefix_to_option(
_pipe@3
)
end,
fun(Users_responded) ->
{ok,
{event_data,
Basic_data,
Description,
Place,
Hosts,
Time_details,
Ticket_url,
Users_responded}}
end
)
end
)
end
)
end
)
end)
end
)
end
).
-file("src/scrapbook/event.gleam", 767).
?DOC(" Construct the request for the event with a given URL.\n").
-spec construct_event_request_from_url(binary(), binary()) -> {ok,
gleam@http@request:request(binary())} |
{error, scrapbook@error:scrape_error()}.
construct_event_request_from_url(Url, User_agent) ->
gleam@result:'try'(
gleam@result:replace_error(
scrapbook@internal@event@url:format_url(Url),
url_error
),
fun(Url@1) ->
case scrapbook@internal@http:construct_request(Url@1, User_agent) of
{ok, Req} ->
{ok, Req};
{error, Err} ->
{error, {request_error, Err}}
end
end
).
-file("src/scrapbook/event.gleam", 782).
?DOC(" Construct the request for the event with a given ID.\n").
-spec construct_event_request_from_id(binary(), binary()) -> {ok,
gleam@http@request:request(binary())} |
{error, scrapbook@error:scrape_error()}.
construct_event_request_from_id(Id, User_agent) ->
gleam@result:'try'(
gleam@result:replace_error(
scrapbook@internal@event@url:id_to_url(Id),
url_error
),
fun(Url) ->
case scrapbook@internal@http:construct_request(Url, User_agent) of
{ok, Req} ->
{ok, Req};
{error, Err} ->
{error, {request_error, Err}}
end
end
).
-file("src/scrapbook/event.gleam", 795).
?DOC(
" Scrape the event from the response received for a previously generated event\n"
" request.\n"
).
-spec scrape_event_response(gleam@http@response:response(binary())) -> {ok,
event_data()} |
{error, scrapbook@error:scrape_error()}.
scrape_event_response(Resp) ->
case get_event(erlang:element(4, Resp)) of
{ok, Event} ->
{ok, Event};
{error, Err} ->
{error, {property_error, Err}}
end.
-file("src/scrapbook/event.gleam", 805).
?DOC(" Encode the scraped event data into a JSON object.\n").
-spec to_json(event_data()) -> gleam@json:json().
to_json(Event_data) ->
encode_event_data(Event_data).