Skip to content
This repository was archived by the owner on Aug 20, 2025. It is now read-only.

Commit 2d20651

Browse files
committed
initial commit
1 parent 7a2aeeb commit 2d20651

7 files changed

Lines changed: 267 additions & 2 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,6 @@ ENV/
9999

100100
# mypy
101101
.mypy_cache/
102+
103+
test.py
104+
.idea/**

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018 Thomas
3+
Copyright (c) 2018 tbl42
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,49 @@
11
# python-cointracking-api
2-
Python Interface for CoinTracking.info API
2+
3+
Python Interface for [CoinTracking.info API](https://cointracking.info/api/api.php)
4+
5+
# Requirements:
6+
7+
* requests
8+
* futures
9+
10+
# Install
11+
```
12+
13+
```
14+
15+
# How to Use
16+
17+
This is an example how you can use the library in a python script
18+
```
19+
#! /usr/bin/env python
20+
21+
from ctapi import CTAPI
22+
23+
api_key = <YourAPIKey>
24+
api_secret = <YourAPISecret>
25+
26+
api = CTAPI(api_key, api_secret)
27+
trades = api.getTrades()
28+
29+
if trades['success']:
30+
print 'credits left: {0}'.format(trades['result']['credits'])
31+
32+
for order in trades['result']['orders']:
33+
print 'Order ID: {0} >>> Price: {1} EUR'.format(order['order_id'], order['price'])
34+
else:
35+
print "got no orders"
36+
```
37+
38+
39+
POST /api/v1/getTrades HTTP/1.1
40+
Host: cointracking.info
41+
Connection: keep-alive
42+
Accept-Encoding: gzip, deflate
43+
Accept: */*
44+
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
45+
Key: 5f3ab6b651b5ae77ca85ea20baa955fc
46+
Sign: 33b3c257ab3dab73e593bf9ba90ee722bf257632dcdcdb26aae6ca1c0fa21c983568da463d06030581b0de596541c344c8ec337370d3eec2889657e9a03af381
47+
Content-Length: 34
48+
49+
method=getTrades&nonce=15153302975

ctapi/__init__.py

Whitespace-only changes.

ctapi/ctapi.py

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
"""
2+
See https://cointracking.info/api/api.php
3+
"""
4+
5+
try:
6+
from urllib import urlencode
7+
from urlparse import urljoin
8+
except ImportError:
9+
from urllib.parse import urlencode
10+
from urllib.parse import urljoin
11+
12+
import time
13+
import hmac
14+
import logging
15+
import hashlib
16+
17+
import requests
18+
19+
__author__ = "tbl42"
20+
__copyright__ = "tbl42 2017"
21+
__version__ = '0.1.0-dev'
22+
23+
# set logging
24+
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
25+
logging.getLogger('requests.packages.urllib3').setLevel(logging.INFO)
26+
logging.getLogger('urllib3.connectionpool').setLevel(logging.INFO)
27+
logger = logging.getLogger(__name__)
28+
29+
# disable unsecure SSL warning
30+
requests.packages.urllib3.disable_warnings()
31+
32+
URI_API = 'https://cointracking.info/api/v1/'
33+
# URI_API = 'http://127.0.0.1:8080/'
34+
35+
36+
class CTAPI(object):
37+
""" requesting CoinTracking API with API key and API secret """
38+
39+
#
40+
# init
41+
#
42+
def __init__(self, api_key='', api_secret='', debug=False):
43+
self.api_key = api_key
44+
self.api_secret = api_secret
45+
self.debug = debug
46+
47+
if self.debug:
48+
import http.client
49+
http.client.HTTPConnection.debuglevel = 1
50+
logging.getLogger('requests.packages.urllib3').setLevel(logging.DEBUG)
51+
logger.setLevel(logging.DEBUG)
52+
else:
53+
logger.setLevel(logging.INFO)
54+
55+
logger.debug("creating instance of CoinTracking API with api_key %s" % self.api_key)
56+
57+
#
58+
# encode parameters for url
59+
#
60+
def _encode_params_url(self, params):
61+
""" TODO """
62+
63+
encoded_string = ''
64+
65+
if params:
66+
# for key, value in sorted(params.items()):
67+
for key, value in params.items():
68+
encoded_string += str(key) + '=' + str(value) + '&'
69+
encoded_string = encoded_string[:-1]
70+
71+
return encoded_string
72+
73+
#
74+
# make query to API
75+
#
76+
def _api_query(self, method, params={}):
77+
""" TODO """
78+
79+
global URI_API
80+
81+
params.update({
82+
'method': method,
83+
'nonce': 1515332961, #int(time.time() * 10),
84+
})
85+
86+
params_string = self._encode_params_url(params)
87+
# params_signed = hmac.new(self.api_secret.encode(), msg='limit=1&method=getTrades&nonce=1515332961', digestmod=hashlib.sha512).hexdigest()
88+
# params_signed = hmac.new(self.api_secret.encode(), msg=params_string.encode(), digestmod=hashlib.sha512).hexdigest()
89+
params_signed = hmac.new(self.api_secret.encode(), msg='limit=1&method=getTrades&nonce=1515332961', digestmod=hashlib.sha512).hexdigest()
90+
91+
hdrs = {
92+
'Key': self.api_key,
93+
'Sign': params_signed,
94+
'Connection': 'close',
95+
# 'Content-Type': 'text/text'
96+
# 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
97+
}
98+
99+
logger.debug("="*30)
100+
logger.debug(params)
101+
logger.debug(params_string)
102+
logger.debug(params_signed)
103+
logger.debug(hdrs)
104+
logger.debug(self.api_key)
105+
logger.debug(self.api_secret)
106+
logger.debug("="*30)
107+
108+
try:
109+
# r = requests.post(URI_API, headers=hdrs, data=params_string, verify=False)
110+
r = requests.post(URI_API, headers=hdrs, data='limit=1&method=getTrades&nonce=1515332962', verify=False)
111+
ret_json = r.json()
112+
113+
return {
114+
'success': True,
115+
'result': ret_json
116+
}
117+
except:
118+
return {
119+
'success': False,
120+
'message': "error connecting to API"
121+
}
122+
123+
###########################################################################
124+
# ORDERS
125+
###########################################################################
126+
127+
#
128+
# showOrderbook
129+
#
130+
def getTrades(self, **args):
131+
""" TODO """
132+
params = {
133+
'limit': 1,
134+
# 'limit': 'all',
135+
# 'order': 'ASC',
136+
# 'start': 1200000000,
137+
# 'end': 1450000000,
138+
}
139+
params.update(args)
140+
return self._api_query('getTrades', params)
141+
142+
# async function coinTracking(method, params) {
143+
# params.method = method;
144+
# params.nonce = moment().unix();
145+
#
146+
# var post_data = http_build_query(params, {leave_brackets: false});
147+
#
148+
# var hash = crypto.createHmac('sha512', secret);
149+
# hash.update(post_data);
150+
# var sign = hash.digest('hex');
151+
#
152+
# var headers = { 'Key': key, 'Sign': sign};
153+
#
154+
# var form = new FormData();
155+
# for(var paramKey in params) {
156+
# var value = params[paramKey];
157+
# form.append(paramKey, value);
158+
# }
159+
#
160+
# var result = await fetch(url, {
161+
# method: 'POST',
162+
# body: form,
163+
# headers: headers,
164+
# });
165+
# var json = await result.json();
166+
# return json;
167+
# }
168+
#
169+
# async function getTrades() {
170+
# var params={};
171+
# params.limit=200;
172+
#
173+
# var res = await coinTracking('getTrades', params);
174+
# console.log(res);
175+
# }

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
requests==2.18.4
2+
future==0.16.0
3+
PyYAML==3.11

setup.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python
2+
3+
from setuptools import setup, find_packages
4+
5+
setup(
6+
name='python-cointracking-api',
7+
version='0.1.0-dev',
8+
packages=find_packages('ctapi'),
9+
install_requires=[
10+
'requests==2.18.4',
11+
'future==0.16.0',
12+
'PyYAML==3.11',
13+
],
14+
15+
author='tbl42',
16+
author_email='[email protected]',
17+
url='https://github.com/tbl42/python-cointracking-api',
18+
description='Python Interface for CoinTracking.info API',
19+
long_description=open('README.md').read(),
20+
keywords = 'cointracking info btc api',
21+
license='MIT',
22+
23+
classifiers=[
24+
'Programming Language :: Python',
25+
'Programming Language :: Python :: 2',
26+
'Programming Language :: Python :: 2.7',
27+
'Programming Language :: Python :: 3',
28+
'Programming Language :: Python :: 3.4',
29+
'Programming Language :: Python :: 3.5',
30+
'Programming Language :: Python :: 3.6',
31+
'Operating System :: OS Independent',
32+
'Development Status :: 3 - Alpha',
33+
'Intended Audience :: Developers',
34+
'Topic :: Office/Business :: Financial',
35+
]
36+
37+
)

0 commit comments

Comments
 (0)