Readme

Njuškalo Crawler Admin

Admin frontend for Njuškalo Crawler Admin

  • use + and ' to add/remove search strings
  • enter password next to submit button
  • check that you have set up your crawler correctly
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
79
80
81
82
83
84
85
86
87
88
89
90
91
import { njuskaloCreateHtmlResponse } from "https://esm.town/v/kajgod/njuskaloCreateHtmlResponse";
import { set } from "https://esm.town/v/std/set?v=11";
import process from "node:process";
import { njuskaloData } from "https://esm.town/v/kajgod/njuskaloData";
import { njuskaloCreateDataWithInterfaces } from "https://esm.town/v/kajgod/njuskaloCreateDataWithInterfaces";
export async function njuskaloIndex(req: express.Request, res: express.Response) {
let len = 3; // default number of elements, can be overrun on frontend
let keywords: string[] = [];
let { data, emptyDetails, fillEmpty, newKeywords, defaultData } =
njuskaloCreateDataWithInterfaces(
njuskaloData,
);
// =========================================================================
// 1. check incomming values
const k: unknown = req.query.keywords;
if (
Array.isArray(k) &&
k.every((i) => typeof i === "string")
)
keywords = req.query.keywords as string[];
const password = req.query.password;
// =========================================================================
// 2. updating permanent storage
switch (true) {
case (keywords.length > 0): // update: new healthy data from server
if (password !== process.env.njuskalo_password)
return res.send(`
<p>ERROR: wrong password!</p>
<p>You can only change search values if you setup "njuskalo_password" in profile>secrets in your val.town account.</p>
<p>Go back and try again.</p>
`);
const { data: d, len: l } = newKeywords(keywords);
data = d;
len = l;
break;
case (!njuskaloData?.keywords): // init: no data in storage
data = fillEmpty(len);
break;
default: // default: read
data = defaultData;
}
// =========================================================================
// 3. display current data in HTML form
const formData = data.keywords.reduce((acc, v, i) =>
acc + `
<span id="b${i}" class="inputs">
<br>
<input type="text" value="${v}" name="keywords[]"/>
<input type="button" value="-" onclick="removeMe('b${i}')"/>
</span>
`, "");
const tableData = data.keywords.reduce((agg, keyword, i) => {
const { title = "", price = "", url = "" } = data.sentData[i];
return agg += `
<tr>
<td>${keyword}</td>
<td>${title}</td>
<td>${price}</td>
<td>${url}</td>
</tr>
`;
}, "");
const formHtml = `
<form action="?" method="get">
<span id="inputs-container">
${formData}
</span>
<input type="button" value="+" onclick="addNew()"/>
<br><br>
<input type="password" name="password"/>
<input type="submit" value="Submit"><br>
</form>`;
const dataHtml = `
<hr>
<h2>Latest offers:</h2>
<table border="1">
<thead>
<tr>
<th>Keywords</th>
<th>Title</th>
<th>Price</th>
<th>URL</th>
</tr>
</thead>
${tableData}
</table>
`;
await set("njuskaloData", njuskaloData);
return njuskaloCreateHtmlResponse(res, formHtml, dataHtml);
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
kajgod-njuskaloindex.express.val.run
October 23, 2023