scrapbook/event
Examples
You can use this module to create HTTP requests and parse HTTP responses, which allows you to use this module in all targets.
Single event
import gleam/httpc
import gleam/result
import scrapbook/event
// define a const with user agent
fn single_event() -> Result(event.EventData, Nil) {
use req <- result.try(result.replace_error(
event.construct_event_request_from_url(
"https://www.facebook.com/events/2221600491537520",
user_agent,
),
Nil,
))
use resp <- result.try(result.replace_error(httpc.send(req), Nil))
result.replace_error(event.scrape_event_response(resp), Nil)
}
pub fn main() -> Nil {
let assert Ok(single_event) = single_event()
echo single_event
Nil
}
JavaScript target
import gleam/fetch
import gleam/javascript/promise.{type Promise}
import gleam/result
import scrapbook/event
// define a const with user agent
fn single_event() -> Promise(Result(event.EventData, Nil)) {
use req <- promise.try_await(
promise.resolve(result.replace_error(
event.construct_event_request_from_url(
"https://www.facebook.com/events/2221600491537520",
user_agent,
),
Nil,
)),
)
use resp <- promise.try_await(
promise.map(fetch.send(req), result.replace_error(_, Nil)),
)
use resp <- promise.try_await(
promise.map(fetch.read_text_body(resp), result.replace_error(_, Nil)),
)
promise.resolve(result.replace_error(event.scrape_event_response(resp), Nil))
}
pub fn main() -> Promise(Nil) {
use single_event_result <- promise.await(single_event())
let assert Ok(single_event) = single_event_result
echo single_event
promise.resolve(Nil)
}
Single event with redirects
import gleam/httpc
import gleam/result
import scrapbook/event
// define a const with user agent
fn single_event_redir() -> Result(event.EventData, Nil) {
use req <- result.try(result.replace_error(
event.construct_event_request_from_url(
"https://www.facebook.com/events/1137956700212933/1137956706879599/",
user_agent,
),
Nil,
))
use resp <- result.try(result.replace_error(
httpc.configure() |> httpc.follow_redirects(True) |> httpc.dispatch(req),
Nil,
))
result.replace_error(event.scrape_event_response(resp), Nil)
}
pub fn main() -> Nil {
let assert Ok(single_event_redir) = single_event_redir()
echo single_event_redir
Nil
}
Types
pub type EventBasicData {
EventBasicData(
id: String,
name: String,
start_timestamp: Int,
formatted_date: String,
photo: option.Option(EventPhoto),
video: option.Option(EventVideo),
url: String,
is_online: Bool,
parent: option.Option(EventParent),
siblings: List(EventSibling),
)
}
Constructors
-
EventBasicData( id: String, name: String, start_timestamp: Int, formatted_date: String, photo: option.Option(EventPhoto), video: option.Option(EventVideo), url: String, is_online: Bool, parent: option.Option(EventParent), siblings: List(EventSibling), )
pub type EventData {
EventData(
basic_data: EventBasicData,
description: String,
place: option.Option(EventPlace),
hosts: List(EventHost),
time_details: EventTimeDetails,
ticket_url: option.Option(String),
users_responded: option.Option(Int),
)
}
Constructors
-
EventData( basic_data: EventBasicData, description: String, place: option.Option(EventPlace), hosts: List(EventHost), time_details: EventTimeDetails, ticket_url: option.Option(String), users_responded: option.Option(Int), )
pub type EventHost {
EventHost(
id: String,
name: String,
url: option.Option(String),
type_: HostType,
image_uri: String,
)
}
Constructors
-
EventHost( id: String, name: String, url: option.Option(String), type_: HostType, image_uri: String, )
pub type EventParent {
EventParent(id: String)
}
Constructors
-
EventParent(id: String)
pub type EventPhoto {
EventPhoto(
url: String,
id: String,
image_uri: option.Option(String),
)
}
Constructors
-
EventPhoto( url: String, id: String, image_uri: option.Option(String), )
pub type EventPlace {
Online(url: option.Option(String), type_: OnlineType)
Offline(
id: String,
name: String,
description: option.Option(String),
url: option.Option(String),
location: option.Option(OfflineLocation),
type_: OfflineType,
address: option.Option(String),
city: option.Option(OfflineCity),
)
}
Constructors
-
Online(url: option.Option(String), type_: OnlineType) -
Offline( id: String, name: String, description: option.Option(String), url: option.Option(String), location: option.Option(OfflineLocation), type_: OfflineType, address: option.Option(String), city: option.Option(OfflineCity), )
pub type EventSibling {
EventSibling(
id: String,
start_timestamp: Int,
end_timestamp: option.Option(Int),
parent: EventParent,
)
}
Constructors
-
EventSibling( id: String, start_timestamp: Int, end_timestamp: option.Option(Int), parent: EventParent, )
pub type EventTimeDetails {
EventTimeDetails(
start_timestamp: Int,
end_timestamp: Int,
timezone: String,
)
}
Constructors
-
EventTimeDetails( start_timestamp: Int, end_timestamp: Int, timezone: String, )
pub type EventVideo {
EventVideo(
url: String,
id: String,
image_uri: option.Option(String),
)
}
Constructors
-
EventVideo( url: String, id: String, image_uri: option.Option(String), )
pub type OfflineCity {
OfflineCity(name: String, id: String)
}
Constructors
-
OfflineCity(name: String, id: String)
pub type OfflineLocation {
OfflineLocation(
latitude: Float,
longitude: Float,
country_code: option.Option(String),
)
}
Constructors
-
OfflineLocation( latitude: Float, longitude: Float, country_code: option.Option(String), )
pub type OfflineType {
Text
Place
City
}
Constructors
-
Text -
Place -
City
pub type OnlineType {
MessengerRoom
ThirdParty
FacebookLive
Other
}
Constructors
-
MessengerRoom -
ThirdParty -
FacebookLive -
Other
Values
pub fn construct_event_request_from_id(
id: String,
user_agent: String,
) -> Result(request.Request(String), error.ScrapeError)
Construct the request for the event with a given ID.
pub fn construct_event_request_from_url(
url: String,
user_agent: String,
) -> Result(request.Request(String), error.ScrapeError)
Construct the request for the event with a given URL.
pub fn get_event(
html: String,
) -> Result(EventData, List(error.PropertyError))
Get all possible event data from the given HTML string.
pub fn scrape_event_response(
resp: response.Response(String),
) -> Result(EventData, error.ScrapeError)
Scrape the event from the response received for a previously generated event request.