scrapbook/page

Examples

You can use this module to create HTTP requests and parse HTTP responses, which allows you to use this module in all targets.

Event collection

import gleam/httpc
import gleam/io
import gleam/result
import scrapbook/page

fn event_collection() -> Result(page.PageEventCollection, Nil) {
  use req <- result.try(result.replace_error(
    page.construct_upcoming_event_collection_request_from_name(
      "CodeSyncGlobal",
      user_agent,
    ),
    Nil,
  ))
  use resp <- result.try(result.replace_error(httpc.send(req), Nil))
  result.replace_error(page.scrape_event_collection_response(resp), Nil)
}

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

Event collection items (events)

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

fn do_send_many(
  reqs: List(Request(String)),
  resps: List(Response(String)),
) -> Result(List(Response(String)), Nil) {
  case reqs {
    [] -> Ok(resps)
    [first, ..rest] -> {
      use resp <- result.try(result.replace_error(httpc.send(first), Nil))
      do_send_many(rest, [resp, ..resps])
    }
  }
}

fn send_many(reqs: List(Request(String))) -> Result(List(Response(String)), Nil) {
  do_send_many(reqs, [])
}

fn event_items(
  event_collection: page.PageEventCollection,
) -> Result(List(event.EventData), Nil) {
  use reqs <- result.try(result.replace_error(
    page.construct_event_items_requests(event_collection.items, user_agent),
    Nil,
  ))
  use resps <- result.try(send_many(reqs))
  result.replace_error(page.scrape_event_items_responses(resps), Nil)
}

pub fn main() {
  // First, get the page's event collection...

  let assert Ok(event_items) = event_items(event_collection)
  io.debug(event_items)
}

Types

pub type PageEventCollection {
  PageEventCollection(
    name: String,
    count: Int,
    items: List(PageEventItem),
  )
}

Constructors

  • PageEventCollection(
      name: String,
      count: Int,
      items: List(PageEventItem),
    )
pub type PageEventItem {
  PageEventItem(id: String, name: String, canceled: Bool)
}

Constructors

  • PageEventItem(id: String, name: String, canceled: Bool)

Functions

pub fn construct_event_collection_request_from_url(
  url: String,
  user_agent: String,
) -> Result(Request(String), ScrapeError)

Construct the request for the page’s event collection with a given URL.

pub fn construct_event_items_requests(
  items: List(PageEventItem),
  user_agent: String,
) -> Result(List(Request(String)), ScrapeError)

Construct the list of requests for the page’s event items.

pub fn construct_past_event_collection_request_from_name(
  name: String,
  user_agent: String,
) -> Result(Request(String), ScrapeError)

Construct the request for the page’s past events collection with a given name.

pub fn construct_upcoming_event_collection_request_from_name(
  name: String,
  user_agent: String,
) -> Result(Request(String), ScrapeError)

Construct the request for the page’s upcoming events collection with a given name.

pub fn get_event_collection(
  html: String,
) -> Result(PageEventCollection, List(PropertyError))

Get a collection of page event items from the given HTML string.

pub fn scrape_event_collection_response(
  resp: Response(String),
) -> Result(PageEventCollection, ScrapeError)

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

pub fn scrape_event_items_responses(
  resps: List(Response(String)),
) -> Result(List(EventData), ScrapeError)

Scrape the list of events from the responses received for previously generated page event item requests.

Search Document