kumuniter/bma2kumu.js

91 lines
2.4 KiB
JavaScript

import {
args
, delayR // call a function with a random delay
, log // console.log in .map or .then
, concat // concat for reduce: [[1,2],3,[4],[5,6]].reduce(concat,[]) > [1,2,3,4,5,6]
, only // only n element for array.filter
, toCSV // transforms an arry of same objects to CSV string (1st line with columns)
, get // nodejs HTTP.get
, stick // stick together an array of string and arguments (for template literals)
, toJson // parses JSON string
, toFile, // fs.writeFile but in .map or .then (ex: p.then( toFile('toto.txt') ) )
cache
} from './gentleman'
// templates
import p2e from './person2element'
import c2c from './certifications2connections'
// shrotcuts
const ROOT = (ss,...pp)=> `https://g1.duniter.org${stick(ss,...pp)}`
global.BMA = (ss,...pp)=> delayR( get, ROOT`${stick(ss,...pp)}` ).then( toJson )
delayR.amount = 20000
await cache(`./.cache`)
// Get last UD to compute relative balance
const relativity = (
await BMA`/blockchain/block/${
( await BMA`/blockchain/with/ud` )
.result.blocks.pop()
}`
).dividend / 100
// console.log( relativity )
console.log( ROOT`/wot/members` )
// console.log( await BMA`/wot/members` )
// console.log( (await BMA`/wot/members`).results.filter(only(10)) )
// Load all members then each certifications
let members = await Promise.all(
( await BMA`/wot/members` )
.results
.filter( only(args.limit) )
// .map( person=> ROOT`/wot/certified-by/${person.pubkey}` )
// .map( log() )
.map( person=> BMA`/wot/certified-by/${person.pubkey}` )
)
// console.log( members )
// console.log( 'elems: ', toCSV( await Promise.all(members.map(p2e)) ) )
// process.exit()
let kumu = {
elements: await Promise.all( members.map(p2e) )
, connections: members.map(c2c).reduce(concat,[])
}
toFile( `./wot-elem.csv` )(
toCSV( kumu.elements )
)
// console.log( 'conns: ', toCSV( members.map(c2c).reduce(concat,[]) ) )
toFile( `./wot-conn.csv` )(
toCSV( kumu.connections )
)
toFile( `./wot.kumu.json` )(
JSON.stringify( kumu )
)
// let csv = `${toCSV( members.map(p2e) )}
// ${toCSV( members.map(c2c).reduce(concat,[]) )}`
// let csv = await load()
// .then( json=> json.results.filter((o,i)=>i<5).map(gentlyLoad) )
// .then( arr=> Promise.all(arr) )
// // .then( members=> console.log(members) )
// .then( members=> `
// ${toCSV( members.map(p2e) )}
// ${toCSV( members.map(c2c).reduce(concat,[]) )}
// `)
// console.log('Final CVS:', csv )