-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrends_finder.ts
47 lines (38 loc) · 1.03 KB
/
trends_finder.ts
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
import googleTrends from "npm:google-trends-api";
interface TrendingSearch {
title: { query: string };
articles: { title: string }[];
}
interface TrendingSearchesDay {
trendingSearches: TrendingSearch[];
}
interface GoogleTrendsResponse {
trendingSearchesDays: TrendingSearchesDay[];
}
export interface Filter {
geo: string;
}
export interface TrendArticle {
title: string;
}
export interface Trend {
query: string;
articles: TrendArticle[];
}
export class TrendFinder {
async find(filter: Filter): Promise<Trend[]> {
const response = await googleTrends.dailyTrends({ geo: filter.geo });
const parsed = JSON.parse(response).default as GoogleTrendsResponse;
return parsed.trendingSearchesDays
.flatMap((t) => t.trendingSearches)
.map((t) => ({
query: t.title.query,
articles: t.articles.map((a) => ({ title: a.title })),
}));
}
}
Deno.test(async function testClient() {
const client = new TrendFinder();
const trends = await client.find({ geo: "US" });
console.log(trends);
});