-module(scrapbook@event). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([get_event/1, construct_event_request_from_url/2, construct_event_request_from_id/2, scrape_event_response/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]). -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(), 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("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 235). -spec photo_image_uri_decoder() -> decode@zero:decoder(binary()). photo_image_uri_decoder() -> decode@zero:one_of( decode@zero:at( [<<"image"/utf8>>, <<"uri"/utf8>>], {decoder, fun decode@zero:decode_string/1} ), [decode@zero:at( [<<"full_image"/utf8>>, <<"uri"/utf8>>], {decoder, fun decode@zero:decode_string/1} )] ). -file("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 241). -spec photo_decoder() -> decode@zero:decoder(event_photo()). photo_decoder() -> decode@zero:field( <<"url"/utf8>>, {decoder, fun decode@zero:decode_string/1}, fun(Url) -> decode@zero:field( <<"id"/utf8>>, {decoder, fun decode@zero:decode_string/1}, fun(Id) -> decode@zero:then( begin _pipe = photo_image_uri_decoder(), scrapbook@internal@decoder:or_none(_pipe) end, fun(Image_uri) -> decode@zero:success( {event_photo, Url, Id, Image_uri} ) end ) end ) end ). -file("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 249). -spec video_image_uri_decoder() -> decode@zero:decoder(binary()). video_image_uri_decoder() -> decode@zero:at( [<<"image"/utf8>>, <<"uri"/utf8>>], {decoder, fun decode@zero:decode_string/1} ). -file("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 253). -spec video_decoder() -> decode@zero:decoder(event_video()). video_decoder() -> decode@zero:field( <<"url"/utf8>>, {decoder, fun decode@zero:decode_string/1}, fun(Url) -> decode@zero:field( <<"id"/utf8>>, {decoder, fun decode@zero:decode_string/1}, fun(Id) -> decode@zero:then( begin _pipe = video_image_uri_decoder(), scrapbook@internal@decoder:or_none(_pipe) end, fun(Image_uri) -> decode@zero:success( {event_video, Url, Id, Image_uri} ) end ) end ) end ). -file("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 261). -spec parent_decoder() -> decode@zero:decoder(event_parent()). parent_decoder() -> decode@zero:field( <<"id"/utf8>>, {decoder, fun decode@zero:decode_string/1}, fun(Id) -> decode@zero:success({event_parent, Id}) end ). -file("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 267). -spec sibling_decoder() -> decode@zero:decoder(event_sibling()). sibling_decoder() -> decode@zero:field( <<"id"/utf8>>, {decoder, fun decode@zero:decode_string/1}, fun(Id) -> decode@zero:field( <<"start_timestamp"/utf8>>, {decoder, fun decode@zero:decode_int/1}, fun(Start_timestamp) -> decode@zero:field( <<"end_timestamp"/utf8>>, decode@zero:optional( {decoder, fun decode@zero:decode_int/1} ), fun(End_timestamp) -> decode@zero:field( <<"parent_event"/utf8>>, parent_decoder(), fun(Parent) -> decode@zero:success( {event_sibling, Id, Start_timestamp, End_timestamp, Parent} ) end ) end ) end ) end ). -file("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 276). -spec siblings_decoder() -> decode@zero:decoder(list(event_sibling())). siblings_decoder() -> decode@zero:list(sibling_decoder()). -file("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 280). -spec get_basic_data(binary()) -> {ok, event_basic_data()} | {error, list(scrapbook@error:property_error())}. get_basic_data(Html) -> Decoder = (decode@zero:field( <<"id"/utf8>>, {decoder, fun decode@zero:decode_string/1}, fun(Id) -> decode@zero:field( <<"name"/utf8>>, {decoder, fun decode@zero:decode_string/1}, fun(Name) -> decode@zero:field( <<"start_timestamp"/utf8>>, {decoder, fun decode@zero:decode_int/1}, fun(Start_timestamp) -> decode@zero:then( begin _pipe = decode@zero:at( [<<"cover_media_renderer"/utf8>>, <<"cover_photo"/utf8>>, <<"photo"/utf8>>], photo_decoder() ), scrapbook@internal@decoder:or_none(_pipe) end, fun(Photo) -> decode@zero:field( <<"day_time_sentence"/utf8>>, {decoder, fun decode@zero:decode_string/1}, fun(Formatted_date) -> decode@zero:then( begin _pipe@1 = decode@zero:at( [<<"cover_media_renderer"/utf8>>, <<"cover_video"/utf8>>], video_decoder() ), scrapbook@internal@decoder:or_none( _pipe@1 ) end, fun(Video) -> decode@zero:field( <<"url"/utf8>>, {decoder, fun decode@zero:decode_string/1}, fun(Url) -> decode@zero:field( <<"is_online"/utf8>>, {decoder, fun decode@zero:decode_bool/1}, fun(Is_online) -> decode@zero:field( <<"parent_if_exists_or_self"/utf8>>, begin _pipe@2 = parent_decoder( ), scrapbook@internal@decoder:or_none( _pipe@2 ) end, fun( Parent ) -> decode@zero:field( <<"comet_neighboring_siblings"/utf8>>, siblings_decoder( ), fun( Siblings ) -> decode@zero: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 )), scrapbook@internal@html:get_property( Html, <<"event"/utf8>>, fun(_capture) -> decode@zero:run(_capture, Decoder) end, fun(_) -> true end ). -file("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 321). -spec get_description(binary()) -> {ok, binary()} | {error, list(scrapbook@error:property_error())}. get_description(Html) -> Decoder = (decode@zero:field( <<"text"/utf8>>, {decoder, fun decode@zero:decode_string/1}, fun(Text) -> decode@zero:success(Text) end )), scrapbook@internal@html:get_property( Html, <<"event_description"/utf8>>, fun(_capture) -> decode@zero:run(_capture, Decoder) end, fun(_) -> true end ). -file("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 332). -spec online_type_decoder() -> decode@zero:decoder(online_type()). online_type_decoder() -> decode@zero:then( {decoder, fun decode@zero:decode_string/1}, fun(Type_) -> case Type_ of <<"MESSENGER_ROOM"/utf8>> -> decode@zero:success(messenger_room); <<"THIRD_PARTY"/utf8>> -> decode@zero:success(third_party); <<"FB_LIVE"/utf8>> -> decode@zero:success(facebook_live); <<"OTHER"/utf8>> -> decode@zero:success(other); _ -> decode@zero:failure(other, <<"OnlineType"/utf8>>) end end ). -file("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 344). -spec get_place_online(binary()) -> {ok, event_place()} | {error, list(scrapbook@error:property_error())}. get_place_online(Html) -> Decoder = (decode@zero:field( <<"third_party_url"/utf8>>, decode@zero:optional({decoder, fun decode@zero:decode_string/1}), fun(Url) -> decode@zero:field( <<"type"/utf8>>, online_type_decoder(), fun(Type_) -> decode@zero:success({online, Url, Type_}) end ) end )), scrapbook@internal@html:get_property( Html, <<"online_event_setup"/utf8>>, fun(_capture) -> decode@zero:run(_capture, Decoder) end, fun(_) -> true end ). -file("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 359). -spec offline_location_decoder() -> decode@zero:decoder(offline_location()). offline_location_decoder() -> decode@zero:field( <<"latitude"/utf8>>, {decoder, fun decode@zero:decode_float/1}, fun(Latitude) -> decode@zero:field( <<"longitude"/utf8>>, {decoder, fun decode@zero:decode_float/1}, fun(Longitude) -> decode@zero:then( begin _pipe = decode@zero:at( [<<"reverse_geocode"/utf8>>, <<"country_alpha_two"/utf8>>], {decoder, fun decode@zero:decode_string/1} ), scrapbook@internal@decoder:or_none(_pipe) end, fun(Country_code) -> decode@zero:success( {offline_location, Latitude, Longitude, Country_code} ) end ) end ) end ). -file("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 370). -spec offline_type_decoder() -> decode@zero:decoder(offline_type()). offline_type_decoder() -> decode@zero:then( {decoder, fun decode@zero:decode_string/1}, fun(Type_) -> case Type_ of <<"TEXT"/utf8>> -> decode@zero:success(text); <<"PLACE"/utf8>> -> decode@zero:success(place); <<"CITY"/utf8>> -> decode@zero:success(city); _ -> decode@zero:failure(text, <<"OfflineType"/utf8>>) end end ). -file("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 381). -spec offline_city_decoder() -> decode@zero:decoder(offline_city()). offline_city_decoder() -> decode@zero:field( <<"contextual_name"/utf8>>, {decoder, fun decode@zero:decode_string/1}, fun(Name) -> decode@zero:field( <<"id"/utf8>>, {decoder, fun decode@zero:decode_string/1}, fun(Id) -> decode@zero:success({offline_city, Name, Id}) end ) end ). -file("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 390). -spec get_place_offline(binary()) -> {ok, event_place()} | {error, list(scrapbook@error:property_error())}. get_place_offline(Html) -> Decoder = (decode@zero:field( <<"id"/utf8>>, {decoder, fun decode@zero:decode_string/1}, fun(Id) -> decode@zero:field( <<"name"/utf8>>, {decoder, fun decode@zero:decode_string/1}, fun(Name) -> decode@zero:then( begin _pipe = decode@zero:at( [<<"best_description"/utf8>>, <<"text"/utf8>>], {decoder, fun decode@zero:decode_string/1} ), scrapbook@internal@decoder:or_none(_pipe) end, fun(Description) -> decode@zero:field( <<"url"/utf8>>, begin _pipe@1 = {decoder, fun decode@zero:decode_string/1}, scrapbook@internal@decoder:or_none(_pipe@1) end, fun(Url) -> decode@zero:field( <<"location"/utf8>>, decode@zero:optional( offline_location_decoder() ), fun(Location) -> decode@zero:field( <<"place_type"/utf8>>, offline_type_decoder(), fun(Type_) -> decode@zero:then( begin _pipe@2 = decode@zero:at( [<<"address"/utf8>>, <<"street"/utf8>>], {decoder, fun decode@zero:decode_string/1} ), scrapbook@internal@decoder:or_none( _pipe@2 ) end, fun(Address) -> decode@zero:field( <<"city"/utf8>>, begin _pipe@3 = offline_city_decoder( ), scrapbook@internal@decoder:or_none( _pipe@3 ) end, fun(City) -> decode@zero:success( {offline, Id, Name, Description, Url, Location, Type_, Address, City} ) end ) end ) end ) end ) end ) end ) end ) end )), scrapbook@internal@html:get_property( Html, <<"event_place"/utf8>>, fun(_capture) -> decode@zero:run(_capture, Decoder) end, fun(_) -> true end ). -file("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 426). -spec host_type_decoder() -> decode@zero:decoder(host_type()). host_type_decoder() -> decode@zero:then( {decoder, fun decode@zero:decode_string/1}, fun(Type_) -> case Type_ of <<"User"/utf8>> -> decode@zero:success(user); <<"Page"/utf8>> -> decode@zero:success(page); _ -> decode@zero:failure(page, <<"OfflineType"/utf8>>) end end ). -file("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 436). -spec host_photo_decoder() -> decode@zero:decoder(binary()). host_photo_decoder() -> decode@zero:field( <<"uri"/utf8>>, {decoder, fun decode@zero:decode_string/1}, fun(Image_uri) -> decode@zero:success(Image_uri) end ). -file("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 442). -spec host_decoder() -> decode@zero:decoder(event_host()). host_decoder() -> decode@zero:field( <<"id"/utf8>>, {decoder, fun decode@zero:decode_string/1}, fun(Id) -> decode@zero:field( <<"name"/utf8>>, {decoder, fun decode@zero:decode_string/1}, fun(Name) -> decode@zero:field( <<"url"/utf8>>, {decoder, fun decode@zero:decode_string/1}, fun(Url) -> decode@zero:field( <<"__typename"/utf8>>, host_type_decoder(), fun(Type_) -> decode@zero:field( <<"profile_picture"/utf8>>, host_photo_decoder(), fun(Image_uri) -> decode@zero:success( {event_host, Id, Name, Url, Type_, Image_uri} ) end ) end ) end ) end ) end ). -file("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 452). -spec hosts_decoder() -> decode@zero:decoder(list(event_host())). hosts_decoder() -> decode@zero:list(host_decoder()). -file("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 456). -spec get_hosts(binary()) -> {ok, list(event_host())} | {error, list(scrapbook@error:property_error())}. get_hosts(Html) -> Decoder = (decode@zero:then( hosts_decoder(), fun(Hosts) -> decode@zero:success(Hosts) end )), scrapbook@internal@html:get_property( Html, <<"event_hosts_that_can_view_guestlist"/utf8>>, fun(_capture) -> decode@zero:run(_capture, Decoder) end, fun(_) -> true end ). -file("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 471). -spec get_time_details(binary(), integer()) -> {ok, event_time_details()} | {error, list(scrapbook@error:property_error())}. get_time_details(Html, Expected_start_timestamp) -> Decoder = (decode@zero:field( <<"start_timestamp"/utf8>>, {decoder, fun decode@zero:decode_int/1}, fun(Start_timestamp) -> decode@zero:field( <<"end_timestamp"/utf8>>, {decoder, fun decode@zero:decode_int/1}, fun(End_timestamp) -> decode@zero:field( <<"tz_display_name"/utf8>>, {decoder, fun decode@zero:decode_string/1}, fun(Timezone) -> decode@zero:success( {event_time_details, Start_timestamp, End_timestamp, Timezone} ) end ) end ) end )), scrapbook@internal@html:get_property( Html, <<"data"/utf8>>, fun(_capture) -> decode@zero:run(_capture, Decoder) end, fun(Event_time_details) -> erlang:element(2, Event_time_details) =:= Expected_start_timestamp end ). -file("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 488). -spec get_ticket_url(binary()) -> {ok, binary()} | {error, list(scrapbook@error:property_error())}. get_ticket_url(Html) -> Decoder = (decode@zero:then( decode@zero:at( [<<"event"/utf8>>, <<"event_buy_ticket_url"/utf8>>], {decoder, fun decode@zero:decode_string/1} ), fun(Url) -> decode@zero:success(Url) end )), scrapbook@internal@html:get_property( Html, <<"ticket_card_content_renderer"/utf8>>, fun(_capture) -> decode@zero:run(_capture, Decoder) end, fun(_) -> true end ). -file("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 502). -spec get_users_responded(binary()) -> {ok, integer()} | {error, list(scrapbook@error:property_error())}. get_users_responded(Html) -> Decoder = (decode@zero:field( <<"count"/utf8>>, {decoder, fun decode@zero:decode_int/1}, fun(Users_responded) -> decode@zero:success(Users_responded) end )), scrapbook@internal@html:get_property( Html, <<"event_connected_users_public_responded"/utf8>>, fun(_capture) -> decode@zero:run(_capture, Decoder) end, fun(_) -> true end ). -file("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 518). -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("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 547). -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("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 562). -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("/home/norbert/projects/scrapbook/src/scrapbook/event.gleam", 575). -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.