基于Java的blockchain.info的API请求自发停止

时间:2017-02-27 10:30:20

标签: java bitcoin blockchain.info-api

在我的java类中,我从blk ... dat的文件中提取事务的哈希值,然后通过读取URL:https://blockchain.info/rawtx/+哈希来获取相应事务的数据。有一段时间,我的程序在调用网址1370次后停止了呼叫,另一次,我的程序在拨打网址2243次后停止了呼叫。程序不会终止,无论我等多久都不会继续。

在这种情况下发生了什么?

由于呼叫次数超过此处描述的硬限制:https://blockchain.info/api,我怀疑这不是我缺少API密钥的问题。但在这种情况下,我怎么能获得API密钥?我没有网站(java应用程序是我的学士论文),似乎很多网上人都有从blockchain.info获取API-Key的问题。

可能是因为我正在使用的网络质量,如果是的话,有没有办法解决它与我的java程序?

如果这是blockchain.info方面的固有问题,有人会推荐我另一种方法来获得类似的交易数据吗?

我的代码:

import org.bitcoinj.core.*;
import org.bitcoinj.core.Transaction;
import org.bitcoinj.utils.*;
import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.*;
import org.bitcoinj.params.MainNetParams;


import org.json.*;

public class ParseFromDat {
    private static String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
            sb.append((char) cp);
        }
        return sb.toString();
    }

    public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
        InputStream is = new URL(url).openStream();
        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
            String jsonText = readAll(rd);
            JSONObject json = new JSONObject(jsonText);
            return json;
        } finally {
            is.close();
        }
    }

    public static void main(String[] args) throws IOException, JSONException {
        Context.getOrCreate(MainNetParams.get());

        // Arm the blockchain file loader.
        NetworkParameters np = new MainNetParams();
        List<File> blockChainFiles = new ArrayList<File>();
        blockChainFiles.add(new File("C:\\Users\\...\\AppData\\Roaming\\Bitcoin\\blocks\\blk00514.dat"));
        BlockFileLoader bfl = new BlockFileLoader(np, blockChainFiles);

        // define files to be written into
        PrintWriter transactions = new PrintWriter(new File("./csvs/transactions.csv"));
        StringBuilder traStr = new StringBuilder();
        traStr.append("tranHashString:ID(Trans)\n");
        PrintWriter sendAdd = new PrintWriter(new File("./csvs/sendAdd.csv"));
        StringBuilder sendStr = new StringBuilder();
        sendStr.append("addr:ID(SendAdd),value,tranHashString,addr_tag_link,addr_tag\n");
        PrintWriter receiveAdd = new PrintWriter(new File("./csvs/receiveAdd.csv"));
        StringBuilder receiStr = new StringBuilder();
        receiStr.append("addr:ID(ReceAdd),value,tranHashString,addr_tag_link,addr_tag\n");

        PrintWriter inTran = new PrintWriter(new File("./csvs/intran.csv"));
        StringBuilder inStr = new StringBuilder();
        inStr.append(":START_ID(SendAdd),:END_ID(Trans)\n");
        PrintWriter outTran = new PrintWriter(new File("./csvs/outtran.csv"));
        StringBuilder outStr = new StringBuilder();
        outStr.append(":START_ID(Trans),:END_ID(ReceAdd)\n");

        // Iterate over the blocks in the dataset.
        int counter = 0;
        for (Block block : bfl) {
            System.out.println("aaaa");
            List<Transaction> trans = block.getTransactions();
            for (Transaction ta : trans) {
                System.out.println("bbbb");
                traStr.append(ta.getHashAsString());
                traStr.append("\n");        
                System.out.println("bbbb1 " + (counter++));
                System.out.println(ta.getHashAsString());
                JSONObject json = readJsonFromUrl("https://blockchain.info/rawtx/" + ta.getHashAsString());
                JSONArray inputs = json.getJSONArray("inputs");
                for (int i = 0; i < inputs.length(); i++) {
                    if (inputs.getJSONObject(i).has("prev_out")) { // it is possible that the value is null even when prev_out is not null
                        System.out.println("ccc");
                        JSONObject prevOut = (JSONObject) (inputs.getJSONObject(i).get("prev_out"));
                        inStr.append(prevOut.get("addr").toString());
                        inStr.append(',');
                        inStr.append(ta.getHashAsString());
                        inStr.append("\n");

                        sendStr.append(prevOut.get("addr").toString());
                        sendStr.append(',');
                        sendStr.append(prevOut.get("value").toString());
                        sendStr.append(',');
                        sendStr.append(ta.getHashAsString());

                        if (prevOut.has("addr_tag_link") || prevOut.has("addr_tag")) {
                            if (prevOut.has("addr_tag_link") && prevOut.has("addr_tag")) {
                                sendStr.append(prevOut.get("addr_tag_link").toString());
                                sendStr.append(',');
                                sendStr.append(prevOut.get("addr_tag").toString());
                            } else if (prevOut.has("addr_tag_link")) {
                                sendStr.append(prevOut.get("addr_tag_link").toString());
                                sendStr.append(',');
                                sendStr.append("null");
                            } else {
                                sendStr.append("null");
                                sendStr.append(',');
                                sendStr.append(prevOut.get("addr_tag").toString());
                            }
                        }
                        sendStr.append("\n");
                    }else{
                        continue;
                    }
                }
                JSONArray out = json.getJSONArray("out");
                for (int i = 0; i < out.length(); i++) {
                    JSONObject outItem = out.getJSONObject(i);
                    if (outItem.has("addr")) { // it is possible that the add and value is null?
                        outStr.append(ta.getHashAsString());
                        outStr.append(',');
                        outStr.append(outItem.get("addr").toString());
                        outStr.append("\n");

                        receiStr.append(outItem.get("addr").toString());
                        receiStr.append(',');
                        receiStr.append(outItem.get("value").toString());
                        receiStr.append(',');
                        receiStr.append(ta.getHashAsString());

                        if (outItem.has("addr_tag_link") || outItem.has("addr_tag")) {
                            if (outItem.has("addr_tag_link") && outItem.has("addr_tag")) {
                                receiStr.append(outItem.get("addr_tag_link").toString());
                                receiStr.append(',');
                                receiStr.append(outItem.get("addr_tag").toString());
                            } else if (outItem.has("addr_tag_link")) {
                                receiStr.append(outItem.get("addr_tag_link").toString());
                                receiStr.append(',');
                                receiStr.append("null");
                            } else {
                                receiStr.append("null");
                                receiStr.append(',');
                                receiStr.append(outItem.get("addr_tag").toString());
                            }
                        }
                        receiStr.append("\n");
                    }else{
                        continue;
                    }

                }

            }

        }
        transactions.write(traStr.toString());
        transactions.close();
        sendAdd.write(sendStr.toString());
        sendAdd.close();
        receiveAdd.write(receiStr.toString());
        receiveAdd.close();
        inTran.write(inStr.toString());
        inTran.close();
        outTran.write(outStr.toString());
        outTran.close();
    }
}

0 个答案:

没有答案