0

Get Top Locations By Spam Classification

by
Published Nov 16, 2023

Get the top locations by emails classified as Spam or not.

Script cloudflare Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 403 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Get Top Locations By Spam Classification
8
 * Get the top locations by emails classified as Spam or not.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  spam: "SPAM" | "NOT_SPAM",
13
  limit: string | undefined,
14
  name: string | undefined,
15
  dateRange: string | undefined,
16
  dateStart: string | undefined,
17
  dateEnd: string | undefined,
18
  asn: string | undefined,
19
  location: string | undefined,
20
  arc: string | undefined,
21
  dkim: string | undefined,
22
  dmarc: string | undefined,
23
  spf: string | undefined,
24
  format: "JSON" | "CSV" | undefined
25
) {
26
  const url = new URL(
27
    `https://api.cloudflare.com/client/v4/radar/email/security/top/locations/spam/${spam}`
28
  );
29
  for (const [k, v] of [
30
    ["limit", limit],
31
    ["name", name],
32
    ["dateRange", dateRange],
33
    ["dateStart", dateStart],
34
    ["dateEnd", dateEnd],
35
    ["asn", asn],
36
    ["location", location],
37
    ["arc", arc],
38
    ["dkim", dkim],
39
    ["dmarc", dmarc],
40
    ["spf", spf],
41
    ["format", format],
42
  ]) {
43
    if (v !== undefined && v !== "") {
44
      url.searchParams.append(k, v);
45
    }
46
  }
47
  const response = await fetch(url, {
48
    method: "GET",
49
    headers: {
50
      "X-AUTH-EMAIL": auth.email,
51
      "X-AUTH-KEY": auth.key,
52
      Authorization: "Bearer " + auth.token,
53
    },
54
    body: undefined,
55
  });
56
  if (!response.ok) {
57
    const text = await response.text();
58
    throw new Error(`${response.status} ${text}`);
59
  }
60
  return await response.json();
61
}
62