Onja
1 year ago
2 changed files with 57 additions and 2 deletions
@ -1,9 +1,24 @@ |
|||||
var express = require('express'); |
var express = require('express'); |
||||
var router = express.Router(); |
var router = express.Router(); |
||||
|
|
||||
|
const FileService = require('../services/file'); |
||||
|
|
||||
/* GET home page. */ |
/* GET home page. */ |
||||
router.get('/', function(req, res, next) { |
router.get('/', async function(req, res, next) { |
||||
res.render('index', { title: 'Express' }); |
const url = 'https://bodacc-datadila.opendatasoft.com/api/explore/v2.1/catalog/datasets/annonces-commerciales/exports/csv?lang=fr&refine=publicationavis%3A%22A%22&refine=publicationavis_facette%3A%22Bodacc%20A%22&refine=familleavis_lib%3A%22Ventes%20et%20cessions%22&timezone=Asia%2FBaghdad&use_labels=true&delimiter=%3B'; |
||||
|
|
||||
|
const columns = ['id', 'region_code', 'listepersonnes.personne.typePersonne', 'listepersonnes.personne.denomination', 'listeetablissements.etablissement.adresse', 'listeprecedentproprietaire.personne.nom', 'listeprecedentproprietaire.personne.prenom']; |
||||
|
|
||||
|
const fileService = new FileService(); |
||||
|
const stream = await fileService.parseFromUrl(url, columns); |
||||
|
|
||||
|
if ( !stream ) { |
||||
|
return res.status(500).send('Invalid stream'); |
||||
|
} |
||||
|
|
||||
|
stream.pipe(res); |
||||
|
|
||||
|
// res.render('index', { title: 'Express' });
|
||||
}); |
}); |
||||
|
|
||||
module.exports = router; |
module.exports = router; |
||||
|
@ -0,0 +1,40 @@ |
|||||
|
const EventEmitter = require('events'); |
||||
|
const csv = require('csv-parser'); |
||||
|
|
||||
|
const File = require('../models/File'); |
||||
|
|
||||
|
// Create a class FileService that extends EventEmitter
|
||||
|
class FileService extends EventEmitter { |
||||
|
|
||||
|
|
||||
|
async parseFromUrl(url, columns) { |
||||
|
this.emit('parseFromUrl.start', { url, columns }); |
||||
|
|
||||
|
// Check if url is valid
|
||||
|
if (!url) { |
||||
|
this.emit('parseFromUrl.error', { url, columns, error: 'Invalid url' }); |
||||
|
return Promise.reject(new Error('Invalid url')); |
||||
|
} |
||||
|
|
||||
|
// Check columns
|
||||
|
if (!columns || !Array.isArray(columns) || columns.length === 0) { |
||||
|
this.emit('parseFromUrl.error', { url, columns, error: 'Invalid columns' }); |
||||
|
return Promise.reject(new Error('Invalid columns')); |
||||
|
} |
||||
|
|
||||
|
// Create a new File instance
|
||||
|
const file = new File(url); |
||||
|
const filepath = await file.download(); |
||||
|
|
||||
|
const stream = file.parse(columns); |
||||
|
if ( !stream ) { |
||||
|
this.emit('parseFromUrl.error', { url, columns, error: 'Invalid stream' }); |
||||
|
return Promise.reject(new Error('Invalid stream')); |
||||
|
} |
||||
|
|
||||
|
this.emit('parseFromUrl.end', { url, columns, filepath }); |
||||
|
return Promise.resolve(stream); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
module.exports = FileService; |
Loading…
Reference in new issue