forked from ndom91/briefkasten-scrape
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
78 lines (66 loc) · 2.13 KB
/
index.js
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
70
71
72
73
74
75
76
77
78
import { fetchImage, uploadImage, getTime } from './lib.js'
import * as pg from 'pg'
const { Client } = pg.default
;(async () => {
let client
try {
client = new Client({
connectionString: process.env.DATABASE_URL,
})
await client.connect()
// Fetch the first 5 Bookmarks with missing imageUrls
const { rows } = await client.query(
`SELECT id, url, "userId"
FROM "Bookmark"
WHERE image IS NULL
OR image NOT LIKE $1
LIMIT $2`,
[
process.env.SUPABASE_URL + '%',
process.env.BOOKMARKS_CHUNK ? parseInt(process.env.BOOKMARKS_CHUNK) : 5,
]
)
if (rows.length === 0) {
// No more bookmarks with missing imageUrls found, exit 0
console.log(`[${getTime()}] No more bookmarks with missing images found.`)
process.exit(0)
}
console.log(`[${getTime()}] Fetched bookmarks`)
console.table(rows)
// For each row, i.e. bookmark, visit the URL with Playwright and
// capture a screenshot. Then upload that screenshot to Imagekit
for (const row of rows) {
console.log(`\n[${getTime()}] Attempting URL: ${row.url}`)
const { id, url, userId } = row
const imageBuffer = await fetchImage(url, id)
if (imageBuffer) {
const { imageUrl, imageBlur } = await uploadImage(
userId,
imageBuffer,
Date.now()
)
console.log(`[${getTime()}] Uploaded image: ${imageUrl}`)
const updateRes = await client.query(
`UPDATE "Bookmark" SET image = $1, "imageBlur" = $2 WHERE id = $3 AND "userId" = $4`,
[imageUrl, imageBlur, id, userId]
)
if (updateRes.rowCount === 1) {
console.log(
`[${getTime()}] Successfully updated ${
new URL(url)?.hostname ?? ''
} (${id})`
)
}
}
}
console.log(`\n[${getTime()}] Successfully finished job`)
// Finished all fetched images, exit 0
process.exit(0)
} catch (e) {
// Error fetching and uploading images, exit 1
console.error(`[${getTime()}] Error`, e)
process.exit(1)
} finally {
await client.end()
}
})()