From 275a7600a3114518fe9034204fbab7b4d476ab1a Mon Sep 17 00:00:00 2001 From: Facundo Redon Date: Sat, 24 Jan 2026 14:00:49 -0300 Subject: [PATCH] init --- README.md | 11 ++++++++++- package.json | 20 ++++++++++++++++++++ src/clima.ts | 7 +++++++ src/index.ts | 15 +++++++++++++++ src/logger.ts | 37 +++++++++++++++++++++++++++++++++++++ tsconfig.json | 19 +++++++++++++++++++ 6 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 package.json create mode 100755 src/clima.ts create mode 100644 src/index.ts create mode 100644 src/logger.ts create mode 100644 tsconfig.json diff --git a/README.md b/README.md index 8bcc52f..fe04a5d 100644 --- a/README.md +++ b/README.md @@ -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"` diff --git a/package.json b/package.json new file mode 100644 index 0000000..5053261 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/src/clima.ts b/src/clima.ts new file mode 100755 index 0000000..1db323d --- /dev/null +++ b/src/clima.ts @@ -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 +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..586acda --- /dev/null +++ b/src/index.ts @@ -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()) diff --git a/src/logger.ts b/src/logger.ts new file mode 100644 index 0000000..9608a26 --- /dev/null +++ b/src/logger.ts @@ -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}`; +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..c9235e5 --- /dev/null +++ b/tsconfig.json @@ -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/*" + ] +} \ No newline at end of file