File size: 1,680 Bytes
dc89ab8
4b45b50
dc89ab8
 
 
 
8aad435
dc89ab8
 
4b45b50
 
 
 
 
 
 
8aad435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b45b50
8aad435
 
dc89ab8
4b45b50
8aad435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc89ab8
4b45b50
 
8aad435
4b45b50
9f5528c
4b45b50
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import PQueue from "https://deno.land/x/p_queue@1.0.1/mod.ts"
import { Application, Router } from "https://deno.land/x/oak@v9.0.0/mod.ts";

import * as CSV from './csv.ts';
import Puppet from './puppet.ts';
import selectors from './selectors.ts';
import { fetch_logos } from './logos.ts';

const puppet = new Puppet();
const app = new Application();
const router = new Router();

const stats = {
  in_flight: 0,
  done: 0
}

export const DEFAULT_VIEWPORTS = [{
  width: 640,
  height: 480
}, {
  width: 1080,
  height: 800
}, {
  width: 640,
  height: 640
}, {
  width: 600,
  height: 800,
  hasTouch: true,
  isMobile: true
}]

router.post('/screenshot', async (ctx) => {
  const { request, response } = ctx;
  const { url, path = './debug.png', logos = false, viewports = DEFAULT_VIEWPORTS } = await request.body().value;

  stats.in_flight++;
  let i = 0, v = {};
  viewports.map(async (v, i) => {
    const ret = await puppet.run(async page => {
      await page.setViewport(v)
      console.error('running', url, stats, v)
      try {
        const npath = path.replace('.png', `.${i}.png`)

        await page.goto(url, { waitUntil: 'networkidle2', timeout: 60000 })
        await page.screenshot({ path: npath, fullPage: true })
        if (logos) {
          await fetch_logos(page, npath)
        }
        console.error(`screenshot ${v} / ${i} ok: ${path}`)
      } catch (e) {
        console.error(e)
      }
      return { response: 'ok' }
    })
  })
  stats.in_flight--;
  stats.done++
  response.body = { response: 'ok' }
})

app.use(router.routes())
app.use(router.allowedMethods())
const addr = '0.0.0.0:8000'
console.error(`listen on ${addr}`)
app.listen(addr)