Skip to content

Commit cdb1dbc

Browse files
committed
fix: screenshot command
1 parent a4a2a66 commit cdb1dbc

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

src/commands/scrape.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import fs from 'fs';
12
import {Command} from 'commander';
23
import {post} from '../utils/client';
34
import {ensure_authenticated} from '../utils/auth';
45
import {resolve} from '../utils/config';
56
import {start as start_spinner} from '../utils/spinner';
6-
import {print, success, fail} from '../utils/output';
7+
import {print, success, fail, info} from '../utils/output';
78
import type {
89
Scrape_format,
910
Scrape_request,
@@ -54,11 +55,11 @@ const handle_scrape = async(url: string, opts: Scrape_opts)=>{
5455
const req = build_request(url, zone, fmt, opts);
5556
const spinner = start_spinner(`Scraping ${url}...`);
5657
try {
57-
const result = await post<string|Scrape_response_json|Scrape_async_response>(
58+
const result = await post<string|Buffer|Scrape_response_json|Scrape_async_response>(
5859
api_key,
5960
ENDPOINT,
6061
req,
61-
{timing: opts.timing}
62+
{timing: opts.timing, raw_buffer: fmt == 'screenshot'}
6263
);
6364
spinner.stop();
6465
if (opts.async)
@@ -67,6 +68,13 @@ const handle_scrape = async(url: string, opts: Scrape_opts)=>{
6768
success(`Async job submitted. Response ID: ${async_res.response_id}`);
6869
return;
6970
}
71+
if (fmt == 'screenshot')
72+
{
73+
const out = opts.output ?? 'screenshot.png';
74+
fs.writeFileSync(out, result as Buffer);
75+
info(`Output written to ${out}`);
76+
return;
77+
}
7078
const print_opts = {json: opts.json, pretty: opts.pretty, output: opts.output};
7179
if (fmt == 'json')
7280
{

src/utils/client.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Request_opts = {
1616
body?: unknown;
1717
headers?: Record<string, string>;
1818
timing?: boolean;
19+
raw_buffer?: boolean;
1920
};
2021

2122
type Api_error = {
@@ -66,6 +67,9 @@ const request = async<T = unknown>(
6667
}
6768
if (res.ok)
6869
{
70+
if (opts.raw_buffer)
71+
return Buffer.from(
72+
await res.arrayBuffer()) as unknown as T;
6973
const content_type = res.headers.get('content-type') ?? '';
7074
if (content_type.includes('application/json'))
7175
return await res.json() as T;

0 commit comments

Comments
 (0)