Compare commits

...

5 commits

Author SHA1 Message Date
e376be121a lpm - usar revision id 2024-09-02 11:32:46 -03:00
5a79372cd8 separar en paquete ckan 2024-09-02 09:53:52 -03:00
8d9b1139fe mover a sepa/ 2024-09-02 09:52:16 -03:00
56cd3d87a7 usar curll lmfaooooo 2024-09-02 09:39:39 -03:00
9c14f98c5c tmate 2024-09-02 09:31:32 -03:00
21 changed files with 270 additions and 59 deletions

View file

@ -15,10 +15,10 @@ jobs:
- uses: oven-sh/setup-bun@v2 - uses: oven-sh/setup-bun@v2
with: with:
bun-version: latest bun-version: latest
- name: Setup tmate session # - name: Setup tmate session
uses: mxschmitt/action-tmate@v3 # uses: mxschmitt/action-tmate@v3
with: # with:
limit-access-to-actor: true # limit-access-to-actor: true
- name: Run archiver script - name: Run archiver script
env: env:
@ -27,6 +27,6 @@ jobs:
B2_BUCKET_KEY_ID: ${{ secrets.B2_BUCKET_KEY_ID }} B2_BUCKET_KEY_ID: ${{ secrets.B2_BUCKET_KEY_ID }}
B2_BUCKET_KEY: ${{ secrets.B2_BUCKET_KEY }} B2_BUCKET_KEY: ${{ secrets.B2_BUCKET_KEY }}
run: | run: |
cd sepa-precios-archiver cd sepa/sepa-precios-archiver
bun install --frozen-lockfile bun install --frozen-lockfile
bun index.ts bun index.ts

Binary file not shown.

View file

@ -1,17 +0,0 @@
import { z } from "zod";
export const zDatasetInfo = z.object({
metadata_modified: z.coerce.date(),
metadata_created: z.coerce.date(),
resources: z.array(
z.object({
id: z.string(),
size: z.number(),
format: z.string(),
created: z.coerce.date(),
url: z.string(),
modified: z.coerce.date().optional(),
description: z.string(),
}),
),
});

Binary file not shown.

BIN
sepa/bun.lockb Executable file

Binary file not shown.

15
sepa/ckan/README.md Normal file
View file

@ -0,0 +1,15 @@
# ckan
To install dependencies:
```bash
bun install
```
To run:
```bash
bun run index.ts
```
This project was created using `bun init` in bun v1.1.26. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.

13
sepa/ckan/package.json Normal file
View file

@ -0,0 +1,13 @@
{
"name": "ckan",
"type": "module",
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"zod": "^3.23.8"
}
}

19
sepa/ckan/schemas.ts Normal file
View file

@ -0,0 +1,19 @@
import { z } from "zod";
export const zResource = z.object({
id: z.string(),
revision_id: z.string(),
size: z.number(),
format: z.string(),
created: z.coerce.date(),
url: z.string(),
modified: z.coerce.date().optional(),
description: z.string(),
});
export type Resource = z.infer<typeof zResource>;
export const zDatasetInfo = z.object({
metadata_modified: z.coerce.date(),
metadata_created: z.coerce.date(),
resources: z.array(zResource),
});
export type DatasetInfo = z.infer<typeof zDatasetInfo>;

9
sepa/package.json Normal file
View file

@ -0,0 +1,9 @@
{
"name": "sepa",
"private": true,
"workspaces": [
"sepa-precios-archiver",
"sepa-precios-importer",
"ckan"
]
}

View file

@ -1,5 +1,5 @@
import { z } from "zod"; import { z } from "zod";
import { zDatasetInfo } from "./schemas"; import { zDatasetInfo } from "ckan/schemas";
import { mkdtemp, writeFile, readdir, mkdir, rm } from "fs/promises"; import { mkdtemp, writeFile, readdir, mkdir, rm } from "fs/promises";
import { basename, extname, join } from "path"; import { basename, extname, join } from "path";
import { $, write } from "bun"; import { $, write } from "bun";
@ -33,10 +33,7 @@ const s3 = new S3Client({
async function getRawDatasetInfo() { async function getRawDatasetInfo() {
try { try {
const response = await fetchWithRetry( return await $`curl -L https://datos.produccion.gob.ar/api/3/action/package_show?id=sepa-precios`.json();
"https://datos.produccion.gob.ar/api/3/action/package_show?id=sepa-precios",
);
return await response.json();
} catch (error) { } catch (error) {
console.error( console.error(
`❌ Error fetching dataset info`, `❌ Error fetching dataset info`,
@ -105,33 +102,6 @@ await saveDatasetInfoIntoRepo(rawDatasetInfo);
let errored = false; let errored = false;
async function fetchWithRetry(
url: string,
maxRetries = 3,
waitTime = 15000,
): Promise<Response> {
let retries = 0;
while (retries < maxRetries) {
try {
const response = await fetch(url, {
signal: AbortSignal.timeout(waitTime),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response;
} catch (error) {
console.error(`Attempt ${retries + 1} failed: ${error}`);
retries++;
if (retries >= maxRetries) {
throw error;
}
await new Promise((resolve) => setTimeout(resolve, 1000 * retries));
}
}
throw new Error("Max retries reached");
}
function checkRes( function checkRes(
res: Response, res: Response,
): res is Response & { body: ReadableStream<Uint8Array> } { ): res is Response & { body: ReadableStream<Uint8Array> } {
@ -152,7 +122,7 @@ await uploadToB2Bucket(
const datasetInfo = z.object({ result: zDatasetInfo }).parse(rawDatasetInfo); const datasetInfo = z.object({ result: zDatasetInfo }).parse(rawDatasetInfo);
for (const resource of datasetInfo.result.resources) { for (const resource of datasetInfo.result.resources) {
if (extname(resource.url) === ".zip") { if (extname(resource.url) === ".zip") {
const fileName = `${resource.id}-${basename(resource.url)}-repackaged.tar.zst`; const fileName = `${resource.id}-revID-${resource.revision_id}-${basename(resource.url)}-repackaged.tar.zst`;
if (await checkFileExistsInB2(fileName)) continue; if (await checkFileExistsInB2(fileName)) continue;
console.log(`⬇️ Downloading, repackaging and uploading ${resource.url}`); console.log(`⬇️ Downloading, repackaging and uploading ${resource.url}`);
const dir = await mkdtemp("/tmp/sepa-precios-archiver-repackage-"); const dir = await mkdtemp("/tmp/sepa-precios-archiver-repackage-");
@ -187,10 +157,9 @@ for (const resource of datasetInfo.result.resources) {
const fileName = `${resource.id}-${basename(resource.url)}`; const fileName = `${resource.id}-${basename(resource.url)}`;
if (await checkFileExistsInB2(fileName)) continue; if (await checkFileExistsInB2(fileName)) continue;
console.log(`⬇️ Downloading and reuploading ${resource.url}`); console.log(`⬇️ Downloading and reuploading ${resource.url}`);
const response = await fetchWithRetry(resource.url, 3, 60 * 1000); const response = await $`curl -L ${resource.url}`.blob();
if (!checkRes(response)) continue;
await uploadToB2Bucket(fileName, response.body); await uploadToB2Bucket(fileName, response);
} }
} }

175
sepa/sepa-precios-importer/.gitignore vendored Normal file
View file

@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
# Logs
logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Caches
.cache
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
# Runtime data
pids
_.pid
_.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
# IntelliJ based IDEs
.idea
# Finder (MacOS) folder config
.DS_Store

View file

@ -12,6 +12,7 @@
"dependencies": { "dependencies": {
"p-queue": "^8.0.1", "p-queue": "^8.0.1",
"papaparse": "^5.4.1", "papaparse": "^5.4.1",
"postgres": "^3.4.4" "postgres": "^3.4.4",
"ckan": "workspace:*"
} }
} }

View file

@ -0,0 +1,27 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,
// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,
// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,
// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
}