Last active
January 29, 2019 10:07
-
-
Save kolya-t/986cc8375917cfcd74980b3de96462e8 to your computer and use it in GitHub Desktop.
tron-accounts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"main": "tron-accounts.js", | |
"scripts": { | |
"start": "node tron-accounts.js" | |
}, | |
"dependencies": { | |
"axios": "^0.18.0", | |
"progress": "^2.0.3" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('axios'); | |
const fs = require('fs'); | |
const ProgressBar = require('progress'); | |
const FILENAME = 'tron-accounts.csv'; | |
async function getAccounts(start = 0, limit = 100, attempts = 3) { | |
const url = `https://apilist.tronscan.org/api/account/list?limit=${limit}&start=${start}`; | |
try { | |
const response = await http.get(url); | |
return response.data; | |
} catch (e) { | |
if (attempts <= 1) { | |
throw e; | |
} | |
return getAccounts({start, limit, attempts: attempts - 1}); | |
} | |
} | |
(async () => { | |
const limit = 1000; | |
const bar = new ProgressBar(' [:bar]:percent :current/:total :etas', { | |
complete: '=', | |
incomplete: ' ', | |
width: 30, | |
total: 1000000000 | |
}); | |
fs.writeFileSync(FILENAME, ''); | |
for (let i = 0; ; i = i + limit) { | |
const accounts = await getAccounts(i, limit); | |
let string = ''; | |
accounts.data.forEach(account => { | |
string += `${account.address},${account.balance}\n`; | |
}); | |
fs.appendFileSync(FILENAME, string); | |
bar.tick(accounts.data.length); | |
bar.total = accounts.total; | |
if (accounts.data.length === 0) { | |
break; | |
} | |
} | |
console.info(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment