init
This commit is contained in:
7
src/clima.ts
Executable file
7
src/clima.ts
Executable file
@@ -0,0 +1,7 @@
|
||||
import { log } from "./logger.js"
|
||||
|
||||
export async function ubicacion(lugar: string, limit = '1') {
|
||||
const response = await fetch(`${process.env.WEATHER_URL}/geo/1.0/direct?q=${lugar}&limit=${limit}&appid=${process.env.WEATHER_API_KEY}`)
|
||||
log('GET', response.url, response.status)
|
||||
return response
|
||||
}
|
||||
15
src/index.ts
Normal file
15
src/index.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { ubicacion } from "./clima.js";
|
||||
|
||||
if (typeof process.env.WEATHER_URL === 'undefined' || typeof process.env.WEATHER_API_KEY === 'undefined' || typeof process.argv[2] === 'undefined') {
|
||||
console.error('WEATHER_URL', typeof process.env.WEATHER_URL)
|
||||
console.error('WEATHER_API_KEY', typeof process.env.WEATHER_API_KEY)
|
||||
console.error('Ciudad', typeof process.argv[2])
|
||||
process.exit(0);
|
||||
|
||||
}
|
||||
|
||||
const buqueda = await ubicacion(process.argv[2])
|
||||
|
||||
console.log(buqueda.status, buqueda.statusText)
|
||||
console.log('url', buqueda.url)
|
||||
console.log('Body', await buqueda.json())
|
||||
37
src/logger.ts
Normal file
37
src/logger.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { appendFileSync, existsSync } from 'fs';
|
||||
import { createWriteStream } from 'fs';
|
||||
|
||||
const filename = 'logs.csv',
|
||||
exist = existsSync(filename);
|
||||
|
||||
console.log(filename, exist);
|
||||
|
||||
if (!exist) {
|
||||
|
||||
appendFileSync(filename, ['Date', 'Method', 'Url', 'Status'].join(',') + '\n', 'utf8');
|
||||
}
|
||||
|
||||
export const log = (method: string, url: string, status: number) => {
|
||||
appendFileSync(filename, [isoDateInTimeZone(), method, url, status].join(',') + '\n', 'utf8');
|
||||
}
|
||||
|
||||
function isoDateInTimeZone(timeZone = 'America/Buenos_Aires', date = new Date()) {
|
||||
const parts = new Intl.DateTimeFormat("en-CA", {
|
||||
timeZone,
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
fractionalSecondDigits: 3,
|
||||
hour12: false,
|
||||
timeZoneName: "shortOffset",
|
||||
}).formatToParts(date),
|
||||
map = Object.fromEntries(parts.map(p => [p.type, p.value])),
|
||||
offset = map.timeZoneName.replace("GMT", "");
|
||||
|
||||
return `${map.year}-${map.month}-${map.day}T` +
|
||||
`${map.hour}:${map.minute}:${map.second}.` +
|
||||
`${map.fractionalSecond}${offset}`;
|
||||
}
|
||||
Reference in New Issue
Block a user