-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.py
More file actions
67 lines (54 loc) · 1.68 KB
/
Copy pathrunner.py
File metadata and controls
67 lines (54 loc) · 1.68 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
import pymongo
import sys
import binance
import threading
import time
MONGODB_PORT = 27017
MONGODB_HOST = "localhost"
client = pymongo.MongoClient(MONGODB_HOST, MONGODB_PORT)
db = client.crypto
balancesCollection = db.balances
pricesCollection = db.prices
class RepeatedTimer(object):
"""Runs a function at every interval without drift due to execution time of the function."""
def __init__(self, interval, function, *args, **kwargs):
self._timer = None
self.interval = interval
self.function = function
self.args = args
self.kwargs = kwargs
self.is_running = False
self.next_call = time.time()
self.start()
def _run(self):
self.is_running = False
self.start()
self.function(*self.args, **self.kwargs)
def start(self):
if not self.is_running:
self.next_call += self.interval
self._timer = threading.Timer(self.next_call - time.time(), self._run)
self._timer.start()
self.is_running = True
def stop(self):
self._timer.cancel()
self.is_running = False
def get_balances():
balances = binance.balances()
prices = binance.prices()
epoch = time.time()
data_balances = {"balances": balances, "date": epoch}
data_prices = {"prices": prices, "date": epoch}
balancesCollection.insert_one(data_balances)
pricesCollection.insert_one(data_prices)
print("inserted data")
def main():
args = sys.argv[1:]
secret_key = args[0]
api_key = args[1]
binance.set(api_key, secret_key)
rt = RepeatedTimer(60, get_balances)
rt.start()
print("Started!")
if __name__ == "__main__":
main()