Created
January 16, 2020 00:11
-
-
Save Mikodes/982f686d29e638c6c6a73e8087ce2500 to your computer and use it in GitHub Desktop.
Revisions
-
Mikodes created this gist
Jan 16, 2020 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,103 @@ import ConfigParser import sys from datetime import datetime import MySQLdb import ccxt exchanges = ['binance', 'bitfinex', 'bittrex', 'hitbtc', 'poloniex'] exchanges_id = { 'binance': 5, 'bitfinex': 6, 'bittrex': 1, 'hitbtc': 8, 'poloniex': 2, } def update_user_account_api_is_ok(cursor, echange_id, user_id): cursor.execute('UPDATE accounts_py SET error=0 ,message="API KEY Correcta" WHERE id_user = "' + user_id + '" and exchange = ' + str(echange_id)) cursor.execute( 'UPDATE accounts SET error=0 ,message="API KEY Correcta" WHERE id_user = "' + user_id + '" and exchange = ' + str( echange_id)) def update_user_account_api_is_down(cursor, echange_id, user_id): cursor.execute('UPDATE accounts_py SET error=1 ,message="API KEY incorrecta" WHERE id_user = "' + user_id + '" and exchange = ' + str(echange_id)) cursor.execute( 'UPDATE accounts SET error=1 ,message="API KEY incorrecta" WHERE id_user = "' + user_id + '" and exchange = ' + str( echange_id)) def get_action_from_pila(cursor): cursor.execute("SELECT id_user,titulo,description,type,date,date2,status FROM pila WHERE status=0 ") return cursor.fetchall() def update_user_pila_status(cursor, user_id, status, old_status): cursor.execute( "UPDATE pila SET status = %d ,date2 = NOW() WHERE status= %d AND id_user ='%s'" % ( status, old_status, str(user_id))) def get_connection_info(): config = ConfigParser.RawConfigParser() config.read('/home/ubuntu/synapsebot/config/config.ini') # config.read('D:\Projectos\ctrading\python\config\config.ini') env = config.get('ENV', 'DB_ENV') return { 'name': config.get(env, 'DB_NAME'), 'password': config.get(env, 'DB_PASSWORD'), 'host': config.get(env, 'DB_IP'), 'user': config.get(env, 'DB_USER'), } def get_data_from_user_exhange(exhange_id, private_key, secret): exchange_key = [key for key, value in exchanges_id.iteritems() if value == exhange_id][0] exchange = getattr(ccxt, exchange_key) data = exchange({'apiKey': private_key, 'secret': secret}).fetch_balance() return data def get_user_exhanges_account(cursor, id_user): cursor.execute('SELECT * FROM accounts_py WHERE id_user = "' + id_user + '"') return cursor.fetchall() def update_user_wallet(cursor, echange_id, user_id, data_exhange): time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") cursor.execute('DELETE FROM wallets WHERE exchange=%d AND idusuario ="%s"' % (echange_id, str(user_id))) for data in data_exhange['total']: value = data_exhange.get(data) if value['total'] <= 0: continue cursor.execute("""INSERT INTO wallets (idusuario, exchange, currency, fecha,free,used) VALUES (%s, %s, %s, %s,%s,%s) """, (user_id, echange_id, data, time, value['free'], value['used'])) conexion_data = get_connection_info() db_conexion = MySQLdb.connect(conexion_data.get('host'), conexion_data.get('user'), conexion_data.get('password'), conexion_data.get('name')) cursor = db_conexion.cursor(MySQLdb.cursors.DictCursor) actions = get_action_from_pila(cursor) for action in actions: user_id = action['id_user'] update_user_pila_status(cursor, user_id, 1, 0) user_account = get_user_exhanges_account(cursor, user_id) for account in user_account: try: exhange_data = get_data_from_user_exhange(account['exchange'], account['public'], account['private']) update_user_account_api_is_ok(cursor, account['exchange'], user_id) update_user_wallet(cursor, account['exchange'], user_id, exhange_data) db_conexion.commit() except (ccxt.ExchangeError, ccxt.AuthenticationError) as error: update_user_account_api_is_down(cursor, account['exchange'], user_id) update_user_pila_status(cursor, user_id, 2, 1) db_conexion.commit() cursor.close() db_conexion.close()