Skip to content

Commit

Permalink
All api urls have been moved to config file. Implemented parser of co…
Browse files Browse the repository at this point in the history
…nfig. Changed all relative imports to absolute.
  • Loading branch information
AleksandrLeonov committed Jan 30, 2017
1 parent 4357a8a commit cffe134
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/cryptobalances.egg-info/*
/cryptobalances/__pycache__/*
/cryptobalances/services/__pycache__/*
/tests/__pycache__/*
3 changes: 2 additions & 1 deletion cryptobalances/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from .checker import get_balance
from cryptobalances.checker import get_balance


__all__ = ["get_balance"]
__version__ = '0.9'
19 changes: 14 additions & 5 deletions cryptobalances/checker.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
# -*- coding: utf-8 -*-
from .services.chain_so import pull_request as chain_request
from .services.ethereum import pull_request as eth_request
from .services.doge import pull_request as doge_request
from cryptobalances.validator import autodetect_currency
from cryptobalances.services.chain_so import pull_request as chain_request
from cryptobalances.services.ethereum import pull_request as eth_request
from cryptobalances.services.doge import pull_request as doge_request
from cryptobalances.services.counterparty import pull_request as xcp_request


def get_balance(currency, identifier):

auto_currency = autodetect_currency(identifier)

if auto_currency:
currency = auto_currency

if currency == 'BTC':
return chain_request(currency, identifier)
elif currency == 'LTC':
return chain_request(currency, identifier)
elif currency == 'ETH':
return eth_request(identifier)
return eth_request(currency, identifier)
elif currency == 'DOGE':
return doge_request(identifier)
return doge_request(currency, identifier)
elif currency == 'XCP':
return xcp_request(currency, identifier)
3 changes: 2 additions & 1 deletion cryptobalances/config_parser.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# -*- coding: utf-8 -*-
import os.path
import json


def get_api_url(currency):
# Here, an exception can be thrown out of the fact that
# Here, an exception (TypeError) can be thrown out of the fact that
# function read_config() returns None if file has not been open
return read_config()['api_urls'][currency]

Expand Down
3 changes: 2 additions & 1 deletion cryptobalances/data/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"BTC": "https://chain.so/api/v2/get_address_balance/{network}/{identifier}",
"LTC": "https://chain.so/api/v2/get_address_balance/{network}/{identifier}",
"ETH": "http://api.etherscan.io/api?module=account&action=balance&address={identifier}&tag=latest",
"DOGE": "http://dogechain.info/api/v1/address/balance/{identifier}"
"DOGE": "http://dogechain.info/api/v1/address/balance/{identifier}",
"XCP": "http://xcp.blockscan.com/api2?module=address&action=balance&btc_address={identifier}"
}
}
14 changes: 9 additions & 5 deletions cryptobalances/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# -*- coding: utf-8 -*-
import argparse
from .services import chain_request
from .services import eth_request
from .services import doge_request
from cryptobalances.services import chain_request
from cryptobalances.services import eth_request
from cryptobalances.services import doge_request
from cryptobalances.services import xcp_request


def main():
Expand All @@ -15,9 +17,11 @@ def main():
elif args.currency == 'LTC':
print(chain_request(args.currency, args.wallet))
elif args.currency == 'ETH':
print(eth_request(args.wallet))
print(eth_request(args.currency, args.wallet))
elif args.currency == 'DOGE':
print(doge_request(args.wallet))
print(doge_request(args.currency, args.wallet))
elif args.currency == 'XCP':
print(xcp_request(args.currency, args.wallet))


if __name__ == "__main__":
Expand Down
9 changes: 5 additions & 4 deletions cryptobalances/services/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-
from .chain_so import pull_request as chain_request
from .ethereum import pull_request as eth_request
from .doge import pull_request as doge_request
from cryptobalances.services.chain_so import pull_request as chain_request
from cryptobalances.services.ethereum import pull_request as eth_request
from cryptobalances.services.doge import pull_request as doge_request
from cryptobalances.services.counterparty import pull_request as xcp_request


__all__ = ["chain_request", "eth_request", "doge_request"]
__all__ = ["chain_request", "eth_request", "doge_request", 'xcp_request']
5 changes: 2 additions & 3 deletions cryptobalances/services/chain_so.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
import json
from urllib.request import urlopen
from urllib.error import URLError, HTTPError
from cryptobalances.config_parser import get_api_url


def pull_request(currency, identifier):
api_url = 'https://chain.so/api/v2/get_address_balance/{network}/{identifier}'

try:
with urlopen(api_url.format(network=currency, identifier=identifier), timeout=60) as f:
with urlopen(get_api_url(currency).format(network=currency, identifier=identifier), timeout=60) as f:
return json.loads(f.read().decode('utf-8'))['data']['confirmed_balance']
except HTTPError as error:
response = json.loads(error.read().decode('utf-8'))
Expand Down
7 changes: 3 additions & 4 deletions cryptobalances/services/doge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
import json
from urllib.request import urlopen
from urllib.error import URLError, HTTPError
from cryptobalances.config_parser import get_api_url


def pull_request(identifier):
api_url = 'http://dogechain.info/api/v1/address/balance/{identifier}'

def pull_request(currency, identifier):
try:
with urlopen(api_url.format(identifier=identifier), timeout=60) as f:
with urlopen(get_api_url(currency).format(identifier=identifier), timeout=60) as f:
return json.loads(f.read().decode('utf-8'))['balance']
except HTTPError as error:
return json.loads(error.read().decode('utf-8'))['error']
Expand Down
20 changes: 15 additions & 5 deletions cryptobalances/services/ethereum.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,30 @@
import json
from urllib.request import urlopen
from urllib.error import URLError, HTTPError
from cryptobalances.config_parser import get_api_url


def pull_request(identifier):
api_url = 'http://api.etherscan.io/api?module=account&action=balance&address={identifier}&tag=latest'
def pull_request(currency, identifier):

# TODO: We need to perform validation wallet because api returns always positive response.
# TODO: For example: http://api.etherscan.io/api?module=account&action=balance&address=bla-bla-bla&tag=latest
# TODO: returns {"status":"1","message":"OK","result":"0"}, but it's not mean that balance is zero.
# TODO: This is case when address of wallet not valid

try:
with urlopen(api_url.format(identifier=identifier), timeout=60) as f:
with urlopen(get_api_url(currency).format(identifier=identifier), timeout=60) as f:
response = json.loads(f.read().decode('utf-8'))
if response['message'] is 'NOTOK':
if response['message'] == 'NOTOK':
# TODO: We need return more informative message if api returns error. It's not good: "NOTOK. Error!"
return "{}. {}".format(response['message'], response['result'])
return response['result']
return str(converter(int(response['result'])))
except HTTPError as error:
return error.reason
except URLError as error:
return error.reason
except (ValueError, KeyError) as error:
return error


def converter(balance):
return round(balance/1000000000000000000.0, 5)
8 changes: 6 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
version=cryptobalances.__version__,
author='Aliaksandr Leonau',
author_email='[email protected]',
packages=find_packages(),
packages=find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests"]),
package_data={
'cryptobalances': ['data/*.json'],
},
description='Python module for getting the balance of a various crypto currency',
long_description=open(join(dirname(__file__), 'README.rst')).read(),
url='https://github.com/AleksandrLeonov/Crypto-Balances',
platforms='Linux',
entry_points={
'console_scripts': ['main=cryptobalances.main:main'],
}
},
test_suite='tests'
)

0 comments on commit cffe134

Please sign in to comment.