donGeilo (OP)
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
August 17, 2016, 06:52:09 PM |
|
Hi, i'm working on a small project and got stuck. Maybe somebody coud lead me? I'm running a full node and want a py-script to check the balance of an address, That's the code i am using, but really no idea, how the url needs to look, to get get the balance from my bitcore full node, already tried " http://localhost:3001/insight/"+address+"" but, didn't work! That's the original code i was using: def scan(address,pkey): req = urllib2.Request("http://btc.blockr.io/api/v1/address/info/"+address+"") res = urllib2.urlopen(req) json_data = json.load(res)
|
|
|
|
|
donGeilo (OP)
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
August 17, 2016, 07:28:53 PM |
|
First thx for your reply, but how should the code lines look then?
It's a bitcored full node with insight api, so think -txindex is not suitable?
|
|
|
|
Adriandmen
Sr. Member
![*](https://bitcointalk.org/Themes/custom1/images/star.gif) ![*](https://bitcointalk.org/Themes/custom1/images/star.gif) ![*](https://bitcointalk.org/Themes/custom1/images/star.gif)
Offline
Activity: 292
Merit: 251
Telegram: @Adriandmen
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
August 17, 2016, 07:36:16 PM |
|
The syntax seems wrong. I'm assuming this is Python 2. I haven't tested it yet, but does the following work?: req = urllib2.Request("http://btc.blockr.io/api/v1/address/info/"+address) res = urllib2.urlopen(req) json_data = json.loads(res.read())
|
|
|
|
psycodad
Legendary
Offline
Activity: 1634
Merit: 1743
精神分析的爸
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
August 17, 2016, 08:09:13 PM |
|
I don't know about insight api, however what I said regarding txindex is wrong: that doesn't give you the ability query balances, just all transactions (from which you can extract all balances if you for example store them in a db like a block explorer does).
|
|
|
|
donGeilo (OP)
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
August 17, 2016, 08:37:14 PM |
|
Thx again, but nothing worked!
I think i'm doing somthing wrong with the Url and the json_data quering.
|
|
|
|
kaicrypzen
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
August 17, 2016, 09:16:07 PM |
|
The original code seems okay to me, though there are somethings that bother me: - The scan function has a parameter named pkey which it doesn't use ... - I don't know why +"" is added. - urlopen can directly take the (string) url as parameter, I don't see why call Request first ...
Normally, both json.load(res) and json.loads(res.read()) should work ...
What version of python are you using?
Do you get an exception when running your code?
|
|
|
|
donGeilo (OP)
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
August 17, 2016, 09:31:28 PM |
|
ATM i'm using python 2.7.12
i copied these line from github from a script. Tried to modify it, but till now nothing worked.
If i'm typing an addres in the browser, the node works fine.
Edit: The code like it is, works, just when i try to point it to my localhost, nothing happens
|
|
|
|
kaicrypzen
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
August 17, 2016, 09:45:48 PM |
|
The code like it is, works, just when i try to point it to my localhost, nothing happens
What does print res.read() return?
|
|
|
|
donGeilo (OP)
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
August 17, 2016, 09:51:32 PM |
|
Just an error: unexpected indent
|
|
|
|
|
kaicrypzen
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
August 17, 2016, 10:04:34 PM |
|
Just an error: unexpected indent
So there seem to be an issue with indentation, maybe a mix of tabs and spaces ... Did you try running the lines directly in a python interpreter, like this: import urllib2 import json address = "your_address" req = urllib2.Request("http://btc.blockr.io/api/v1/address/info/"+address) res = urllib2.urlopen(req) print res.read() json_data = json.load(res) print json_data
|
|
|
|
Adriandmen
Sr. Member
![*](https://bitcointalk.org/Themes/custom1/images/star.gif) ![*](https://bitcointalk.org/Themes/custom1/images/star.gif) ![*](https://bitcointalk.org/Themes/custom1/images/star.gif)
Offline
Activity: 292
Merit: 251
Telegram: @Adriandmen
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
August 17, 2016, 10:09:12 PM |
|
This one also works: import urllib2 import json
address = raw_input("Enter Address >> ") urlRequest = urllib2.Request("https://blockchain.info/rawaddr/" + address) data = urllib2.urlopen(urlRequest).read()
json_data = json.loads(data) print "Final Balance: " + str(json_data['final_balance'])
|
|
|
|
donGeilo (OP)
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
August 17, 2016, 10:14:57 PM |
|
Yes, these works great!
But need it to read the addresses from a list.
|
|
|
|
kaicrypzen
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
August 17, 2016, 10:16:39 PM |
|
Maybe you got an easy clean solution, to check the balance of some addresses in a list, on a local node?
If it is parsing the output of a url to get the balance then I think it can be done. I just need some info: - Where are the addresses? in a file? in the code in a list object? - What is the call you perform for the local node i.e. the url which you use to access information about the BTC address? And do you expect the same kind of output that http://btc.blockr.io/api/v1/address/info/<address> gives?
|
|
|
|
|
kaicrypzen
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
August 17, 2016, 10:24:29 PM |
|
Yes, these works great!
But need it to read the addresses from a list.
Then you might loop through your list, something like that: balances = []
for address in addresses: urlRequest = urllib2.Request("https://blockchain.info/rawaddr/" + address) data = urllib2.urlopen(urlRequest).read() json_data = json.loads(data) balances.append("Balance of " + address + " is " + str(json_data['final_balance']))
"\n".join(balances)
|
|
|
|
kaicrypzen
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
August 17, 2016, 10:29:58 PM |
|
I want to put the addresses in a textfile.
Let's assume there is one address per line, then you might replace the previous loop with: f = open("addresses.txt", "r")
for address in f: .... And if you want to write the results somewhere then use: f = open("balances.txt", "w")
f.write(....) Finally you might have something like: addresses = open("addresses.txt", "r") balances = open("balances.txt", "w")
for address in addresses: urlRequest = urllib2.Request("http://localhost:3001/insight/" + address) data = urllib2.urlopen(urlRequest).read() json_data = json.loads(data) balances.write("Balance of " + address + " is " + str(json_data["data"]["balance"]) + "\n") #use the right keys
addresses.close() balances.close()
|
|
|
|
donGeilo (OP)
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
August 17, 2016, 10:37:51 PM |
|
then i need the prefix address in each line right?
|
|
|
|
kaicrypzen
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
August 17, 2016, 10:43:28 PM |
|
then i need the prefix address in each line right?
If your txt file is like: first_addr second_addr ..... last_addr the code will work. The github code seems to loop through lines which are prefixed with Address: and Privkey: but if your file contains only addresses, one per line, then the code will work just fine (it loops through the lines and affects the line's value to the variable address). Is this case? Or will you also have private keys in your txt file?
|
|
|
|
|