File size: 1,224 Bytes
676fc08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { generateSignature } from "@/utils/signature";
import { omit } from "radash";

interface CrawlerResult {
  url: string;
  title: string;
  content: string;
}

interface ReaderResult extends CrawlerResult {
  warning?: string;
  title: string;
  description: string;
  url: string;
  content: string;
  usage: {
    tokens: number;
  };
}

export async function jinaReader(url: string) {
  const response = await fetch("https://r.jina.ai", {
    method: "POST",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ url }),
  });

  const { data }: { data: ReaderResult } = await response.json();
  if (data.warning) {
    throw new Error(data.warning);
  }
  return omit(data, ["usage", "description"]) as CrawlerResult;
}

export async function localCrawler(url: string, password: string) {
  const accessKey = generateSignature(password, Date.now());
  const response = await fetch("/api/crawler", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${accessKey}`,
    },
    body: JSON.stringify({ url }),
  });
  const result: CrawlerResult = await response.json();
  return result;
}