Exemplo n.º 1
0
def writeAPIFiles(api_path, timestamp, calculated_average_rates, calculated_volumes, calculated_global_average_rates_formatted, exchanges_ignored):
    try:
        all_data = {}
        all_data['timestamp'] = timestamp
        all_data['ignored_exchanges'] = exchanges_ignored
        for currency in CURRENCY_LIST:
            cur_data = {'exchanges': calculated_volumes[currency],
                        'averages': calculated_average_rates[currency],
                        'global_averages': calculated_global_average_rates_formatted[currency],
                        }
            all_data[currency] = cur_data

        with open(os.path.join(api_path, API_FILES['ALL_FILE']), 'w+') as api_all_data_file:
            api_all_data_file.write(json.dumps(all_data,  indent=2, sort_keys=True, separators=(',', ': ')))

        rates_all = calculated_average_rates
        rates_all['timestamp'] = timestamp
        with open(os.path.join(api_path, API_FILES['TICKER_PATH'], 'all'), 'w+') as api_ticker_all_file:
            api_ticker_all_file.write(json.dumps(rates_all, indent=2, sort_keys=True, separators=(',', ': ')))

        for currency in CURRENCY_LIST:
            ticker_cur = calculated_average_rates[currency]
            ticker_cur['timestamp'] = timestamp
            api_ticker_file = open(os.path.join(api_path, API_FILES['TICKER_PATH'], currency), 'w+')
            api_ticker_file.write(json.dumps(ticker_cur,  indent=2, sort_keys=True, separators=(',', ': ')))
            api_ticker_file.close()


        rates_all = calculated_global_average_rates_formatted
        rates_all['timestamp'] = timestamp
        with open(os.path.join(api_path, API_FILES['GLOBAL_TICKER_PATH'], 'all'), 'w+') as api_global_ticker_all_file:
            api_global_ticker_all_file.write(json.dumps(rates_all, indent=2, sort_keys=True, separators=(',', ': ')))

        for currency in CURRENCY_LIST:
            ticker_cur = calculated_global_average_rates_formatted[currency]
            ticker_cur['timestamp'] = timestamp
            api_ticker_file = open(os.path.join(api_path, API_FILES['GLOBAL_TICKER_PATH'], currency), 'w+')
            api_ticker_file.write(json.dumps(ticker_cur,  indent=2, sort_keys=True, separators=(',', ': ')))
            api_ticker_file.close()

        volumes_all = calculated_volumes
        volumes_all['timestamp'] = timestamp
        with open(os.path.join(api_path, API_FILES['EXCHANGES_PATH'], 'all'), 'w+') as api_volume_all_file:
            api_volume_all_file.write(json.dumps(volumes_all, indent=2, sort_keys=True, separators=(',', ': ')))

        for currency in CURRENCY_LIST:
            volume_cur = calculated_volumes[currency]
            volume_cur['timestamp'] = timestamp
            api_ticker_file = open(os.path.join(api_path, API_FILES['EXCHANGES_PATH'], currency), 'w+')
            api_ticker_file.write(json.dumps(volume_cur,  indent=2, sort_keys=True, separators=(',', ': ')))
            api_ticker_file.close()

        with open(os.path.join(api_path, API_FILES['IGNORED_FILE']), 'w+') as api_ignored_file:
            api_ignored_file.write(json.dumps(exchanges_ignored,  indent=2, sort_keys=True, separators=(',', ': ')))

    except IOError as error:
        error_text = '%s, %s ' % (sys.exc_info()[0], error)
        helpers.write_log(error_text)
        print 'ERROR: %s ' % (error_text)
        raise error
Exemplo n.º 2
0
from bitcoinaverage.helpers import write_config, write_log, write_fiat_rates_config
from bitcoinaverage.nogox import create_nogox_api

if ba.server.PROJECT_PATH == '':
    ba.server.PROJECT_PATH = include_path
if ba.server.LOG_PATH == '':
    ba.server.LOG_PATH = os.path.join(ba.server.PROJECT_PATH, 'runtime', 'app.log')
if ba.server.API_DOCUMENT_ROOT == '':
    ba.server.API_DOCUMENT_ROOT = os.path.join(ba.server.PROJECT_PATH, 'api')
    ba.server.API_DOCUMENT_ROOT_NOGOX = os.path.join(ba.server.API_DOCUMENT_ROOT, 'no-mtgox')
if ba.server.WWW_DOCUMENT_ROOT == '':
    ba.server.WWW_DOCUMENT_ROOT = os.path.join(ba.server.PROJECT_PATH, 'www')
if ba.server.HISTORY_DOCUMENT_ROOT == '':
    ba.server.HISTORY_DOCUMENT_ROOT = os.path.join(ba.server.PROJECT_PATH, 'api', 'history')

write_log('script started', 'LOG')
write_config()
last_fiat_exchange_rate_update = 0

while True:
    if last_fiat_exchange_rate_update < int(time.time())-FIAT_RATES_QUERY_FREQUENCY:
        write_fiat_rates_config()
        last_fiat_exchange_rate_update = int(time.time())

    start_time = int(time.time())

    exchanges_rates, exchanges_ignored = ba.api_parsers.callAll()

    calculated_average_rates = {}
    total_currency_volumes = {}
    total_currency_volumes_ask = {}
Exemplo n.º 3
0
def callAPI(exchange_name):
    global API_QUERY_CACHE, API_QUERY_FREQUENCY, API_IGNORE_TIMEOUT, EXCHANGE_LIST

    current_timestamp = int(time.time())
    result = None
    exchange_ignore_reason = None

    if exchange_name not in API_QUERY_CACHE:
        API_QUERY_CACHE[exchange_name] = {"last_call_timestamp": 0, "result": None, "call_fail_count": 0}

    if (
        "ignored" in EXCHANGE_LIST[exchange_name]
        and EXCHANGE_LIST[exchange_name]["ignored"] == True
        and "ignore_reason" in EXCHANGE_LIST[exchange_name]
    ):
        exchange_ignore_reason = str(EXCHANGE_LIST[exchange_name]["ignore_reason"])
    else:
        try:
            try:
                if (
                    exchange_name in API_QUERY_FREQUENCY
                    and API_QUERY_CACHE[exchange_name]["last_call_timestamp"] + API_QUERY_FREQUENCY[exchange_name]
                    > current_timestamp
                ):
                    result = API_QUERY_CACHE[exchange_name]["result"]
                else:
                    if "_%sApiCall" % exchange_name in globals():
                        try:
                            result = globals()["_%sApiCall" % exchange_name](**EXCHANGE_LIST[exchange_name])
                            result["data_source"] = "api"
                        except (
                            KeyError,
                            TypeError,
                            ValueError,
                            simplejson.decoder.JSONDecodeError,
                            socket.error,
                            urllib2.URLError,
                            httplib.BadStatusLine,
                            CallTimeoutException,
                        ) as error:
                            if "bitcoincharts_symbols" in EXCHANGE_LIST[exchange_name]:
                                result = getData(EXCHANGE_LIST[exchange_name]["bitcoincharts_symbols"])
                                result["data_source"] = "bitcoincharts"
                            else:
                                raise error
                    elif "bitcoincharts_symbols" in EXCHANGE_LIST[exchange_name]:
                        result = getData(EXCHANGE_LIST[exchange_name]["bitcoincharts_symbols"])
                        result["data_source"] = "bitcoincharts"
                    else:
                        raise NoApiException

                    API_QUERY_CACHE[exchange_name] = {
                        "last_call_timestamp": current_timestamp,
                        "result": result,
                        "call_fail_count": 0,
                    }
            except (
                KeyError,
                TypeError,
                ValueError,
                socket.error,
                simplejson.decoder.JSONDecodeError,
                urllib2.URLError,
                httplib.BadStatusLine,
                CallTimeoutException,
            ) as error:

                API_QUERY_CACHE[exchange_name]["call_fail_count"] = (
                    API_QUERY_CACHE[exchange_name]["call_fail_count"] + 1
                )
                if API_QUERY_CACHE[exchange_name]["last_call_timestamp"] + API_IGNORE_TIMEOUT > current_timestamp:
                    result = API_QUERY_CACHE[exchange_name]["result"]
                    result["data_source"] = "cache"
                    write_log(
                        "%s call failed, %s, %s fails in a row, using cache, cache age %ss"
                        % (
                            exchange_name,
                            type(error).__name__,
                            str(API_QUERY_CACHE[exchange_name]["call_fail_count"]),
                            str(current_timestamp - API_QUERY_CACHE[exchange_name]["last_call_timestamp"]),
                        ),
                        "WARNING",
                    )
                else:
                    last_call_datetime = datetime.datetime.fromtimestamp(
                        API_QUERY_CACHE[exchange_name]["last_call_timestamp"]
                    )
                    today = datetime.datetime.now()
                    if API_QUERY_CACHE[exchange_name]["last_call_timestamp"] == 0:
                        datetime_str = today.strftime("%H:%M")
                        API_QUERY_CACHE[exchange_name]["last_call_timestamp"] = current_timestamp - API_IGNORE_TIMEOUT
                    elif last_call_datetime.day == today.day and last_call_datetime.month == today.month:
                        datetime_str = last_call_datetime.strftime("%H:%M")
                    else:
                        datetime_str = last_call_datetime.strftime("%d %b, %H:%M")

                    last_call_strdate = "never"
                    if API_QUERY_CACHE[exchange_name]["last_call_timestamp"] != 0:
                        last_call_strdate = email.utils.formatdate(
                            API_QUERY_CACHE[exchange_name]["last_call_timestamp"]
                        )

                    log_message = (
                        "%s call failed, %s, %s fails in a row, last successful call - %s, cache timeout, exchange ignored"
                        % (
                            exchange_name,
                            type(error).__name__,
                            str(API_QUERY_CACHE[exchange_name]["call_fail_count"]),
                            last_call_strdate,
                        )
                    )
                    write_log(log_message, "ERROR")
                    exception = CacheTimeoutException()
                    exception.strerror = exception.strerror % datetime_str
                    raise exception
        except (NoApiException, NoVolumeException, CacheTimeoutException) as error:
            exchange_ignore_reason = error.strerror

    return exchange_name, result, exchange_ignore_reason
Exemplo n.º 4
0
if ba.server.PROJECT_PATH == '':
    ba.server.PROJECT_PATH = include_path
if ba.server.LOG_PATH == '':
    ba.server.LOG_PATH = os.path.join(ba.server.PROJECT_PATH, 'runtime',
                                      'app.log')
if ba.server.API_DOCUMENT_ROOT == '':
    ba.server.API_DOCUMENT_ROOT = os.path.join(ba.server.PROJECT_PATH, 'api')
    ba.server.API_DOCUMENT_ROOT_NOGOX = os.path.join(
        ba.server.API_DOCUMENT_ROOT, 'no-mtgox')
if ba.server.WWW_DOCUMENT_ROOT == '':
    ba.server.WWW_DOCUMENT_ROOT = os.path.join(ba.server.PROJECT_PATH, 'www')
if ba.server.HISTORY_DOCUMENT_ROOT == '':
    ba.server.HISTORY_DOCUMENT_ROOT = os.path.join(ba.server.PROJECT_PATH,
                                                   'api', 'history')

helpers.write_log('script started', 'LOG')
helpers.write_js_config()
helpers.write_fiat_rates_config()
last_fiat_exchange_rate_update = time.time()
helpers.write_api_index_files()

while True:
    if last_fiat_exchange_rate_update < int(
            time.time()) - FIAT_RATES_QUERY_FREQUENCY:
        helpers.write_fiat_rates_config()

    start_time = int(time.time())

    exchanges_rates, exchanges_ignored = ba.api_parsers.callAll()

    total_currency_volumes, total_currency_volumes_ask, total_currency_volumes_bid = calculateTotalVolumes(
Exemplo n.º 5
0
def callAPI(exchange_name):
    global API_QUERY_CACHE, API_QUERY_FREQUENCY, API_IGNORE_TIMEOUT, EXCHANGE_LIST

    current_timestamp = int(time.time())
    result = None
    exchange_ignore_reason = None

    if exchange_name not in API_QUERY_CACHE:
        API_QUERY_CACHE[exchange_name] = {'last_call_timestamp': 0,
                                           'result': None,
                                           'call_fail_count': 0,
                                               }

    try:
        try:
            if (exchange_name in API_QUERY_FREQUENCY
                and API_QUERY_CACHE[exchange_name]['last_call_timestamp']+API_QUERY_FREQUENCY[exchange_name] > current_timestamp):
                result = API_QUERY_CACHE[exchange_name]['result']
            else:
                if '_%sApiCall' % exchange_name in globals():
                    try:
                        result = globals()['_%sApiCall' % exchange_name](**EXCHANGE_LIST[exchange_name])
                        result['data_source'] = 'api'
                    except (
                            KeyError,
                            ValueError,
                            simplejson.decoder.JSONDecodeError,
                            socket.error,
                            urllib2.URLError,
                            httplib.BadStatusLine,
                            CallTimeoutException) as error:
                        if 'bitcoincharts_symbols' in EXCHANGE_LIST[exchange_name]:
                            result = getData(EXCHANGE_LIST[exchange_name]['bitcoincharts_symbols'])
                            result['data_source'] = 'bitcoincharts'
                        else:
                            raise error
                elif 'bitcoincharts_symbols' in EXCHANGE_LIST[exchange_name]:
                            result = getData(EXCHANGE_LIST[exchange_name]['bitcoincharts_symbols'])
                            result['data_source'] = 'bitcoincharts'
                else:
                    raise NoApiException

                API_QUERY_CACHE[exchange_name] = {'last_call_timestamp': current_timestamp,
                                                   'result':result,
                                                   'call_fail_count': 0,
                                                   }
        except (
                KeyError,
                ValueError,
                socket.error,
                simplejson.decoder.JSONDecodeError,
                urllib2.URLError,
                httplib.BadStatusLine,
                CallTimeoutException) as error:

            API_QUERY_CACHE[exchange_name]['call_fail_count'] = API_QUERY_CACHE[exchange_name]['call_fail_count'] + 1
            if (API_QUERY_CACHE[exchange_name]['last_call_timestamp']+API_IGNORE_TIMEOUT > current_timestamp):
                result = API_QUERY_CACHE[exchange_name]['result']
                result['data_source'] = 'cache'
                write_log('%s call failed, %s, %s fails in a row, using cache, cache age %ss'
                          % (exchange_name,
                             type(error).__name__,
                             str(API_QUERY_CACHE[exchange_name]['call_fail_count']),
                             str(current_timestamp-API_QUERY_CACHE[exchange_name]['last_call_timestamp']) ),
                          'WARNING')
            else:
                last_call_datetime = datetime.datetime.fromtimestamp(API_QUERY_CACHE[exchange_name]['last_call_timestamp'])
                today = datetime.datetime.now()
                if API_QUERY_CACHE[exchange_name]['last_call_timestamp'] == 0:
                    datetime_str = today.strftime('%H:%M')
                    API_QUERY_CACHE[exchange_name]['last_call_timestamp'] = current_timestamp-API_IGNORE_TIMEOUT
                elif last_call_datetime.day == today.day and last_call_datetime.month == today.month:
                    datetime_str = last_call_datetime.strftime('%H:%M')
                else:
                    datetime_str = last_call_datetime.strftime('%d %b, %H:%M')

                last_call_strdate = 'never'
                if API_QUERY_CACHE[exchange_name]['last_call_timestamp'] != 0:
                    last_call_strdate = email.utils.formatdate(API_QUERY_CACHE[exchange_name]['last_call_timestamp'])

                log_message = ('%s call failed, %s, %s fails in a row, last successful call - %s, cache timeout, exchange ignored'
                               % (exchange_name,
                                  type(error).__name__,
                                  str(API_QUERY_CACHE[exchange_name]['call_fail_count']),
                                  last_call_strdate,
                                    ))
                write_log(log_message, 'ERROR')
                exception = CacheTimeoutException()
                exception.strerror = exception.strerror % datetime_str
                raise exception
    except (NoApiException, NoVolumeException, CacheTimeoutException) as error:
        exchange_ignore_reason = error.strerror

    return exchange_name, result, exchange_ignore_reason
Exemplo n.º 6
0
import email

import bitcoinaverage as ba
from bitcoinaverage.config import HISTORY_QUERY_FREQUENCY, CURRENCY_LIST
from bitcoinaverage.helpers import write_log
from bitcoinaverage import history_writers


if ba.server.PROJECT_PATH == '':
    ba.server.PROJECT_PATH = include_path
if ba.server.LOG_PATH == '':
    ba.server.LOG_PATH = os.path.join(ba.server.PROJECT_PATH, 'runtime', 'app.log')
if ba.server.HISTORY_DOCUMENT_ROOT == '':
    ba.server.HISTORY_DOCUMENT_ROOT = os.path.join(ba.server.PROJECT_PATH, 'api', 'history')

write_log('script started', 'LOG')


for currency_code in CURRENCY_LIST:
    if not os.path.exists(os.path.join(ba.server.HISTORY_DOCUMENT_ROOT, currency_code)):
        os.makedirs(os.path.join(ba.server.HISTORY_DOCUMENT_ROOT, currency_code))

while True:
    ticker_url = ba.server.API_INDEX_URL+'all'
    fiat_data_url = ba.server.API_INDEX_URL+'fiat_data'
    try:
        current_data_all = requests.get(ticker_url, headers=ba.config.API_REQUEST_HEADERS).json()
        fiat_data_all = requests.get(fiat_data_url, headers=ba.config.API_REQUEST_HEADERS).json()
    except (simplejson.decoder.JSONDecodeError, requests.exceptions.ConnectionError), err:
        write_log("can not get API data: {0}".format(str(err)), "ERROR")
        time.sleep(10)
Exemplo n.º 7
0
import bitcoinaverage as ba
from bitcoinaverage.config import HISTORY_QUERY_FREQUENCY, CURRENCY_LIST
from bitcoinaverage.helpers import write_log
from bitcoinaverage import history_writers

if ba.server.PROJECT_PATH == '':
    ba.server.PROJECT_PATH = include_path
if ba.server.LOG_PATH == '':
    ba.server.LOG_PATH = os.path.join(ba.server.PROJECT_PATH, 'runtime',
                                      'app.log')
if ba.server.HISTORY_DOCUMENT_ROOT == '':
    ba.server.HISTORY_DOCUMENT_ROOT = os.path.join(ba.server.PROJECT_PATH,
                                                   'api', 'history')

write_log('script started', 'LOG')

for currency_code in CURRENCY_LIST:
    if not os.path.exists(
            os.path.join(ba.server.HISTORY_DOCUMENT_ROOT, currency_code)):
        os.makedirs(
            os.path.join(ba.server.HISTORY_DOCUMENT_ROOT, currency_code))

while True:
    ticker_url = ba.server.API_INDEX_URL + 'all'
    fiat_data_url = ba.server.API_INDEX_URL + 'fiat_data'
    try:
        current_data_all = requests.get(
            ticker_url, headers=ba.config.API_REQUEST_HEADERS).json()
        fiat_data_all = requests.get(
            fiat_data_url, headers=ba.config.API_REQUEST_HEADERS).json()
Exemplo n.º 8
0
import email

import bitcoinaverage as ba
from bitcoinaverage.config import HISTORY_QUERY_FREQUENCY, CURRENCY_LIST
from bitcoinaverage.helpers import write_log
from bitcoinaverage import history_writers


if ba.server.PROJECT_PATH == '':
    ba.server.PROJECT_PATH = include_path
if ba.server.LOG_PATH == '':
    ba.server.LOG_PATH = os.path.join(ba.server.PROJECT_PATH, 'runtime', 'app.log')
if ba.server.HISTORY_DOCUMENT_ROOT == '':
    ba.server.HISTORY_DOCUMENT_ROOT = os.path.join(ba.server.PROJECT_PATH, 'api', 'history')

write_log('script started', 'LOG')


for currency_code in CURRENCY_LIST:
    if not os.path.exists(os.path.join(ba.server.HISTORY_DOCUMENT_ROOT, currency_code)):
        os.makedirs(os.path.join(ba.server.HISTORY_DOCUMENT_ROOT, currency_code))

while True:
    ticker_url = ba.server.API_INDEX_URL+'all'
    fiat_data_url = ba.server.API_INDEX_URL+'fiat_data'
    try:
        current_data_all = requests.get(ticker_url, headers=ba.config.API_REQUEST_HEADERS).json()
        fiat_data_all = requests.get(fiat_data_url, headers=ba.config.API_REQUEST_HEADERS).json()
    except (simplejson.decoder.JSONDecodeError, requests.exceptions.ConnectionError):
        time.sleep(2)
        continue
Exemplo n.º 9
0
import bitcoinaverage.helpers as helpers
from bitcoinaverage.api_calculations import calculateTotalVolumes, calculateRelativeVolumes, calculateAverageRates, formatDataForAPI, writeAPIFiles, createNogoxApi, calculateGlobalAverages

if ba.server.PROJECT_PATH == '':
    ba.server.PROJECT_PATH = include_path
if ba.server.LOG_PATH == '':
    ba.server.LOG_PATH = os.path.join(ba.server.PROJECT_PATH, 'runtime', 'app.log')
if ba.server.API_DOCUMENT_ROOT == '':
    ba.server.API_DOCUMENT_ROOT = os.path.join(ba.server.PROJECT_PATH, 'api')
    ba.server.API_DOCUMENT_ROOT_NOGOX = os.path.join(ba.server.API_DOCUMENT_ROOT, 'no-mtgox')
if ba.server.WWW_DOCUMENT_ROOT == '':
    ba.server.WWW_DOCUMENT_ROOT = os.path.join(ba.server.PROJECT_PATH, 'www')
if ba.server.HISTORY_DOCUMENT_ROOT == '':
    ba.server.HISTORY_DOCUMENT_ROOT = os.path.join(ba.server.PROJECT_PATH, 'api', 'history')

helpers.write_log('script started', 'LOG')
helpers.write_js_config()
helpers.write_api_index_files()
last_fiat_exchange_rate_update = 0

while True:
    if last_fiat_exchange_rate_update < int(time.time())-FIAT_RATES_QUERY_FREQUENCY:
        helpers.write_html_currency_pages()
        helpers.write_sitemap()
        helpers.write_fiat_rates_config()
        last_fiat_exchange_rate_update = int(time.time())

    start_time = int(time.time())

    exchanges_rates, exchanges_ignored = ba.api_parsers.callAll()
Exemplo n.º 10
0
    calculateGlobalAverages,
)

if ba.server.PROJECT_PATH == "":
    ba.server.PROJECT_PATH = include_path
if ba.server.LOG_PATH == "":
    ba.server.LOG_PATH = os.path.join(ba.server.PROJECT_PATH, "runtime", "app.log")
if ba.server.API_DOCUMENT_ROOT == "":
    ba.server.API_DOCUMENT_ROOT = os.path.join(ba.server.PROJECT_PATH, "api")
    ba.server.API_DOCUMENT_ROOT_NOGOX = os.path.join(ba.server.API_DOCUMENT_ROOT, "no-mtgox")
if ba.server.WWW_DOCUMENT_ROOT == "":
    ba.server.WWW_DOCUMENT_ROOT = os.path.join(ba.server.PROJECT_PATH, "www")
if ba.server.HISTORY_DOCUMENT_ROOT == "":
    ba.server.HISTORY_DOCUMENT_ROOT = os.path.join(ba.server.PROJECT_PATH, "api", "history")

helpers.write_log("script started", "LOG")
helpers.write_js_config()
helpers.write_api_index_files()
last_fiat_exchange_rate_update = 0

while True:
    if last_fiat_exchange_rate_update < int(time.time()) - FIAT_RATES_QUERY_FREQUENCY:
        helpers.write_html_currency_pages()
        helpers.write_sitemap()
        helpers.write_fiat_rates_config()
        last_fiat_exchange_rate_update = int(time.time())

    start_time = int(time.time())

    exchanges_rates, exchanges_ignored = ba.api_parsers.callAll()
Exemplo n.º 11
0
def create_nogox_api(timestamp):
    exchanges_rates = []
    exchanges_ignored = {}

    exchanges_rates, exchanges_ignored = ba.api_parsers.callAll(ignore_mtgox=True)

    calculated_average_rates = {}
    total_currency_volumes = {}
    total_currency_volumes_ask = {}
    total_currency_volumes_bid = {}
    calculated_volumes = {}
    for currency in CURRENCY_LIST:
        calculated_average_rates[currency] = {'last': DEC_PLACES,
                                               'ask': DEC_PLACES,
                                               'bid': DEC_PLACES,
                                                }
        total_currency_volumes[currency] = DEC_PLACES
        total_currency_volumes_ask[currency] = DEC_PLACES
        total_currency_volumes_bid[currency] = DEC_PLACES
        calculated_volumes[currency] = {}

    for rate in exchanges_rates:
        for currency in CURRENCY_LIST:
            if currency in rate:
                if rate[currency]['volume'] is None:
                    rate[currency]['volume'] = DEC_PLACES

                total_currency_volumes[currency] = total_currency_volumes[currency] + rate[currency]['volume']
                if rate[currency]['ask'] is not None:
                    total_currency_volumes_ask[currency] = total_currency_volumes_ask[currency] + rate[currency]['volume']
                if rate[currency]['bid'] is not None:
                    total_currency_volumes_bid[currency] = total_currency_volumes_bid[currency] + rate[currency]['volume']

    for rate in exchanges_rates:
        for currency in CURRENCY_LIST:
            if currency in rate:
                calculated_volumes[currency][rate['exchange_name']] = {}
                calculated_volumes[currency][rate['exchange_name']]['rates'] = {'ask': rate[currency]['ask'],
                                                                                'bid': rate[currency]['bid'],
                                                                                'last': rate[currency]['last'],
                                                                                    }
                calculated_volumes[currency][rate['exchange_name']]['source'] = rate['data_source']
                if calculated_volumes[currency][rate['exchange_name']]['rates']['last'] is not None:
                    calculated_volumes[currency][rate['exchange_name']]['rates']['last'].quantize(DEC_PLACES)
                calculated_volumes[currency][rate['exchange_name']]['volume_btc'] = rate[currency]['volume'].quantize(DEC_PLACES)
                if total_currency_volumes[currency] > 0:
                    calculated_volumes[currency][rate['exchange_name']]['volume_percent'] = (rate[currency]['volume']
                        / total_currency_volumes[currency] * Decimal(100) ).quantize(DEC_PLACES)
                else:
                    calculated_volumes[currency][rate['exchange_name']]['volume_percent'] = Decimal(0).quantize(DEC_PLACES)

                if calculated_volumes[currency][rate['exchange_name']]['rates']['ask'] is not None:
                    calculated_volumes[currency][rate['exchange_name']]['rates']['ask'].quantize(DEC_PLACES)
                    if total_currency_volumes[currency] > 0:
                        calculated_volumes[currency][rate['exchange_name']]['volume_percent_ask'] = (rate[currency]['volume']
                            / total_currency_volumes_ask[currency] * Decimal(100) ).quantize(DEC_PLACES)
                    else:
                        calculated_volumes[currency][rate['exchange_name']]['volume_percent_ask'] = Decimal(0).quantize(DEC_PLACES)

                if calculated_volumes[currency][rate['exchange_name']]['rates']['bid'] is not None:
                    calculated_volumes[currency][rate['exchange_name']]['rates']['bid'].quantize(DEC_PLACES)
                    if total_currency_volumes[currency] > 0:
                        calculated_volumes[currency][rate['exchange_name']]['volume_percent_bid'] = (rate[currency]['volume']
                            / total_currency_volumes_bid[currency] * Decimal(100) ).quantize(DEC_PLACES)
                    else:
                        calculated_volumes[currency][rate['exchange_name']]['volume_percent_bid'] = Decimal(0).quantize(DEC_PLACES)

    for rate in exchanges_rates:
        for currency in CURRENCY_LIST:
            if currency in rate:
                if rate[currency]['last'] is not None:
                    calculated_average_rates[currency]['last'] = ( calculated_average_rates[currency]['last']
                                                            + rate[currency]['last'] * calculated_volumes[currency][rate['exchange_name']]['volume_percent'] / Decimal(100) )
                if rate[currency]['ask'] is not None:
                    calculated_average_rates[currency]['ask'] = ( calculated_average_rates[currency]['ask']
                                                            + rate[currency]['ask'] * calculated_volumes[currency][rate['exchange_name']]['volume_percent_ask'] / Decimal(100) )
                if rate[currency]['bid'] is not None:
                    calculated_average_rates[currency]['bid'] = ( calculated_average_rates[currency]['bid']
                                                            + rate[currency]['bid'] * calculated_volumes[currency][rate['exchange_name']]['volume_percent_bid'] / Decimal(100) )

                calculated_average_rates[currency]['last'] = calculated_average_rates[currency]['last'].quantize(DEC_PLACES)
                calculated_average_rates[currency]['ask'] = calculated_average_rates[currency]['ask'].quantize(DEC_PLACES)
                calculated_average_rates[currency]['bid'] = calculated_average_rates[currency]['bid'].quantize(DEC_PLACES)

    for currency in CURRENCY_LIST:
        try:
            calculated_average_rates[currency]['last'] = float(calculated_average_rates[currency]['last'])
        except TypeError:
            calculated_average_rates[currency]['last'] = str(calculated_average_rates[currency]['last'])
        try:
            calculated_average_rates[currency]['ask'] = float(calculated_average_rates[currency]['ask'])
        except TypeError:
            calculated_average_rates[currency]['ask'] = str(calculated_average_rates[currency]['ask'])
        try:
            calculated_average_rates[currency]['bid'] = float(calculated_average_rates[currency]['bid'])
        except TypeError:
            calculated_average_rates[currency]['bid'] = str(calculated_average_rates[currency]['bid'])
        try:
            calculated_average_rates[currency]['total_vol'] = float(total_currency_volumes[currency])
        except TypeError:
            calculated_average_rates[currency]['total_vol'] = str(total_currency_volumes[currency])

        for exchange_name in EXCHANGE_LIST:
            if exchange_name in calculated_volumes[currency]:
                try:
                    calculated_volumes[currency][exchange_name]['rates']['last'] = float(calculated_volumes[currency][exchange_name]['rates']['last'])
                except TypeError:
                    calculated_volumes[currency][exchange_name]['rates']['last'] = str(calculated_volumes[currency][exchange_name]['rates']['last'])
                try:
                    calculated_volumes[currency][exchange_name]['rates']['ask'] = float(calculated_volumes[currency][exchange_name]['rates']['ask'])
                except TypeError:
                    calculated_volumes[currency][exchange_name]['rates']['ask'] = str(calculated_volumes[currency][exchange_name]['rates']['ask'])
                try:
                    calculated_volumes[currency][exchange_name]['rates']['bid'] = float(calculated_volumes[currency][exchange_name]['rates']['bid'])
                except TypeError:
                    calculated_volumes[currency][exchange_name]['rates']['bid'] = str(calculated_volumes[currency][exchange_name]['rates']['bid'])
                try:
                    calculated_volumes[currency][exchange_name]['volume_btc'] = float(calculated_volumes[currency][exchange_name]['volume_btc'])
                except TypeError:
                    calculated_volumes[currency][exchange_name]['volume_btc'] = str(calculated_volumes[currency][exchange_name]['volume_btc'])
                try:
                    calculated_volumes[currency][exchange_name]['volume_percent'] = float(calculated_volumes[currency][exchange_name]['volume_percent'])
                except TypeError:
                    calculated_volumes[currency][exchange_name]['volume_percent'] = str(calculated_volumes[currency][exchange_name]['volume_percent'])
                if 'volume_percent_ask' in calculated_volumes[currency][exchange_name]:
                    del calculated_volumes[currency][exchange_name]['volume_percent_ask']
                if 'volume_percent_bid' in calculated_volumes[currency][exchange_name]:
                    del calculated_volumes[currency][exchange_name]['volume_percent_bid']

    try:
        all_data = {}
        all_data['timestamp'] = timestamp
        all_data['ignored_exchanges'] = exchanges_ignored
        for currency in CURRENCY_LIST:
            cur_data = {'exchanges': calculated_volumes[currency],
                        'averages': calculated_average_rates[currency],
                        }
            all_data[currency] = cur_data

        with open(os.path.join(ba.server.API_DOCUMENT_ROOT_NOGOX, API_FILES['ALL_FILE']), 'w+') as api_all_data_file:
            api_all_data_file.write(json.dumps(all_data,  indent=2, sort_keys=True, separators=(',', ': ')))

        rates_all = calculated_average_rates
        rates_all['timestamp'] = timestamp
        with open(os.path.join(ba.server.API_DOCUMENT_ROOT_NOGOX, API_FILES['TICKER_PATH'], 'all'), 'w+') as api_ticker_all_file:
            api_ticker_all_file.write(json.dumps(rates_all, indent=2, sort_keys=True, separators=(',', ': ')))

        for currency in CURRENCY_LIST:
            ticker_cur = calculated_average_rates[currency]
            ticker_cur['timestamp'] = timestamp
            api_ticker_file = open(os.path.join(ba.server.API_DOCUMENT_ROOT_NOGOX, API_FILES['TICKER_PATH'], currency), 'w+')
            api_ticker_file.write(json.dumps(ticker_cur,  indent=2, sort_keys=True, separators=(',', ': ')))
            api_ticker_file.close()

        volumes_all = calculated_volumes
        volumes_all['timestamp'] = timestamp
        with open(os.path.join(ba.server.API_DOCUMENT_ROOT_NOGOX, API_FILES['EXCHANGES_PATH'], 'all'), 'w+') as api_volume_all_file:
            api_volume_all_file.write(json.dumps(volumes_all, indent=2, sort_keys=True, separators=(',', ': ')))

        for currency in CURRENCY_LIST:
            volume_cur = calculated_volumes[currency]
            volume_cur['timestamp'] = timestamp
            api_ticker_file = open(os.path.join(ba.server.API_DOCUMENT_ROOT_NOGOX, API_FILES['EXCHANGES_PATH'], currency), 'w+')
            api_ticker_file.write(json.dumps(volume_cur,  indent=2, sort_keys=True, separators=(',', ': ')))
            api_ticker_file.close()

        with open(os.path.join(ba.server.API_DOCUMENT_ROOT_NOGOX, API_FILES['IGNORED_FILE']), 'w+') as api_ignored_file:
            api_ignored_file.write(json.dumps(exchanges_ignored,  indent=2, sort_keys=True, separators=(',', ': ')))

    except IOError as error:
        error_text = '%s, %s ' % (sys.exc_info()[0], error)
        write_log(error_text)
        print 'ERROR: %s ' % (error_text)
        raise error
Exemplo n.º 12
0
def writeAPIFiles(api_path, timestamp, calculated_average_rates_formatted, calculated_volumes_formatted,
                  calculated_global_average_rates_formatted, exchanges_ignored):
    try:
        # /all
        all_data = {}
        all_data['timestamp'] = timestamp
        all_data['ignored_exchanges'] = exchanges_ignored
        for currency in CURRENCY_LIST:
            if (currency in calculated_volumes_formatted
            and currency in calculated_average_rates_formatted
            and currency in calculated_global_average_rates_formatted):
                cur_data = {'exchanges': calculated_volumes_formatted[currency],
                            'averages': calculated_average_rates_formatted[currency],
                            'global_averages': calculated_global_average_rates_formatted[currency],
                            }
                all_data[currency] = cur_data

        with open(os.path.join(api_path, API_FILES['ALL_FILE']), 'w+') as api_all_data_file:
            api_all_data_file.write(json.dumps(all_data,  indent=2, sort_keys=True, separators=(',', ': ')))

        # /ticker/*
        for currency in CURRENCY_LIST:
            if (currency in calculated_volumes_formatted and currency in calculated_average_rates_formatted
            and currency in calculated_global_average_rates_formatted):
                ticker_cur = calculated_average_rates_formatted[currency]
                ticker_cur['timestamp'] = timestamp
                ticker_currency_path = os.path.join(api_path, API_FILES['TICKER_PATH'], currency)
                with open(os.path.join(ticker_currency_path, INDEX_DOCUMENT_NAME), 'w+') as api_ticker_file:
                    api_ticker_file.write(json.dumps(ticker_cur, indent=2, sort_keys=True, separators=(',', ': ')))
                for key in ticker_cur:
                    with open(os.path.join(ticker_currency_path, key), 'w+') as api_ticker_file:
                        api_ticker_file.write(str(ticker_cur[key]))

        # /ticker/all
        rates_all = calculated_average_rates_formatted
        rates_all['timestamp'] = timestamp
        with open(os.path.join(api_path, API_FILES['TICKER_PATH'], 'all'), 'w+') as api_ticker_all_file:
            api_ticker_all_file.write(json.dumps(rates_all, indent=2, sort_keys=True, separators=(',', ': ')))

        # /ticker/global/*
        for currency in calculated_global_average_rates_formatted:
            ticker_cur = calculated_global_average_rates_formatted[currency]
            ticker_cur['timestamp'] = timestamp
            ticker_currency_path = os.path.join(api_path, API_FILES['GLOBAL_TICKER_PATH'], currency)
            with open(os.path.join(ticker_currency_path, INDEX_DOCUMENT_NAME), 'w+') as api_ticker_file:
                api_ticker_file.write(json.dumps(ticker_cur, indent=2, sort_keys=True, separators=(',', ': ')))
            for key in ticker_cur:
                with open(os.path.join(ticker_currency_path, key), 'w+') as api_ticker_file:
                    api_ticker_file.write(str(ticker_cur[key]))

        # /ticker/global/all
        rates_all = calculated_global_average_rates_formatted
        rates_all['timestamp'] = timestamp
        try:
            with open(os.path.join(api_path, API_FILES['GLOBAL_TICKER_PATH'], 'all'), 'w+') as api_global_ticker_all_file:
                api_global_ticker_all_file.write(json.dumps(rates_all, indent=2, sort_keys=True, separators=(',', ': ')))
        except IOError as error:
            #pass on Windows if there is currency with code ALL and it will interfer with file called 'all'
            pass

        # /exchanges/all
        volumes_all = calculated_volumes_formatted
        volumes_all['timestamp'] = timestamp
        with open(os.path.join(api_path, API_FILES['EXCHANGES_PATH'], 'all'), 'w+') as api_volume_all_file:
            api_volume_all_file.write(json.dumps(volumes_all, indent=2, sort_keys=True, separators=(',', ': ')))

        # /exchanges/*
        for currency in CURRENCY_LIST:
            if (currency in calculated_volumes_formatted and currency in calculated_average_rates_formatted
                and currency in calculated_global_average_rates_formatted):
                volume_cur = calculated_volumes_formatted[currency]
                volume_cur['timestamp'] = timestamp
                api_ticker_file = open(os.path.join(api_path, API_FILES['EXCHANGES_PATH'], currency), 'w+')
                api_ticker_file.write(json.dumps(volume_cur,  indent=2, sort_keys=True, separators=(',', ': ')))
                api_ticker_file.close()

        # /ignored
        with open(os.path.join(api_path, API_FILES['IGNORED_FILE']), 'w+') as api_ignored_file:
            api_ignored_file.write(json.dumps(exchanges_ignored,  indent=2, sort_keys=True, separators=(',', ': ')))

    except IOError as error:
        error_text = '%s, %s ' % (sys.exc_info()[0], error)
        helpers.write_log(error_text)
        print 'ERROR: %s ' % (error_text)
        raise error
Exemplo n.º 13
0
from bitcoinaverage.nogox import create_nogox_api
from bitcoinaverage.api_calculations import get24hAverage

if ba.server.PROJECT_PATH == '':
    ba.server.PROJECT_PATH = include_path
if ba.server.LOG_PATH == '':
    ba.server.LOG_PATH = os.path.join(ba.server.PROJECT_PATH, 'runtime', 'app.log')
if ba.server.API_DOCUMENT_ROOT == '':
    ba.server.API_DOCUMENT_ROOT = os.path.join(ba.server.PROJECT_PATH, 'api')
    ba.server.API_DOCUMENT_ROOT_NOGOX = os.path.join(ba.server.API_DOCUMENT_ROOT, 'no-mtgox')
if ba.server.WWW_DOCUMENT_ROOT == '':
    ba.server.WWW_DOCUMENT_ROOT = os.path.join(ba.server.PROJECT_PATH, 'www')
if ba.server.HISTORY_DOCUMENT_ROOT == '':
    ba.server.HISTORY_DOCUMENT_ROOT = os.path.join(ba.server.PROJECT_PATH, 'api', 'history')

helpers.write_log('script started', 'LOG')
helpers.write_js_config()
helpers.write_api_index_files()
last_fiat_exchange_rate_update = 0

while True:
    start_time = int(time.time())

    exchanges_rates, exchanges_ignored = ba.api_parsers.callAll()

    calculated_average_rates = {}
    total_currency_volumes = {}
    total_currency_volumes_ask = {}
    total_currency_volumes_bid = {}
    calculated_volumes = {}
    for currency in CURRENCY_LIST: