Powered by AppSignal & Oban Pro

Untitled notebook

screenshot_analysis.livemd

Untitled notebook

Mix.install([
  {:playwright, "~> 1.49.1-alpha.2"},
])

Make sure node and npx are available

home = System.user_home!()
asdf_shims =
  System.get_env("ASDF_DATA_DIR") || Path.join([home, ".asdf", "shims"])

new_path =
  [System.get_env("PATH") || "", asdf_shims]
  |> Enum.reject(&(&1 in [nil, ""]))
  |> Enum.join(":")

if String.contains?(System.get_env("PATH"), ".asdf/shims"), do: :ok, else: System.put_env("PATH", new_path)
base = Path.join(System.tmp_dir!(), "playwright-cache")
File.mkdir_p!(base)

if !System.get_env("PLAYWRIGHT_BROWSERS_PATH") do
  System.put_env(%{
    "PLAYWRIGHT_BROWSERS_PATH" => Path.join(base, "browsers"),
    "XDG_CACHE_HOME" => Path.join(base, ".cache")
  })
end

{System.get_env("PLAYWRIGHT_BROWSERS_PATH"), System.get_env("XDG_CACHE_HOME")}
home = System.user_home!()
asdf_dir = System.get_env("ASDF_DIR") || Path.join(home, ".asdf")

path =
  Enum.join([
    Path.join(asdf_dir, "bin"), 
    Path.join(asdf_dir, "shims"),
    System.get_env("PATH") || ""
  ], ":")

if !System.get_env("ASDF_DIR") do
System.put_env(%{
  "ASDF_DIR" => asdf_dir,
  "PATH" => path
})
end

# sanity checks
{System.find_executable("asdf"),
 System.find_executable("node"),
 System.find_executable("npx")}
if !System.get_env("PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD") do
  Mix.Task.rerun("playwright.install")
  System.put_env(%{
    "PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD" => "1",
  })
end 
defmodule SimpleShot do
  alias Playwright.{Browser, BrowserContext, Page}

  @doc """
  Screenshot `url` to `out_path`.

  Options:
    * :full_page (boolean) – default true
    * :viewport (map) – %{width: 1440, height: 900}
  """
  def run(url, out_path, opts \\ []) when is_binary(url) and is_binary(out_path) do
    full_page = Keyword.get(opts, :full_page, true)
    viewport  = Keyword.get(opts, :viewport, %{width: 1440, height: 900})

    {:ok, browser} = Playwright.launch(:firefox)

    try do
      context = Browser.new_context(browser, %{viewport: viewport})
      page    = BrowserContext.new_page(context)

      _resp = Page.goto(page, url, %{wait_until: "load", timeout: 60_000})

      png_b64 = Page.screenshot(page, %{full_page: full_page})
      File.mkdir_p!(Path.dirname(out_path))
      File.write!(out_path, Base.decode64!(png_b64))
      {:ok, out_path}
    after
      :ok = Browser.close(browser)
    end
  end
end
SimpleShot.run("https://www.bbc.co.uk/news/articles/cp3q74v7gz7o", "/Users/dimova01/Desktop/test.png")
SimpleShot.run("https://www.bbc.co.uk/news/articles/cp3q74v7gz7o", "/Users/dimova01/Desktop/test1.png",
               full_page: false, viewport: %{width: 1280, height: 800})
{:ok, browser} = Playwright.launch(:firefox)
width = 1440
height = 900
p12 = ""
pass = ""
url = ""
context = Playwright.Browser.new_context(browser, %{
    ignore_https_errors: true,
    client_certificates: [
      %{
        origin: "https://pagemon.tools.bbc.co.uk/",
        pfx_path: p12,
        passphrase: pass
      }
    ],
    viewport: %{width: 1440, height: 900}
  })
page = Playwright.BrowserContext.new_page(context)
{:ok, state} = Playwright.Page.set_viewport_size(page, %{width: width, height: height})
IO.puts(inspect(state))
resp = Playwright.Page.goto(page, url)
IO.inspect(resp)
:ok = Playwright.Browser.close(browser)