[Ethereum] How to download contracts by address from Etherscan using web3.js

contract-deploymentjavascriptnodejsweb3js

I want to download all contracts (open source code) from etherscan.io using Web3js. Etherscan gives only the list of such verified open source contract with txHash, name, and their addresses. Is there any way to write a program that takes address (from mentioned list) and then download its source code and save to local folder. If, yes, then is there any way to download only the source code of contract having version ^0.5. ?

I am trying to follow this code from EtherScan, and intend to give the address field data run-time/dynamically, but I'm not sure how to proceed in web3.js, node.js or with other APIs.
Also, I am not sure how to call $.getJSON? Where it is defined?

    var Web3 = require('web3');
    var web3 = new Web3(new Web3.providers.HttpProvider());
    var version = web3.version.api;

    $.getJSON('http://api.etherscan.io/api?module=contract&action=getabi&address=0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359', function (data) {
        var contractABI = "";
        contractABI = JSON.parse(data.result);
        if (contractABI != ''){
            var MyContract = web3.eth.contract(contractABI);
            var myContractInstance = MyContract.at("0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359");
            var result = myContractInstance.memberId("0xfe8ad7dd2f564a877cc23feea6c0a9cc2e783715");
            console.log("result1 : " + result);            
            var result = myContractInstance.members(1);
            console.log("result2 : " + result);
        } else {
            console.log("Error" );
        }            
    });

NOTE: I have read all questions regarding this topic on this forum, but no one is providing a solution.

Best Answer

$ refers to jQuery, and the Etherscan link you shared also notes that. Check out their docs here.

I would recommend downloading their list of addresses, and then iterating over it like so:

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider());
var version = web3.version.api;

let addresses = [];
// TODO: Read in CSV data

for (let i = 0; i < addresses.length; i += 1)
  $.getJSON('http://api.etherscan.io/api?module=contract&action=getabi&address=' + addresses[i], function (data) {
        // function body from etherscan
        });

For reading the CSV data, check out this question.