-
Notifications
You must be signed in to change notification settings - Fork 1
/
crexprice_thread.py
executable file
·51 lines (36 loc) · 1.24 KB
/
crexprice_thread.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
#!/usr/bin/python
import urllib, urllib2, simplejson, threading, pymongo, datetime
def getPrice():
threading.Timer(30.0, getPrice).start()
### DB stuffs
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["qbot"]
mycol = mydb["crexprice"]
### time
curtime = datetime.datetime.now()
#print curtime
### URL data/fetch loop
url = 'https://api.crex24.com/v2/public/tickers'
class MyException(Exception):
pass
try:
response = urllib2.urlopen(url, timeout = 5)
except urllib2.URLError, e:
raise MyException("There was an error: %r" % e)
data = simplejson.load(response)
#mycol.drop()
for line in data:
pair = line['instrument']
last = line['last']
percentchange = line['percentChange']
low = line['low']
high = line['high']
basevol = line['baseVolume']
quotevol = line['quoteVolume']
volbtc = line['volumeInBtc']
volusd = line['volumeInUsd']
ask = line['ask']
bid = line['bid']
datains = { "pair": pair, "last": last, "percentchange": percentchange, "low": low, "high": high, "basevol": basevol, "quotevol": quotevol, "volbtc": volbtc, "volusd": volusd, "ask": ask, "bid": bid, "date": curtime}
mycol.insert_one(datains)
getPrice()