1
2import { HttpCrawler, KeyValueStore, log } from "crawlee";
3import { Actor } from "apify";
4
5import { router } from "./routes.js";
6
7await Actor.init();
8
9interface InputSchema {
10 debug?: boolean;
11 searchStrings: string[];
12}
13
14let debug, searchStrings;
15
16const input = await KeyValueStore.getInput<InputSchema>();
17
18debug = input?.debug;
19searchStrings = input?.searchStrings;
20
21
22if (!searchStrings) {
23 const input = await Actor.getInput<InputSchema>();
24
25 debug = input?.debug;
26 searchStrings = input?.searchStrings;
27}
28
29if (!(Array.isArray(searchStrings) && searchStrings.length > 0)) {
30 throw new Error('Wrong INPUT: searchStrings has to be an array with at least one text');
31}
32
33if (debug) {
34 log.setLevel(log.LEVELS.DEBUG);
35}
36
37const proxyConfiguration = await Actor.createProxyConfiguration({
38 useApifyProxy: true,
39});
40
41const crawler = new HttpCrawler({
42 proxyConfiguration,
43 navigationTimeoutSecs: 60 * 5,
44 requestHandler: router,
45 errorHandler: async ({ response }) => {
46 log.error(`Response status is: ${response?.statusCode} msg: ${response?.statusMessage}`);
47 },
48});
49
50await crawler.run(
51 searchStrings.map(searchString => {
52 log.info(`Generating images for ${searchString}...`);
53
54 return {
55 url: "https://backend.craiyon.com/generate",
56 method: "POST",
57 payload: JSON.stringify({ prompt: searchString.toLowerCase() }),
58 headers: {
59 "content-type": "application/json",
60 },
61 useExtendedUniqueKey: true,
62 userData: {
63 searchString
64 }
65 }
66 })
67);
68
69await Actor.exit();