<file_sep># MIT License
# 
# Copyright (c) 2017 <NAME>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.

import hashlib
import hmac
import requests
import time
import urllib

BASE_URL = 'https://bittrex.com/api/v1.1/{}/{}'


class BittrexAPIBind(object):

    def __init__(self, api_key=None, api_secret=None):
        self.api_key = api_key
        self.api_secret = api_secret

    def _api_query(self, scope, req, **kwargs):

        tokens = ('{}?').format(req)
        request_header = None

        if scope == 'account':
            nonce = str(int(time.time()/100))
            tokens += ('apikey={}&nonce={}').format(self.api_key, nonce)

        if kwargs:
            tokens += ('&{}').format(urllib.urlencode(kwargs))

        request_url = (BASE_URL).format(scope, tokens)

        if scope == 'account':
            api_sign = hmac.new(self.api_secret.encode(),
                                request_url.encode(),
                                hashlib.sha512).hexdigest()
            request_header = dict(apisign=api_sign)

        return requests.get(request_url, headers=request_header).json()

    def get_market_summaries(self):
        resp = self._api_query('public', 'getmarketsummaries')
        return resp['result']

    def get_market_summary(self, currency_base, currency_market):
        market = ('{}-{}').format(currency_base, currency_market)
        resp = self._api_query('public', 'getmarketsummary', market=market)
        market = resp['result'][0]
        return market

    def get_ticker_last(self, currency_base, currency_market):
        market = ('{}-{}').format(currency_base, currency_market)
        resp = self._api_query('public', 'getticker', market=market)
        market = resp['result']['Last']
        return market

    def get_balances(self):
        resp = self._api_query('account', 'getbalances')
        cnt = -1
        wallet = resp['result']
        while cnt < len(wallet)-1:
            cnt += 1
            if not wallet[cnt].get('Balance') or wallet[cnt].get('Currency') in ["BTC", "ETH"]:
                del wallet[cnt]
                cnt -= 1

        return resp['result']
<file_sep>#!/usr/bin/env python
# flake8: noqa

from setuptools import setup, find_packages

setup(
    name = "bit-bind",
    version = "0.0.1",
    packages = find_packages(),

    author = "<NAME>",
    author_email = "<EMAIL>",
    description = "Bittrex SMS Notifier",
    license = "MIT",
    keywords = "sms bittrex",
    url = "https://github.com/hthompson6/bit-bind",

    long_description = open('README.md').read(),
    classifiers = [
      'Programming Language :: Python',
      'Programming Language :: Python :: 2.7',
      'Operating System :: OS Independent',
      'License :: OSI Approved :: MIT License',
      'Development Status :: 3 - Alpha',
    ]
)
<file_sep># bit_bind
Custom API bindings for bittrex

## Installation
```
$ pip install bit-bind
```

### Install from source
```
$ git clone  https://github.com/hthompson6/bit-bind.git
$ cd bit-bind
$ python setup.py install -e .
```

## Usage
```python
api_key = `<KEY>`
api_secret = `zzzzzzzzzzzzzz`

bit_bound = BittrexAPIBinder(api_key, api_secret)
bit_bound.get_current_balances()
bit_bound.get_current_value('BTC', 'VOX')
```
