-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
42 lines (36 loc) · 1.24 KB
/
index.js
File metadata and controls
42 lines (36 loc) · 1.24 KB
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
// Import the required libraries
import { getAssetFromKV } from '@cloudflare/kv-asset-handler'
import { parse } from 'json2csv'
// Define the KV namespace
const BUCKET = KV_NAMESPACE
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
try {
// Fetch the JSON data
const response = await fetch('https://he.net/3d-map/he-pops.js')
const data = await response.json()
// Flatten the data and convert it to CSV
const flattenedData = Object.entries(data).map(([id, details]) => ({
id,
lat: details.latlon.lat,
lon: details.latlon.lon,
city: details.city,
name: details.name,
country: details.country,
pop: details.pop,
street: details.street,
exchanges: details.exchanges.join(', '),
}))
const csv = parse(flattenedData)
// Store the CSV in the KV namespace
await BUCKET.put('he_pops.csv', csv)
// Return a success response
return new Response('CSV stored successfully', { status: 200 })
} catch (error) {
// Log the error and return a response with the error message and a 500 status code
console.error(error)
return new Response(`Error: ${error.message}`, { status: 500 })
}
}