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 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[] }[];
const [cachedResults, cachedList] = await Promise.all([
blob.getJSON(cacheKey),
blob.list(BLOB_KEY),
]);
if (cachedResults) {
sites = cachedResults;
} else {
}
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