-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitNotify.py
More file actions
executable file
·161 lines (127 loc) · 4.81 KB
/
Copy pathBitNotify.py
File metadata and controls
executable file
·161 lines (127 loc) · 4.81 KB
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
# -*- coding: utf-8 -*-
import os,requests,argparse
from time import sleep
from datetime import datetime
from twitter import *
#os.system("mode con cols=24 lines=20")
#use the TwitterStream class for stop/start: https://github.com/sixohsix/twitter/tree/master
#NEED SOME TWEETING ON ERROR MESSAGES!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--threshold", help="the margin percentage to trigger a DM", type=int, default=8)
parser.add_argument("-d", "--debug", help="we got bugs, irradicate bugs with bug spray / 0",
type=int, default=0)
parser.add_argument("-dev", help="testing new features yo!",
type=int, default=0)
args = parser.parse_args()
threshold = args.threshold
debug = args.debug
dev = args.dev
if (debug): print('threshold: ', threshold)
URL_bitstamp = 'https://www.bitstamp.net/api/ticker/'
URL_USDGBP = 'http://openexchangerates.org/api/latest.json?app_id=f6eb23a351d74b44a1ae9ff7561c4a0e'
URL_Bitty = 'https://bittylicious.com/api/v1/quote/BTC/GB/GBP/BANK/1'
repeatTime = 30
def twitAuth():
t = Twitter(
auth = OAuth('2261857933-6osWHs9QAdpl69yMHLCSpNcty2voBhA1uaEcwue',
'96ue1lqn9uetCeISQZKtA1ahsODU1tU7VZ8o4xGzWZlYm',
'9VtsP5VUEuWOgQ4wkiXQ',
'3x7IyiJqdqPfS1T17xg41eASz0LMZgQFnSoMnUBnUk')
)
return t
def getDM(t):
msg = t.direct_messages(count=1)
return msg[0]['text']
def getBitstampUSD():
try:
data = requests.get(URL_bitstamp, timeout = 10).json()
USD_last = data["last"]
if debug: print('Last price = $' + USD_last)
except ValueError:
print('[Error 1001] Bitstamp is having issues, no surprise there...')
return 1
return float(USD_last)
def bitstampUSDBGP(USD_last, rate):
GBP_last = float(USD_last) / rate
if debug: print(u'And in GBP = £%.2f' % GBP_last)
return GBP_last
def getUSDGBP():
data = requests.get(URL_USDGBP).json()
USDGBP = data['rates']['GBP']
inv = 1/float(USDGBP)
if debug: print('USD/GBP rate = $%.3f\n' % inv)
return inv
def getBitty():
data = requests.get(URL_Bitty).json()
Bitty_last = data['totalPrice']
if debug: print(u'Bittylicious = £%.2f' % Bitty_last)
return Bitty_last
def bittyDelta(stamp, bitty, time):
delta = bitty - stamp
if debug: print(u'Bitty_Delta = £%.2f' % delta)
percent = ( bitty / stamp * 100 ) - 100
if debug: print(u' = %.2f%%' % percent)
return percent
def sendDM(time, info):
message = time, info
t.direct_messages.new(user = 'jamesmgillard', text = message)
def getTime():
now = datetime.now()
time = now.strftime('%d/%m %H:%M:%S')
if debug: print(time)
return time
rate = getUSDGBP()
t = twitAuth()
histList = [0,0,0,0,0,0,0,0,0,0]
repeatTemp = 0
sendLoop = True
while True:
while sendLoop == True:
time = getTime()
stampUSD = getBitstampUSD()
stampGBP = bitstampUSDBGP(stampUSD, rate)
bittyGBP = getBitty()
percent = bittyDelta(stampGBP, bittyGBP, time)
for i in range(0,9):
histList[i] = histList[i+1]
histList[9] = percent
if debug: print(histList)
total = 0
for i in range(0,10):
total += histList[i]
avg = total / 10.0
if debug: print('10 min avg: %.2f%%' % avg)
if debug: print('repeatTemp: ', repeatTemp)
# if avg dips below threshold, resets repeatTemp
if avg < threshold:
repeatTemp = repeatTime
# if avg >= threshold and still counting down, take 1 off count
if avg >= threshold and repeatTemp > 0:
repeatTemp -= 1
# if avg >= threshold and count is zero, sendDM and reset repeatTemp
if avg >= threshold and repeatTemp == 0:
print("DM sent")
sendDM(time, "BL:%.2f BS:%.2f (%.2f) -> %.2f%%" % (bittyGBP,stampGBP,stampUSD,avg))
repeatTemp = repeatTime
if debug: print('Snoozing..............')
print('\n')
sleep(60)
if(dev):
msg = getDM(t);
print(msg)
if(msg.lower() =="stop"):
print("Sleepy time")
t.direct_messages.new(user = 'jamesmgillard', text = "We're gonna stop spamming you for a 'lil while then")
sendLoop = False
while sendLoop == False:
msg = getDM(t)
if msg.lower() == "start":
print("Wakey wakey")
t.direct_messages.new(user = 'jamesmgillard', text = "Spammyspamspam time!")
sendLoop = True
histList = [0,0,0,0,0,0,0,0,0,0]
repeatTemp = 0
break
if debug: print('Sleeping..............')
print('\n')
sleep(60)