-
Notifications
You must be signed in to change notification settings - Fork 0
/
pytrtbot.py
247 lines (213 loc) · 7.08 KB
/
pytrtbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import arrow
import requests
import sqlite3
from PyRock import PyRock
HEADERS = {
'User-Agent': '@TheRockBot Telegram Bot',
'content-type': 'application/json'
}
HOME = """
Select a command from the keyboard!
"""
HELP = """
You can add your API Keys to this bot, if you want! Just log into your The Rock account and generate an Api Key and an Api Secret. They don't need special permissions.
Press /delete_keys and TheRockBot will delete your API Keys.
For any issue, contact @mikexine here on Telegram."""
def Home():
return HOME
def Help():
return HELP
def Last():
last = PyRock().Ticker('btceur')
return "Last price on BTC/EUR market: %s Eur" % (last['last'])
def Ticker(fund_id):
pretty = ''
ticker = PyRock().Ticker(fund_id)
high = ticker['high']
low = ticker['low']
ask = ticker['ask']
bid = ticker['bid']
last = ticker['last']
fund_id = ticker['fund_id']
volume = ticker['volume']
pretty += ('\
Fund: %s:\n \
- Last: %s\n \
- High: %s, Low: %s\n \
- Ask: %s, Bid: %s\n \
- Volume: %s\n') % (fund_id, last, high, low,
ask, bid, (str(volume) + ' ' + fund_id[3:]))
return pretty
def bitcoinData():
url = 'http://btc.blockr.io/api/v1/coin/info'
headers = {
'User-Agent': '@TheRockBot Telegram Bot',
'content-type': 'application/json'
}
r = requests.get(url, headers=headers)
data = r.json()
lastBlock = data['data']['last_block']
blockNumber = lastBlock['nb']
timeMined = (arrow.get(lastBlock['time_utc'])).format('HH:mm, DD-MM-YYYY')
fees = '%s BTC' % lastBlock['fee']
transactions = lastBlock['nb_txs']
currentDifficulty = '%13.f' % float(lastBlock['difficulty'])
textLastBlock = '* Last block data *\n \
- Block number: %s\n \
- Mined (UTC): %s\n \
- Amount of fees: %s\n \
- Number of transactions %s\n \
- Current difficulty: %s\n\n\
' % (blockNumber, timeMined, fees, transactions, currentDifficulty)
nextDifficulty = data['data']['next_difficulty']
retargetBlock = nextDifficulty['retarget_block']
retargetIn = nextDifficulty['retarget_in']
percentageChange = nextDifficulty['perc']
forecastDifficulty = nextDifficulty['difficulty']
textNextDifficulty = '* Next difficulty *\n \
- Retarget block: %s\n \
- Retarget in %s blocks.\n \
- Percentage change in difficulty: %2.2f %%\n \
- Next expected difficulty: %13.f\n \
' % (retargetBlock, retargetIn, percentageChange, forecastDifficulty)
return textLastBlock + textNextDifficulty
def MyBalances(key, secret):
pretty = ''
rock = PyRock(key, secret)
data = rock.AllBalances()
try:
balances = data['balances']
for b in balances:
if b['balance'] != 0.0:
pretty += "%s -> Total: %.8f, Trading Balance: %.8f\n" % \
(b['currency'], b['balance'], b['trading_balance'])
if pretty is '':
pretty = 'No balance on The Rock!'
return ('/home', pretty)
except:
pretty = ('Error occured. Check your API Keys! ' +
'type /register to update them.')
return ('/register', pretty)
def MyTransactions(key, secret):
pretty = ''
rock = PyRock(key, secret)
data = rock.Transactions()
try:
transactions = data['transactions']
for transaction in transactions:
amount = transaction['price']
currency = transaction['currency']
transactionType = transaction['type'].replace('_', ' ')
date = arrow.get(transaction['date']).format('HH:mm, DD-MM-YYYY')
pretty += '- %r %s, type: %s, date: %s\n' % \
(amount, currency, transactionType, date)
if pretty is '':
pretty = 'No transactions on The Rock!'
return ('/home', pretty)
except:
pretty = ('Error occured. Check your API Keys! ' +
'type /register to update them.')
return ('/register', pretty)
def MyTrades(key, secret, fund_id):
pretty = ''
rock = PyRock(key, secret)
data = rock.UserTrades(fund_id)
try:
trades = data['trades']
for trade in trades:
amount = trade['amount']
price = trade['price']
side = trade['side']
order_id = trade['order_id']
date = arrow.get(trade['date']).format('HH:mm, DD-MM-YYYY')
pretty += '- %s %s %s %s at price = %s %s. Id: %s.\n' % \
(date, side, amount, fund_id[:3],
price, fund_id[3:], order_id)
if pretty is '':
pretty = 'No trades on The Rock with this fund!'
return ('/trades', pretty)
except:
pretty = ('Error occured. Check your API Keys! ' +
'type /register to update them.')
return ('/register', pretty)
def MyDiscount(key, secret, fund_id):
pretty = ''
rock = PyRock(key, secret)
data = rock.DiscountLevel(fund_id)
try:
pretty = 'Discount on %s: %s %%' % (fund_id, data['discount'])
return ('/discounts', pretty)
except:
pretty = ('Error occured. Check your API Keys! ' +
'type /register to update them.')
return ('/register', pretty)
def MyOrders(key, secret, fund_id):
pretty = ''
rock = PyRock(key, secret)
data = rock.ListAllOrders(fund_id)
try:
orders = data['orders']
for order in orders:
order_id = order['id']
amount = order['amount']
price = order['price']
orderType = order['type']
side = order['side']
status = order['status']
unfilledAmount = order['amount_unfilled']
date = arrow.get(order['date']).format('HH:mm, DD-MM-YYYY')
pretty += '\
* Order Id: %s\n \
- Amount: %s %s \n\
- Price: %s %s \n\
- Type: %s %s \n\
- Status: %s \n\
- Unfilled amount: %s \n\
- Date: %s\n\n' % (order_id, amount, fund_id[:3], price, fund_id[3:],
orderType, side, status, unfilledAmount, date)
if pretty is '':
pretty = 'No open orders on The Rock with this fund!'
return ('/orders', pretty)
except:
pretty = ('Error occured. Check your API Keys! ' +
'type /register to update them.')
return ('/register', pretty)
def writedb(mdict):
a, b, c, d, e, f, g, h = [0, 0, 0, 0, 0, 0, 0, 0]
con = sqlite3.connect("db/logs.db")
try:
a = mdict["message_id"]
except:
pass
try:
b = mdict["from"]["id"]
except:
pass
try:
c = mdict["from"]["username"]
except:
pass
try:
d = mdict["from"]["first_name"]
except:
pass
try:
e = mdict["from"]["last_name"]
except:
pass
try:
f = mdict["text"]
except:
pass
try:
g = mdict["chat"]["id"]
except:
pass
try:
h = arrow.utcnow().format('YYYY-MM-DD HH:mm:ss:SSS ZZ')
except:
pass
with con:
cur = con.cursor()
cur.execute("INSERT INTO log VALUES (?,?,?,?,?,?,?,?)",
(a, b, c, d, e, f, g, h))