Skip to content
Permalink
Browse files

is_manager_stopping(), is_update_available()

  • Loading branch information...
bithon committed Jun 2, 2019
1 parent 8e13061 commit 173cb7b7f270c340febe74d34445a5f15ea7877e
@@ -4,9 +4,11 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 1.3.4.dev (development stage/unreleased)
## 1.3.5.dev (development stage/unreleased)

## 1.3.5
### Added
- TODO.md file
- is_manager_stopping()
### Fixed
- is_update_available returns False if github API is not available.

@@ -1,4 +1,11 @@
![GitHub release](https://img.shields.io/github/release/unicorn-data-analysis/unicorn-binance-websocket-api.svg) ![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/unicorn-data-analysis/unicorn-binance-websocket-api.svg) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/unicorn-binance-websocket-api.svg) ![code coverage 100%](https://img.shields.io/badge/coverage-100%25-brightgreen.svg) ![PyPI - Status](https://img.shields.io/pypi/status/unicorn-binance-websocket-api.svg) ![PyPI - yes](https://img.shields.io/badge/PyPI-yes-brightgreen.svg) ![PyPI - Wheel](https://img.shields.io/pypi/wheel/unicorn-binance-websocket-api.svg) ![GitHub](https://img.shields.io/github/license/unicorn-data-analysis/unicorn-binance-websocket-api.svg)
![GitHub release](https://img.shields.io/github/release/unicorn-data-analysis/unicorn-binance-websocket-api.svg)
![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/unicorn-data-analysis/unicorn-binance-websocket-api.svg)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/unicorn-binance-websocket-api.svg)
![code coverage 100%](https://img.shields.io/badge/coverage-100%25-brightgreen.svg)
![PyPI - Status](https://img.shields.io/pypi/status/unicorn-binance-websocket-api.svg)
![PyPI - yes](https://img.shields.io/badge/PyPI-yes-brightgreen.svg)
![PyPI - Wheel](https://img.shields.io/pypi/wheel/unicorn-binance-websocket-api.svg)
![GitHub](https://img.shields.io/github/license/unicorn-data-analysis/unicorn-binance-websocket-api.svg)

# UNICORN Binance WebSocket API
A python API to use the Binance Websocket API in a easy, fast, flexible, robust and fully-featured way.
@@ -15,18 +22,24 @@ processing. The current dependencies are listed
[here](https://github.com/unicorn-data-analysis/unicorn-binance-websocket-api/blob/master/requirements.txt).

Be aware that the Binance websocket API just offers to receive data. If you would like to set orders, withdraws and so
on, you have to use the [Binance Rest API](https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md) in combination.
on, you have to use the [Binance Rest API](https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md)
in combination.

Why to use the UNICORN Binance WebSocket API?

- 100% auto-reconnect!
- Streams are processing asynchronous/concurrent (python asyncio) and each stream is started in a separate thread.
- No use of the twisted module, so you can use this lib in a daemonized application (compatible with
[python-daemon](https://pypi.org/project/python-daemon/)).
- If you can not store your data in cause of a temporary technical issue, you can kick back the data to the stream_buffer which stores the receives in the RAM till you are able to process the data in the normal way again.
- If you can not store your data in cause of a temporary technical issue, you can kick back the data to the
stream_buffer which stores the receives in the RAM till you are able to process the data in the normal way again.
- Helpful management features like `get_binance_api_status()`, `get_stream_info()`, `get_stream_list()`,
`get_stream_statistic()`, `is_websocket_uri_length_valid()`, `replace_stream()`, `wait_till_stream_has_started()` and many more, explore them [here](https://www.unicorn-data.com/unicorn-binance-websocket-api.html#binance_websocket_api_docu).
- Monitoring API service and a [check_command](https://github.com/unicorn-data-analysis/unicorn-binance-websocket-api/blob/master/tools/icinga/README.md) for [ICINGA](https://exchange.icinga.com/bithon/check_binance_websocket_api_manager)/Nagios ![icinga2-demo](https://s3.gifyu.com/images/icinga2-unicorn_binance_websocket_api.png)
`get_stream_statistic()`, `is_websocket_uri_length_valid()`, `replace_stream()`, `wait_till_stream_has_started()` and
many more, explore them [here](https://www.unicorn-data.com/unicorn-binance-websocket-api.html#binance_websocket_api_docu).
- [Monitoring API service](https://www.unicorn-data.com/blog/article-details/howto-monitoring-unicorn-binance-websocket-api-manager-with-icinga2.html)
and a [check_command](https://github.com/unicorn-data-analysis/unicorn-binance-websocket-api/blob/master/tools/icinga/)
for [ICINGA](https://exchange.icinga.com/bithon/check_binance_websocket_api_manager)/Nagios
![icinga2-demo](https://s3.gifyu.com/images/icinga2-unicorn_binance_websocket_api.png)

## Installation
`pip install unicorn-binance-websocket-api`
@@ -49,11 +62,14 @@ https://github.com/unicorn-data-analysis/unicorn-binance-websocket-api
https://github.com/unicorn-data-analysis/unicorn-binance-websocket-api/wiki

## How to report bugs or suggest improvements?
First try the latest release via [download](https://github.com/unicorn-data-analysis/unicorn-binance-websocket-api/releases) or with `pip install unicorn-binance-websocket-api --upgrade`. If the issue still exists, provide the error trace, OS and python version and explain how to reproduce the error. A demo script is appreciated.
First try the latest release via [download](https://github.com/unicorn-data-analysis/unicorn-binance-websocket-api/releases)
or with `pip install unicorn-binance-websocket-api --upgrade`. If the issue still exists, provide the error trace, OS
and python version and explain how to reproduce the error. A demo script is appreciated.

Please open a new issue:
https://github.com/unicorn-data-analysis/unicorn-binance-websocket-api/issues

## Contributing
UNICORN Binance WebSocket API is an open source project which welcomes contributions which can be anything from simple
documentation fixes to new features. To contribute, fork the project on [GitHub](https://github.com/unicorn-data-analysis/unicorn-binance-websocket-api) and send a pull request.
documentation fixes to new features. To contribute, fork the project on
[GitHub](https://github.com/unicorn-data-analysis/unicorn-binance-websocket-api) and send a pull request.

This file was deleted.

@@ -101,7 +101,7 @@ def print_stream_data_from_stream_buffer(binance_websocket_api_manager):
print("waiting 30 seconds, then we start flushing the stream_buffer")
time.sleep(30)
while True:
if binance_websocket_api_manager.stop_manager_request is not None:
if binance_websocket_api_manager.is_manager_stopping():
exit(0)
oldest_stream_data_from_stream_buffer = binance_websocket_api_manager.pop_stream_data_from_stream_buffer()
if oldest_stream_data_from_stream_buffer is False:
@@ -55,7 +55,7 @@

def print_stream_data_from_stream_buffer(binance_websocket_api_manager):
while True:
if binance_websocket_api_manager.stop_manager_request is not None:
if binance_websocket_api_manager.is_manager_stopping():
exit(0)
oldest_stream_data_from_stream_buffer = binance_websocket_api_manager.pop_stream_data_from_stream_buffer()
if oldest_stream_data_from_stream_buffer is False:
@@ -51,7 +51,7 @@
def print_stream_data_from_stream_buffer(binance_websocket_api_manager):
time.sleep(10)
while True:
if binance_websocket_api_manager.stop_manager_request is not None:
if binance_websocket_api_manager.is_manager_stopping():
sys.exit(0)
oldest_stream_data_from_stream_buffer = binance_websocket_api_manager.pop_stream_data_from_stream_buffer()
if oldest_stream_data_from_stream_buffer is False:
@@ -71,7 +71,7 @@ def print_stream_data_from_stream_buffer(binance_websocket_api_manager):
print("waiting 30 seconds, then we start flushing the stream_buffer")
time.sleep(30)
while True:
if binance_websocket_api_manager.stop_manager_request is not None:
if binance_websocket_api_manager.is_manager_stopping():
exit(0)
oldest_stream_data_from_stream_buffer = binance_websocket_api_manager.pop_stream_data_from_stream_buffer()
if oldest_stream_data_from_stream_buffer is False:
@@ -63,7 +63,7 @@

def print_stream_data_from_stream_buffer(binance_websocket_api_manager):
while True:
if binance_websocket_api_manager.stop_manager_request is not None:
if binance_websocket_api_manager.is_manager_stopping():
exit(0)
oldest_stream_data_from_stream_buffer = binance_websocket_api_manager.pop_stream_data_from_stream_buffer()
if oldest_stream_data_from_stream_buffer is False:
@@ -40,7 +40,8 @@

# get version of the used UNICORN Binance WebSocket API
if binance_websocket_api_manager.is_update_availabe():
print("please upgrade to " + binance_websocket_api_manager.get_latest_version() + ", you are on", binance_websocket_api_manager.get_version())
print("please upgrade to " + binance_websocket_api_manager.get_latest_version() + ", you are on",
binance_websocket_api_manager.get_version())

latest_release_info = binance_websocket_api_manager.get_latest_release_info()
if latest_release_info:
@@ -1,5 +1,6 @@
cheroot
colorama
flask
flask_restful
pathlib
requests
@@ -40,15 +40,16 @@

setuptools.setup(
name='unicorn-binance-websocket-api',
version='1.3.4',
version='1.3.5',
author="UNICORN Data Analysis",
url="https://www.unicorn-data.com",
scripts=['unicorn_binance_websocket_api.py'],
description="A python API to use the Binance Websocket API in a easy, fast, flexible, robust and fully-featured way.",
long_description=long_description,
long_description_content_type="text/markdown",
license='MIT License',
install_requires=['colorama', 'pathlib', 'requests', 'websocket-client', 'websockets', 'flask_restful', 'cheroot'],
install_requires=['colorama', 'pathlib', 'requests', 'websocket-client', 'websockets', 'flask_restful', 'cheroot',
'flask'],
keywords='unicorn-data-analysis, binance, asyncio, async, asynchronous, concurrent, websocket-api, webstream-api, '
'binance-websocket, binance-webstream, webstream, websocket, api',
project_urls={
@@ -46,7 +46,8 @@
scripts=['unicorn_binance_websocket_api.py'],
description="Development package in testing stage! Please use https://pypi.org/project/unicorn-binance-websocket-api/",
license='MIT License',
install_requires=['colorama', 'pathlib', 'requests', 'websocket-client', 'websockets', 'flask_restful', 'cheroot'],
install_requires=['colorama', 'pathlib', 'requests', 'websocket-client', 'websockets', 'flask_restful', 'cheroot',
'flask'],
keywords='unicorn-data-analysis, binance, asyncio, async, asynchronous, concurrent, websocket-api, webstream-api, '
'binance-websocket, binance-webstream, webstream, websocket, api',
project_urls={
@@ -41,7 +41,7 @@ import urllib3


def status_unkown():
print("BINANCE WEBSOCKETS - UNKNOWN: not connected to monitoring API service!")
print("BINANCE WEBSOCKETS - UNKNOWN: not connected!")
exit(3)


@@ -55,12 +55,13 @@ parser = ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
parser.add_argument('-H', '--hostname', dest='hostname', help='host name or ip address (default: 127.0.0.1)',
default="127.0.0.1")
parser.add_argument('-P', '--port', dest='port', help='port number (default: 64201)', default=64201)
parser.add_argument('-R', dest='req_cert', help='Require a valid certificate (use with -S)', action="store_true")
parser.add_argument('-S', '--ssl', dest='ssl', help='use https prefix instead of http', action="store_true")
parser.add_argument('-V', '--version', dest='version', help='print version information', action="store_true")
parsed_args = parser.parse_args()

if parsed_args.version is True:
print("check_binance_websocket_api_manager 0.1.1 for ICINGA/Nagios by UNICORN Data Analysis 2019")
print("check_binance_websocket_api_manager 0.1.2 for ICINGA/Nagios by UNICORN Data Analysis 2019")
exit(0)

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
@@ -70,8 +71,14 @@ if parsed_args.ssl is True:
else:
protocol = "http"

if parsed_args.req_cert is True:
verify = True
else:
verify = False

try:
respond = requests.get(protocol + '://' + str(parsed_args.hostname) + ':' + str(parsed_args.port) + '/status/icinga', verify=False)
respond = requests.get(protocol + '://' + str(parsed_args.hostname) + ':' +
str(parsed_args.port) + '/status/icinga', verify=verify)
status = respond.json()
if status['text']:
print(status['text'])
@@ -80,3 +87,5 @@ try:
status_unkown()
except requests.exceptions.ConnectionError:
status_unkown()
except KeyError:
status_unkown()
@@ -68,7 +68,7 @@ class BinanceWebSocketApiManager(threading.Thread):

def __init__(self, process_stream_data=False):
threading.Thread.__init__(self)
self.version = "1.3.4.dev"
self.version = "1.3.5.dev"
self.websocket_base_uri = "wss://stream.binance.com:9443/"
if process_stream_data is False:
# no special method to process stream data provided, so we use write_to_stream_buffer:
@@ -664,7 +664,7 @@ def get_keep_max_received_last_second_entries(self):
"""
return self.keep_max_received_last_second_entries

def get_monitoring_status_icinga(self):
def get_monitoring_status_icinga(self, **kwargs):
"""
Get status and perfdata to monitor and collect metrics with ICINGA/Nagios
@@ -679,6 +679,7 @@ def get_monitoring_status_icinga(self):
- stream_buffer size
- stream_buffer items
- reconnects
- uptime
:return: dict (text, time, return_code)
"""
@@ -692,7 +693,8 @@ def get_monitoring_status_icinga(self):
str(result['average_speed_per_second']) + ";;;0 " \
"received_mb=" + str(result['total_received_mb']) + ";;;0 stream_buffer_mb=" + \
str(int(result['stream_buffer_mb'])) + ";;;0 stream_buffer_items=" + \
str(result['stream_buffer_items']) + ";;;0 reconnects=" + str(result['reconnects']) + ";;;0"
str(result['stream_buffer_items']) + ";;;0 reconnects=" + str(result['reconnects']) + ";;;0 " \
"uptime_days=" + str(result['uptime']) + ";;;0"
status = {'text': check_message,
'time': int(result['timestamp']),
'return_code': result['return_code']}
@@ -703,7 +705,7 @@ def get_monitoring_status_plain(self):
Get plain monitoring status data:
active_streams, crashed_streams, restarting_streams, stopped_streams, return_code, status_text,
timestamp, update_msg, average_receives_per_second, average_speed_per_second, total_received_mb,
stream_buffer_items, stream_buffer_mb, reconnects
stream_buffer_items, stream_buffer_mb, reconnects, uptime
:return: dict
"""
@@ -737,15 +739,16 @@ def get_monitoring_status_plain(self):
result['status_text'] = "WARNING"
result['return_code'] = 1
result['average_receives_per_second'] = (self.total_receives - self.monitoring_total_receives) / time_period
result['average_speed_per_second'] = int(((self.total_received_bytes - self.monitoring_total_received_bytes) /
time_period) / 1024)
result['total_received_mb'] = int(self.get_total_received_bytes() / (1024 * 1024))
result['average_speed_per_second'] = (((self.total_received_bytes - self.monitoring_total_received_bytes) /
time_period) / 1024).__round__(2)
result['total_received_mb'] = (self.get_total_received_bytes() / (1024 * 1024)).__round__(2)
result['stream_buffer_items'] = str(len(self.stream_buffer))
result['stream_buffer_mb'] = self.get_stream_buffer_byte_size() / (1024 * 1024)
result['stream_buffer_mb'] = (self.get_stream_buffer_byte_size() / (1024 * 1024)).__round__(2)
result['reconnects'] = self.get_reconnects()
self.monitoring_total_receives = self.get_total_receives()
self.monitoring_total_received_bytes = self.get_total_received_bytes()
self.last_monitoring_check = result['timestamp']
result['uptime'] = ((result['timestamp'] - self.start_time) / (60*60*24)).__round__(2) # days ...
return result

def get_reconnects(self):
@@ -922,6 +925,17 @@ def increase_reconnect_counter(self, stream_id):
self.stream_list[stream_id]['reconnects'] += 1
self.reconnects += 1

def is_manager_stopping(self):
"""
Returns `True` if the manager has a stop request, 'False' if not.
:return: bool
"""
if self.stop_manager_request is None:
return False
else:
return True

def is_stop_request(self, stream_id):
"""
Has a specific stream a stop_request?
@@ -933,7 +947,7 @@ def is_stop_request(self, stream_id):
logging.debug("BinanceWebSocketApiManager->is_stop_request(" + str(stream_id) + ")")
if self.stream_list[stream_id]['stop_request'] is True:
return True
elif self.stop_manager_request is True:
elif self.is_manager_stopping():
return True
else:
return False

0 comments on commit 173cb7b

Please sign in to comment.
You can’t perform that action at this time.