Bitcoin Forum
May 15, 2024, 07:38:44 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: « 1 [2] 3 »  All
  Print  
Author Topic: Python scripting  (Read 2519 times)
donGeilo (OP)
Full Member
***
Offline Offline

Activity: 169
Merit: 100



View Profile
August 17, 2016, 10:46:46 PM
 #21

No, like it is is great i'll give it a try now, just need to figure out how i reach the local host.

Then i'll give you a reply.

But THANK YOU VERY MUCH!!!
kaicrypzen
Hero Member
*****
Offline Offline

Activity: 1344
Merit: 656



View Profile
August 17, 2016, 10:51:47 PM
 #22

No, like it is is great i'll give it a try now, just need to figure out how i reach the local host.

Then i'll give you a reply.

But THANK YOU VERY MUCH!!!

Ok then, I can go to sleep Tongue.

You are very welcome. Good luck Wink.

xcbtrader
Hero Member
*****
Offline Offline

Activity: 865
Merit: 1006


View Profile
August 18, 2016, 07:16:57 AM
 #23

Hello

If you use a external API like https://blockchain.info/rawaddr/, the problem is if your make more connections to the same IP in a small time, server blockeds your IP (2 o 3 connections per second is max).
The solution is add sleep instruction.

Code:
import time
...
...
...
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']))
    time.sleep(0.5) # 2 connectios per second

...
...
...

Another solution is connect differents APIs.

donGeilo (OP)
Full Member
***
Offline Offline

Activity: 169
Merit: 100



View Profile
August 18, 2016, 07:49:42 AM
 #24

Yes, thx

that s the reason I want to use my own node.

But unfortunately i can't reach it from the script.
kaicrypzen
Hero Member
*****
Offline Offline

Activity: 1344
Merit: 656



View Profile
August 18, 2016, 08:44:16 AM
 #25

Yes, thx

that s the reason I want to use my own node.

But unfortunately i can't reach it from the script.

Still not working?

Can you post your code and/or any error/exception you're getting?

donGeilo (OP)
Full Member
***
Offline Offline

Activity: 169
Merit: 100



View Profile
August 18, 2016, 09:00:09 AM
Last edit: August 18, 2016, 09:24:45 AM by donGeilo
 #26

Good morning!

lol No still doesn't work!

My code:

Code:
import urllib2

import json



addresses = open("addresses.txt", "r")
balances = open("balances.txt", "w")

for address in addresses:
    urlRequest = urllib2.Request("http://localhost:3001/insight-api/" + 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()



My errors:

Code:



Traceback (most recent call last):
 
 File "self4.py", line 10, in <module>

    data = urllib2.urlopen(urlRequest).read()
 
 File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
  
  return opener.open(url, data, timeout)
 
 File "/usr/lib/python2.7/urllib2.py", line 435, in open
  
  response = meth(req, response)
 
 File "/usr/lib/python2.7/urllib2.py", line 548, in http_response
  
  'http', request, response, code, msg, hdrs)
 
 File "/usr/lib/python2.7/urllib2.py", line 473, in error
  
  return self._call_chain(*args)
 
 File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain
  
  result = func(*args)
 
 File "/usr/lib/python2.7/urllib2.py", line 556, in http_error_default
  
  raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)

urllib2.HTTPError: HTTP Error 404: Not Found




Edit:
I think i'm missing something inside the bitcoin.conf file

Edtit2:

Just recognized, seems to be really close! It's the first time the bitcored-Terminal shows the GET requets from python (but with the 404 error in it).
Never appeard before!!!
kaicrypzen
Hero Member
*****
Offline Offline

Activity: 1344
Merit: 656



View Profile
August 18, 2016, 09:51:54 AM
 #27

Good morning!

Good morning to you too Smiley (or maybe good evening, or good day Smiley)


My errors:

Code:
urllib2.HTTPError: HTTP Error 404: Not Found


If you have this error, then I don't understand why it would work when you enter the url in your browser ... Are you a 100% sure that when you enter http://localhost:3001/insight-api/<address> it works in your browser?

You might try two things:
- Clean your browser's cache and retry (entering the url directly in the browser).
- Add a print statement to see what is actually passed to Request:

Code:
url = "http://localhost:3001/insight-api/" + address
print url

donGeilo (OP)
Full Member
***
Offline Offline

Activity: 169
Merit: 100



View Profile
August 18, 2016, 10:06:14 AM
Last edit: August 18, 2016, 10:16:57 AM by donGeilo
 #28

Alright,

if i put: localhost:3001/insight/ +address  in the address-bar i get the error 404!

If i just open localhost:3001/insight/  -the page opens an i can put the address in the search field, and it shows me the balance!!!  Huh

EDIT:

I think it supposed to be http://localhost:3001/insight-api/address/ +address

'cause that's the url it shows me if i enter the address in the search field (in fact it shows me"/insight/address/")

kaicrypzen
Hero Member
*****
Offline Offline

Activity: 1344
Merit: 656



View Profile
August 18, 2016, 10:35:10 AM
 #29

Alright,

if i put: localhost:3001/insight/ +address  in the address-bar i get the error 404!

If i just open localhost:3001/insight/  -the page opens an i can put the address in the search field, and it shows me the balance!!!  Huh

EDIT:

I think it supposed to be http://localhost:3001/insight-api/address/ +address

'cause that's the url it shows me if i enter the address in the search field (in fact it shows me"/insight/address/")



Ah! There might be your answer then. If indeed http://localhost:3001/insight-api/address/<address> is the right url then just update the code and see if it works better Wink

donGeilo (OP)
Full Member
***
Offline Offline

Activity: 169
Merit: 100



View Profile
August 18, 2016, 10:37:35 AM
 #30

Nope,
same 404 error
kaicrypzen
Hero Member
*****
Offline Offline

Activity: 1344
Merit: 656



View Profile
August 18, 2016, 10:56:58 AM
 #31

Nope,
same 404 error

Well you'll just have to figure out what the right url is.

When you put the address in the search field, do you have to click on a search button (or hit enter) ? If so, then what is the url that appears in the search bar once you've done that? This would be the url you should be using.

donGeilo (OP)
Full Member
***
Offline Offline

Activity: 169
Merit: 100



View Profile
August 18, 2016, 11:05:00 AM
 #32

I copy-paste the addres in the search field and hit enter,
and the exact url is:

localhost:3001/insight/address/1NH5FzSuo........

but from the bitcore forum i got the info:
/insight/* is routed via client-side javascript application, the api is accessible at /insight-api/

kaicrypzen
Hero Member
*****
Offline Offline

Activity: 1344
Merit: 656



View Profile
August 18, 2016, 11:27:32 AM
 #33

I copy-paste the addres in the search field and hit enter,
and the exact url is:

localhost:3001/insight/address/1NH5FzSuo........

but from the bitcore forum i got the info:
/insight/* is routed via client-side javascript application, the api is accessible at /insight-api/



I found this https://github.com/bitpay/insight-api

It says an address is accessed with:
Code:
/insight-api/addr/<address>
and a balance with:
Code:
/insight-api/addr/<address>/balance

donGeilo (OP)
Full Member
***
Offline Offline

Activity: 169
Merit: 100



View Profile
August 18, 2016, 11:39:53 AM
 #34

If i use:
Code:
 http://localhost:3001/insight-api/addr/+address")

Code:
 http://localhost:3001/insight-api/addr/+address/balance")

i get the error 400 bad request

EDIT:

misstyped in the code!!

Now it is just KeyError: 'data'

So it passsed the url!!!
donGeilo (OP)
Full Member
***
Offline Offline

Activity: 169
Merit: 100



View Profile
August 18, 2016, 11:44:48 AM
 #35

YAY!!!
removed 'data'


and ......

it works!!!!!!!!!!!

But gives no output!!!


EDIT:

SOLVED!!!
kaicrypzen
Hero Member
*****
Offline Offline

Activity: 1344
Merit: 656



View Profile
August 18, 2016, 11:53:12 AM
 #36

YAY!!!
removed 'data'

That's why I commented
Code:
#use the right keys
Wink

and ......

it works!!!!!!!!!!!

But gives no output!!!

The code isn't supposed to give any output ... it just stores balances in balances.txt Wink

EDIT:

SOLVED!!!

Cool Smiley, so what was the matter?

kaicrypzen
Hero Member
*****
Offline Offline

Activity: 1344
Merit: 656



View Profile
August 18, 2016, 12:12:46 PM
 #37

during the tests i edited a few balances -output text files


Just looked into the wrong one!

OK

Balance of 1NH5FzSuo.......
  is 0.1

Do you mean there is a carriage return after the address? If that's the case, then use:
Code:
balances.write("Balance of " + address.strip("\n") + " is " + str(json_data["balance"]) + "\n")

donGeilo (OP)
Full Member
***
Offline Offline

Activity: 169
Merit: 100



View Profile
August 18, 2016, 12:19:01 PM
 #38

No everything works fine, like it should!!

Just one more question:

If my list would contain more lines, like:


John Smith
Address: 1234567890...

Benjamin Franklin
Address: 1098765432.....

How would that look, to just output all balances ?



kaicrypzen
Hero Member
*****
Offline Offline

Activity: 1344
Merit: 656



View Profile
August 18, 2016, 12:47:32 PM
 #39

No everything works fine, like it should!!

Just one more question:

If my list would contain more lines, like:


John Smith
Address: 1234567890...

Benjamin Franklin
Address: 1098765432.....

How would that look, to just output all balances ?


If you have more info in your text file, maybe you should consider using a csv format or make the lines very distinguishable:
Code:
Name: John Smith
Address: 1234567890...

Then you'll have to test, let's assume that you don't care about the name:

Code:
....

for line in addresses:
    if line.startswith("Address: "): # Test that the line is an address line
        address = line.strip("Address: ") # Remove the prefix Address:
        urlRequest = urllib2.Request("http://localhost:3001/insight-api/addr/" + address)
        data = urllib2.urlopen(urlRequest).read()
        json_data = json.loads(data)
        balances.write("Balance of " + address + " is " + str(json_data["balance"]) + "\n")

....

donGeilo (OP)
Full Member
***
Offline Offline

Activity: 169
Merit: 100



View Profile
August 18, 2016, 01:18:39 PM
 #40

Perfect!!!

Thanks alot!!!
Pages: « 1 [2] 3 »  All
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!