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
92
93
94
95
96
97
98
99
100
/** @jsxImportSource npm:hono/jsx */
import Exa from "https://esm.sh/exa-js";
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo?v=29";
import { blob } from "https://esm.town/v/std/blob";
import { stripHTML } from "https://esm.town/v/stevekrouse/stripHTML";
import { Hono } from "npm:hono";
// const exaSecret = Deno.env.get("EXA_API_KEY");
// const exa = new Exa(exaSecret);
const app = new Hono();
const BLOB_KEY = "exa-similar-";
function safeHost(url: string): string {
const formattedUrl = url.startsWith("http://") || url.startsWith("https://") ? url : `http://${url}`;
const urlObject = new URL(formattedUrl);
return urlObject.hostname;
}
app.get("/", async (c) => {
try {
const siteUrl = safeHost(c.req.query("url") || "todepond.com");
const cacheKey = `${BLOB_KEY}${siteUrl}`;
let sites: { score: number; url: string; highlights: string[] }[];
// Try to get cached results
const [cachedResults, cachedList] = await Promise.all([
blob.getJSON(cacheKey),
blob.list(BLOB_KEY),
]);
if (cachedResults) {
sites = cachedResults;
} else {
// If not cached, fetch new results
// const results = await exa.findSimilarAndContents(siteUrl, {
// highlights: { numSentences: 1, query: "Summary of the person" },
// });
// sites = results.results.map(x => {
// const u = new URL(x.url);
// return { ...x, url: u.host };
// }).filter(x => !x.url.includes(sitexUrl));
// // Cache the results
// await blob.setJSON(cacheKey, sites);
}
// Helper function to truncate text
const truncate = (text: string, maxLength: number) =>
text.length > maxLength ? text.slice(0, maxLength - 1) + "…" : text;
return c.html(
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Similar Sites Finder</title>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet" />
</head>
<body class="bg-gray-100 p-6">
<div class="max-w-4xl mx-auto bg-white p-8 rounded-lg shadow-md">
<h1 class="text-2xl font-bold mb-4">Find your internet twin!</h1>
<p class="mb-4 bg-red-300 font-bold p-2 rounded-md">
This site is currently broken for new queries. It seems like some bot hit it and we ran out of exa
credits.
</p>
<p class="mb-4">
Enter your personal website to find others like you. Powered by{" "}
<a class="text-blue-500 hover:underline" href="https://docs.exa.ai/reference/findsimilar">
Exa's "find similar"
</a>.{" "}
<a class="text-blue-500 hover:underline" href={extractValInfo(import.meta.url).htmlUrl}>
Built on Val Town.
</a>
</p>
<form method="GET" class="mb-6">
<input
type="text"
name="url"
placeholder="Enter a URL"
value={siteUrl}
required
class="border border-gray-300 p-2 rounded w-full"
/>
<button type="submit" class="mt-2 bg-blue-500 text-white p-2 rounded">Find Similar Sites</button>
</form>
<h2 class="text-xl font-semibold mb-2">Similar Sites:</h2>
<div>
{sites.map(site => (
<div class="mb-4">
<div class="flex justify-between items-center">
<span>
<a href={"https://" + site.url} target="_blank" class="text-blue-600 hover:underline">
{safeHost(site.url)}
</a>{" "}
<span class="text-xs text-gray-700">{Math.round(site.score * 100)}% match</span>
</span>
<a href={`?url=${encodeURIComponent(site.url)}`} class="ml-4 text-sm text-blue-500 hover:underline">
More like this