This commit is contained in:
2026-01-24 14:00:49 -03:00
parent 7a4d94fbc4
commit 275a7600a3
6 changed files with 108 additions and 1 deletions

View File

@@ -1,2 +1,11 @@
# clima
# El clima de Oshitox
Producto oficial Sudestec.
## Instalar dependencias
- `npm i`
## Ejecutar Programa
- `npm start prod "Nombre de un lugar"`

20
package.json Normal file
View File

@@ -0,0 +1,20 @@
{
"name": "osito",
"version": "1.0.0",
"description": "",
"license": "ISC",
"author": "",
"type": "module",
"main": "out/index.ts",
"scripts": {
"dev": "node --env-file .env out/index.js",
"compile": "npx tsc",
"prod": "npx tsc && node --env-file .env out/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"devDependencies": {
"@types/node": "^25.0.10",
"csv-parser": "^3.2.0",
"typescript": "^5.9.3"
}
}

7
src/clima.ts Executable file
View 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
View 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
View 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}`;
}

19
tsconfig.json Normal file
View File

@@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "Node16",
"moduleResolution": "node16",
"target": "ES2024",
"jsx": "react-jsx",
"strictNullChecks": true,
"strictFunctionTypes": true,
"strict": true,
"sourceMap": true,
"sourceRoot": "src",
"outDir": "out"
},
"exclude": [
"out",
"node_modules",
"**/node_modules/*"
]
}