-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsync_elastic.js
75 lines (71 loc) · 2.17 KB
/
sync_elastic.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const Promise = require('bluebird')
const ora = require('ora');
const chalk = require('chalk');
const bitcoin = require('bitcoin-promise');
const request = require('request');
const sleep = require('sleep');
var elasticsearch = require('elasticsearch');
var eclient = new elasticsearch.Client({
host: 'http://elastic:changeme@localhost:9200',
log: 'info'
});
const client = new bitcoin.Client({
host: 'localhost',
port: 9245,
user: 'lbry',
pass: 'lbry',
timeout: 30000
});
let claimsSynced=0;
async function sync (currentHeight) {
try {
let maxHeight = await client.getBlockCount().then(blockHash => {return blockHash}).catch( err => reject(err));
if( currentHeight <= maxHeight ) {
let claims = await require('./getClaims')(currentHeight, client);
send(claims);
claimsSynced += claims.length;
spinner.color = 'green';
spinner.text = `Current block: ${currentHeight}/${maxHeight} | TotalClaimsImported: ${claimsSynced} `
sync(currentHeight+1);
} else {
process.exit(0);
spinner.color = 'yellow';
spinner.text = `Waiting for new blocks...`;
sync(currentHeight);
}
} catch (err) {
spinner.color = 'red';
spinner.text = ('Error with block: %s, %s', currentHeight, err);
}
}
function send(arr){ // Modular change output here :)
arr.forEach(function(claim) {
claim['id'] = claim['claimId'];
//Check if our value is a object, else make it a object...
claim['value'] = (typeof claim.value == "object" ? claim.value : JSON.parse(claim.value));
//claim['value'] = JSON.stringify(claim['value']);
if(claim.name && claim.value){
claim.suggest_name ={
input: claim.name,
weight: 20
}
if(claim.value.description){
claim.suggest_desc ={
input: claim.value.description.split(" "),
weight: 10
}
}
}
eclient.create({
index: 'claims',
type: 'claim',
id: claim.claimId,
body: claim
}, function (error, response) {
console.log(response);
});
});
}
console.log(chalk.green.underline.bold('Running LBRYSync v0.0.1rc1'))
const spinner = ora('Loading LBRYsync..').start();
sync(0)// Block to start from... :)