1 | |
2 | type Pinterest = { |
3 | token: string; |
4 | }; |
5 | |
6 | * List trending keywords |
7 | * Get the top trending search keywords among the Pinterest user audience. |
8 | */ |
9 | export async function main( |
10 | auth: Pinterest, |
11 | region: |
12 | | "US" |
13 | | "CA" |
14 | | "DE" |
15 | | "FR" |
16 | | "ES" |
17 | | "IT" |
18 | | "DE+AT+CH" |
19 | | "GB+IE" |
20 | | "IT+ES+PT+GR+MT" |
21 | | "PL+RO+HU+SK+CZ" |
22 | | "SE+DK+FI+NO" |
23 | | "NL+BE+LU" |
24 | | "AR" |
25 | | "BR" |
26 | | "CO" |
27 | | "MX" |
28 | | "MX+AR+CO+CL" |
29 | | "AU+NZ", |
30 | trend_type: "growing" | "monthly" | "yearly" | "seasonal", |
31 | interests: string | undefined, |
32 | genders: string | undefined, |
33 | ages: string | undefined, |
34 | include_keywords: string | undefined, |
35 | normalize_against_group: string | undefined, |
36 | limit: string | undefined, |
37 | ) { |
38 | const url = new URL( |
39 | `https://api.pinterest.com/v5/trends/keywords/${region}/top/${trend_type}`, |
40 | ); |
41 | for (const [k, v] of [ |
42 | ["interests", interests], |
43 | ["genders", genders], |
44 | ["ages", ages], |
45 | ["include_keywords", include_keywords], |
46 | ["normalize_against_group", normalize_against_group], |
47 | ["limit", limit], |
48 | ]) { |
49 | if (v !== undefined && v !== "" && k !== undefined) { |
50 | url.searchParams.append(k, v); |
51 | } |
52 | } |
53 | const response = await fetch(url, { |
54 | method: "GET", |
55 | headers: { |
56 | Authorization: "Bearer " + auth.token, |
57 | }, |
58 | body: undefined, |
59 | }); |
60 | if (!response.ok) { |
61 | const text = await response.text(); |
62 | throw new Error(`${response.status} ${text}`); |
63 | } |
64 | return await response.json(); |
65 | } |
66 |
|