Files
battery-heater/functions/aemet/src/main.ts

31 lines
1.1 KiB
TypeScript

import { Client, Users } from 'node-appwrite';
import { XMLParser } from 'fast-xml-parser';
// This Appwrite function will be executed every time your function is triggered
export default async ({ req, res, log, error }: any) => {
// You can use the Appwrite SDK to interact with other services
// For this example, we're using the Users service
const client = new Client()
.setEndpoint(process.env.APPWRITE_FUNCTION_API_ENDPOINT ?? '')
.setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID ?? '')
.setKey(req.headers['x-appwrite-key'] ?? '');
const users = new Users(client);
try {
const response = await fetch('https://www.aemet.es/xml/municipios_h/localidad_h_31019.xml');
if (!response.ok) {
throw new Error(`Error! status: ${response.status}`);
}
const parser = new XMLParser();
let json = parser.parse(await response.text());
log(json)
return res.json({sucess: false, data: json})
} catch(err: any) {
error("Error fetching forecast: " + err.message);
return res.json({success: false, error: err.message})
}
};