본문 바로가기
Block Chain

bithumb 거래소][ 이더리움 10초마다 opening price값 가져오기

by SpeeDr00t 2017. 9. 14.
반응형

bithumb 거래소][ 이더리움 10초마다 opening price값 가져오기


1. timer.py


import threading
import sys
import locale
from xcoin_api_client import *
import pprint

locale.setlocale(locale.LC_ALL, '')



class Cbithumb :


    def __init__(self) :

        print("Bithumb Public API URI('/public/ticker') Request...");

        self.api_key = "api_connect_key";
        self.api_secret = "api_secret_key";
        self.api = XCoinAPI( self.api_key, self.api_secret);



    def coin_price(self) :

        rgParams = {
                "order_currency" : "ALL",
             	"payment_currency" : "KRW"
        };


        eth_result  = self.api.xcoinApiCall("/public/ticker", rgParams, "ETH" ); 

        eth_open_price    = eth_result["data"]["opening_price"]
        print("- eth Opening Price : " + locale.format( "%3f", float(eth_open_price ) , grouping=True) )



class CTask:
    def __init__(self):
        
        self.bithumb = Cbithumb()



    def doWork(self):
        self.bithumb.coin_price()
        threading.Timer(10,self.doWork()).start()


def start():
    at = CTask()
    at.doWork()

if __name__ == '__main__':
    start()


 


2. xcoin_price_client.py



#
# @brief XCoin API-call related functions (for Python 2.x, 3.x)
#
# @author btckorea
# @date 2017-04-14
#

import sys
import time
import math
import base64
import hmac, hashlib

PY3 = sys.version_info[0] > 2;
if PY3:
	import urllib.parse
else:
	import urllib

import pycurl
import json


class XCoinAPI:
	api_url = "https://api.bithumb.com";
	api_key = "";
	api_secret = "";

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

	def http_body_callback(self, buf):
		self.contents = buf;

	def microtime(self, get_as_float = False):
		if get_as_float:
			return time.time();
		else:
			return '%f %d' % math.modf(time.time());

	def microsectime(self) :
		mt = self.microtime(False);
		mt_array = mt.split(" ")[:2];
		return mt_array[1] + mt_array[0][2:5];

	def xcoinApiCall(self, endpoint, rgParams , coin_name ):
		# 1. Api-Sign and Api-Nonce information generation.
		# 2. Request related information from the Bithumb API server.
		#
		# - nonce: it is an arbitrary number that may only be used once. (Microseconds)
		# - api_sign: API signature information created in various combinations values.

		endpoint_item_array = {
			"endpoint" : endpoint
		};

		uri_array = dict(endpoint_item_array, **rgParams); # Concatenate the two arrays.
		if PY3:
			e_uri_data = urllib.parse.urlencode(uri_array);
		else:
			e_uri_data = urllib.urlencode(uri_array);
                        print e_uri_data

		# Api-Nonce information generation.
		nonce = self.microsectime();

		# Api-Sign information generation.
		hmac_key = self.api_secret;
		utf8_hmac_key = hmac_key.encode('utf-8');

		hmac_data = endpoint + chr(0) + e_uri_data + chr(0) + nonce;

                print "hmac_data = " + hmac_data

		utf8_hmac_data = hmac_data.encode('utf-8');

		hmh = hmac.new(bytes(utf8_hmac_key), utf8_hmac_data, hashlib.sha512);
		hmac_hash_hex_output = hmh.hexdigest();
		utf8_hmac_hash_hex_output = hmac_hash_hex_output.encode('utf-8');
		utf8_hmac_hash = base64.b64encode(utf8_hmac_hash_hex_output);

		api_sign = utf8_hmac_hash;
		utf8_api_sign = api_sign.decode('utf-8');

		# Connects to Bithumb API server and returns JSON result value.
		curl_handle = pycurl.Curl();
		curl_handle.setopt(pycurl.POST, 1);
		#curl_handle.setopt(pycurl.VERBOSE, 1); # vervose mode :: 1 => True, 0 => False
		curl_handle.setopt(pycurl.POSTFIELDS, e_uri_data);

		url = self.api_url + endpoint + "/" + coin_name 
                print "****url = " + url
		curl_handle.setopt(curl_handle.URL, url);

		curl_handle.setopt(curl_handle.HTTPHEADER, ['Api-Key: ' + self.api_key, 'Api-Sign: ' + utf8_api_sign, 'Api-Nonce: ' + nonce]);
		curl_handle.setopt(curl_handle.WRITEFUNCTION, self.http_body_callback);
		curl_handle.perform();

		#response_code = curl_handle.getinfo(pycurl.RESPONSE_CODE); # Get http response status code.

		curl_handle.close();


                #print len(self.contents)
		return (json.loads(self.contents));

 

2. 결과



반응형