Pages:
Author

Topic: Python scripting (Read 2584 times)

hero member
Activity: 1344
Merit: 656
August 18, 2016, 08:49:29 AM
#41
Perfect!!!

Thanks alot!!!

You're welcome.

You might wanna check dive into python and the python standard library for python 2 and python 3.

Also, maybe lock this thread if all is okay Wink and don't hesitate to shoot me a pm if you need anything.

Good luck Wink.
full member
Activity: 169
Merit: 100
August 18, 2016, 08:18:39 AM
#40
Perfect!!!

Thanks alot!!!
hero member
Activity: 1344
Merit: 656
August 18, 2016, 07:47:32 AM
#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")

....
full member
Activity: 169
Merit: 100
August 18, 2016, 07:19:01 AM
#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 ?



hero member
Activity: 1344
Merit: 656
August 18, 2016, 07:12:46 AM
#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")
hero member
Activity: 1344
Merit: 656
August 18, 2016, 06: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?
full member
Activity: 169
Merit: 100
August 18, 2016, 06:44:48 AM
#35
YAY!!!
removed 'data'


and ......

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

But gives no output!!!


EDIT:

SOLVED!!!
full member
Activity: 169
Merit: 100
August 18, 2016, 06: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!!!
hero member
Activity: 1344
Merit: 656
August 18, 2016, 06: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/
and a balance with:
Code:
/insight-api/addr/
/balance
full member
Activity: 169
Merit: 100
August 18, 2016, 06: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/

hero member
Activity: 1344
Merit: 656
August 18, 2016, 05: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.
full member
Activity: 169
Merit: 100
August 18, 2016, 05:37:35 AM
#30
Nope,
same 404 error
hero member
Activity: 1344
Merit: 656
August 18, 2016, 05: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/
is the right url then just update the code and see if it works better Wink
full member
Activity: 169
Merit: 100
August 18, 2016, 05:06:14 AM
#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/")

hero member
Activity: 1344
Merit: 656
August 18, 2016, 04: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/
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
full member
Activity: 169
Merit: 100
August 18, 2016, 04:00:09 AM
#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

    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!!!
hero member
Activity: 1344
Merit: 656
August 18, 2016, 03: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?
full member
Activity: 169
Merit: 100
August 18, 2016, 02: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.
hero member
Activity: 865
Merit: 1006
August 18, 2016, 02: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.
hero member
Activity: 1344
Merit: 656
August 17, 2016, 05: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.
Pages:
Jump to: