Created
May 12, 2017 21:57
-
-
Save lukem512/4cd0ac166b343f726bd40cedfad7c01e to your computer and use it in GitHub Desktop.
Retrieve Bitcoin address balance from Blockchain API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
import getopt | |
import urllib2 | |
from optparse import OptionParser | |
def main(): | |
# variables | |
btcaddr = "" | |
baseurl = "https://blockchain.info/q/addressbalance/" | |
# build parser | |
usage = "usage: %prog address" | |
parser = OptionParser(usage); | |
# parse argument | |
(options, args) = parser.parse_args() | |
if len(args) != 1: | |
parser.error("incorrect number of arguments") | |
else: | |
btcaddr = sys.argv[1] | |
# get balance | |
url = baseurl + btcaddr | |
response = urllib2.urlopen(url) | |
html = response.read() | |
# if non-zero, write to a results file! | |
if (html != "0"): | |
print("Found a non-zero balance at: " + url) | |
print("Balance has " + html + " Satoshis") | |
with open("output.log", "a") as outputfile: | |
outputfile.write(btcaddr + "\t" + html) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i just changed your code a bit for python3:
` #!/usr/bin/python
import sys
import getopt
import urllib.request
from optparse import OptionParser
def main():
variables
btcaddr = ""
baseurl = "https://blockchain.info/q/addressbalance/"
build parser
usage = "usage: %prog address"
parser = OptionParser(usage);
parse argument
(options, args) = parser.parse_args()
if len(args) != 1:
parser.error("incorrect number of arguments")
else:
btcaddr = sys.argv[1]
get balance
url = baseurl + btcaddr
response = urllib.request.urlopen(url)
html = int(response.read())
btcbal = (html / 100000000)
if non-zero, write to a results file!
if (html != "0"):
print("Found a non-zero balance at: " + url)
print(str(btcbal) + "BTC")
with open("output.log", "a") as outputfile:
outputfile.write(btcaddr + "-balance-:" + str(btcbal) + "\n")
if name == "main":
main()
`