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/io
import gleam/result
import scrapbook/event

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() {
  let assert Ok(single_event) = single_event()
  io.debug(single_event)
}

JavaScript target

import gleam/fetch
import gleam/io
import gleam/javascript/promise
import gleam/result
import scrapbook/event

fn single_event() -> promise.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() {
  use single_event <- promise.try_await(single_event())
  io.debug(single_event)
  promise.resolve(Ok(Nil))
}

Single event with redirects

import gleam/dict
import gleam/http/request.{type Request}
import gleam/http/response.{type Response}
import gleam/httpc
import gleam/io
import gleam/result
import gleam/uri
import scrapbook/event

fn send_redir(req: Request(String)) -> Result(Response(String), Nil) {
  case req |> httpc.send {
    Ok(resp) -> {
      case resp.status >= 300 && resp.status < 400 {
        True -> {
          let headers = dict.from_list(resp.headers)
          use location <- result.try(result.replace_error(
            headers |> dict.get("location"),
            Nil,
          ))
          use uri <- result.try(uri.parse(location))
          send_redir(req |> request.set_path(uri.path))
        }
        False -> Ok(resp)
      }
    }
    Error(_) -> Error(Nil)
  }
}

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(send_redir(req))
  result.replace_error(event.scrape_event_response(resp), Nil)
}

pub fn main() {
  let assert Ok(single_event_redir) = single_event_redir()
  io.debug(single_event_redir)
}

Types

pub type EventBasicData {
  EventBasicData(
    id: String,
    name: String,
    start_timestamp: Int,
    formatted_date: String,
    photo: Option(EventPhoto),
    video: Option(EventVideo),
    url: String,
    is_online: Bool,
    parent: Option(EventParent),
    siblings: List(EventSibling),
  )
}

Constructors

  • EventBasicData(
      id: String,
      name: String,
      start_timestamp: Int,
      formatted_date: String,
      photo: Option(EventPhoto),
      video: Option(EventVideo),
      url: String,
      is_online: Bool,
      parent: Option(EventParent),
      siblings: List(EventSibling),
    )
pub type EventData {
  EventData(
    basic_data: EventBasicData,
    description: String,
    place: Option(EventPlace),
    hosts: List(EventHost),
    time_details: EventTimeDetails,
    ticket_url: Option(String),
    users_responded: Option(Int),
  )
}

Constructors

  • EventData(
      basic_data: EventBasicData,
      description: String,
      place: Option(EventPlace),
      hosts: List(EventHost),
      time_details: EventTimeDetails,
      ticket_url: Option(String),
      users_responded: Option(Int),
    )
pub type EventHost {
  EventHost(
    id: String,
    name: String,
    url: String,
    type_: HostType,
    image_uri: String,
  )
}

Constructors

  • EventHost(
      id: String,
      name: String,
      url: 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(String))
}

Constructors

  • EventPhoto(url: String, id: String, image_uri: Option(String))
pub type EventPlace {
  Online(url: Option(String), type_: OnlineType)
  Offline(
    id: String,
    name: String,
    description: Option(String),
    url: Option(String),
    location: Option(OfflineLocation),
    type_: OfflineType,
    address: Option(String),
    city: Option(OfflineCity),
  )
}

Constructors

  • Online(url: Option(String), type_: OnlineType)
  • Offline(
      id: String,
      name: String,
      description: Option(String),
      url: Option(String),
      location: Option(OfflineLocation),
      type_: OfflineType,
      address: Option(String),
      city: Option(OfflineCity),
    )
pub type EventSibling {
  EventSibling(
    id: String,
    start_timestamp: Int,
    end_timestamp: Option(Int),
    parent: EventParent,
  )
}

Constructors

  • EventSibling(
      id: String,
      start_timestamp: Int,
      end_timestamp: 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(String))
}

Constructors

  • EventVideo(url: String, id: String, image_uri: Option(String))
pub type HostType {
  User
  Page
}

Constructors

  • User
  • Page
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(String),
  )
}

Constructors

  • OfflineLocation(
      latitude: Float,
      longitude: Float,
      country_code: Option(String),
    )
pub type OfflineType {
  Text
  Place
  City
}

Constructors

  • Text
  • Place
  • City
pub type OnlineType {
  MessengerRoom
  ThirdParty
  FacebookLive
  Other
}

Constructors

  • MessengerRoom
  • ThirdParty
  • FacebookLive
  • Other

Functions

pub fn construct_event_request_from_id(
  id: String,
  user_agent: String,
) -> Result(Request(String), 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(String), ScrapeError)

Construct the request for the event with a given URL.

pub fn get_event(
  html: String,
) -> Result(EventData, List(PropertyError))

Get all possible event data from the given HTML string.

pub fn scrape_event_response(
  resp: Response(String),
) -> Result(EventData, ScrapeError)

Scrape the event from the response received for a previously generated event request.

Search Document