Skip to content

Commit 933ad99

Browse files
committed
Fix and add cron
1 parent dd122a3 commit 933ad99

File tree

5 files changed

+61
-7
lines changed

5 files changed

+61
-7
lines changed

app/api/crons/refresh/route.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import fetch from "@/lib/data";
2+
import { db } from "@/lib/db/connection";
3+
import { reify } from "@/lib/db/domains";
4+
import { sql } from "kysely";
5+
import pino from "pino";
6+
7+
const logger = pino({
8+
name: "cron-refresh",
9+
});
10+
11+
const RAW_QUERY = sql<{
12+
domain: string;
13+
}>`
14+
select domain from tranco TABLESAMPLE system (0.01)
15+
`;
16+
17+
const getRandomDomains = async () => {
18+
const domains = await RAW_QUERY.execute(db);
19+
return domains.rows.map((row) => row.domain);
20+
};
21+
22+
const MAXIMUM_DOMAINS = 10;
23+
24+
export async function GET(
25+
request: Request,
26+
context: {
27+
params: {
28+
domain: string;
29+
};
30+
},
31+
) {
32+
const domains = await getRandomDomains();
33+
const selectedDomains = domains.slice(0, MAXIMUM_DOMAINS);
34+
await Promise.all(selectedDomains.map(async (domain) => {
35+
logger.info({
36+
message: "refresh.started",
37+
domain: domain,
38+
});
39+
const rawResponse = await fetch(domain);
40+
await reify(domain, rawResponse);
41+
}));
42+
43+
return Response.json({
44+
domains: selectedDomains,
45+
});
46+
}

app/api/domains/[domain]/route.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export async function GET(
2020
) {
2121
const rawResponse = await fetch(context.params.domain);
2222
await reify(context.params.domain, rawResponse);
23-
console.log(rawResponse.detected_technologies);
2423

2524
return Response.json({
2625
domain: context.params.domain,

app/technology/[identifier]/page.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,15 @@ export default async function TechnologyPage({
8080
const trancoCount = process.env.DISABLE_DATABASE
8181
? 0
8282
: await db
83-
.selectFrom("tranco")
83+
.selectFrom("affiliations")
8484
.innerJoin(
8585
"detected_technologies",
86-
"tranco.domain",
86+
"affiliations.domain",
8787
"detected_technologies.domain"
8888
)
8989
.where("detected_technologies.technology", "=", params.identifier)
90-
.select(db.fn.count("tranco.domain").as("count"))
90+
.where("affiliations.identifier", "=", "tranco")
91+
.select(db.fn.count("affiliations.domain").as("count"))
9192
.executeTakeFirst()
9293
.then((result) => Number(result?.count || 0));
9394

lib/parsers/html.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const GENERIC_SOCIAL_MEDIA_PROVIDER = (html: string) => {
2828
const username = match[match.length - 1];
2929
return [
3030
{
31-
identifier: potentialMatch?.identifier,
31+
identifier: potentialMatch?.identifier as string,
3232
metadata: {
3333
username: username.split("?")[0],
3434
},
@@ -119,7 +119,7 @@ const JSONLD_RULE = (html: string) => {
119119
);
120120
if (service) {
121121
return {
122-
identifier: service.identifier.split("?")[0],
122+
identifier: service.identifier.split("?")[0] as string,
123123
metadata: {
124124
username: url.split("/").pop() || "",
125125
},
@@ -199,7 +199,7 @@ const SUBDOMAIN_RULE = (html: string, domain: string) => {
199199

200200
const RULES: ((html: string, domain: string) => DetectedTechnology[])[] = [
201201
...Object.values(REGISTRY).map((service) => {
202-
return (html: string, domain: string) => {
202+
return (html: string) => {
203203
const potentialMatches = service.substrings?.filter((substring) =>
204204
html.includes(substring),
205205
);

vercel.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"crons": [
3+
{
4+
"path": "/api/crons/refresh",
5+
"schedule": "* * * * *"
6+
}
7+
]
8+
}

0 commit comments

Comments
 (0)