Skip to content

Commit b0b75f6

Browse files
authoredMay 2, 2024
fix(cool-links): restore video summarization functionality (#141)
1 parent bf3c336 commit b0b75f6

File tree

3 files changed

+38
-29
lines changed

3 files changed

+38
-29
lines changed
 

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"license": "MIT",
88
"scripts": {
99
"build": "tsup-node src/main.ts",
10-
"dev": "tsup-node src/main.ts --watch --on-success \"clear && node --enable-source-maps -r dotenv/config dist/main.js\"",
10+
"dev": "tsup-node src/main.ts --watch src --watch .env --watch pnpm-lock.yaml --on-success \"clear && node --enable-source-maps -r dotenv/config dist/main.js\"",
1111
"test": "vitest",
1212
"check:lint": "eslint src --report-unused-disable-directives",
1313
"check:format": "prettier -c src",
@@ -21,6 +21,7 @@
2121
"cron": "3.1.7",
2222
"discord.js": "14.14.1",
2323
"keyv": "4.5.4",
24+
"nanoid": "5.0.7",
2425
"open-graph-scraper": "6.5.1",
2526
"zod": "3.23.5"
2627
},

‎pnpm-lock.yaml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,39 @@
1+
import { nanoid } from 'nanoid';
2+
import { z } from 'zod';
3+
14
const baseUrl = 'https://www.summarize.tech/api/summary';
25

36
// Example usage:
47
// const videoUrl = 'https://www.youtube.com/watch?v=ruUlK6zRwS8';
58
// const summary = await getVideoSummary(videoUrl);
69
// console.log({ summary });
710

8-
type SummaryResponse = {
9-
rollups: Record<number, SummaryChunk>;
10-
title: string;
11-
};
12-
13-
type SummaryChunk = {
14-
children: Record<number, string>;
15-
summary: string;
16-
};
17-
18-
const makeId = (length: number) => {
19-
let result = '';
20-
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_';
21-
const charactersLength = characters.length;
22-
23-
for (let i = 0; i < length; i++)
24-
result += characters.charAt(Math.floor(Math.random() * charactersLength));
25-
26-
return result;
27-
};
11+
const summarySchema = z.object({
12+
title: z.string(),
13+
rollups: z.record(
14+
z.coerce.number().int().gte(0),
15+
z.object({
16+
children: z.record(z.coerce.number().int().gte(0), z.string()),
17+
summary: z.object({
18+
before: z.string(),
19+
keyword: z.string(),
20+
after: z.string(),
21+
}),
22+
}),
23+
),
24+
});
2825

29-
export const getVideoSummary = async (videoUrl: string) => {
30-
return await fetch(baseUrl, {
26+
export const getVideoSummary = async (videoUrl: string) =>
27+
fetch(baseUrl, {
3128
method: 'POST',
32-
body: JSON.stringify({ url: videoUrl, deviceId: makeId(21) }),
29+
body: JSON.stringify({ url: videoUrl, deviceId: nanoid(21) }),
3330
headers: { 'content-type': 'application/json' },
3431
})
3532
.then((res) => res.json())
36-
.then((res) =>
37-
(Object.values((res as SummaryResponse).rollups) ?? [])
38-
.map((chunk) => chunk.summary)
39-
.join(' '),
33+
.then((json) => summarySchema.parse(json))
34+
.then((result) =>
35+
Object.values(result.rollups)
36+
.map(({ summary }) => summary.before + summary.keyword + summary.after)
37+
.join('\n')
38+
.replace(/\. /g, '.\n'),
4039
);
41-
};

0 commit comments

Comments
 (0)
Please sign in to comment.