-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcoin_manage.py
120 lines (100 loc) · 3.18 KB
/
coin_manage.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
#import logging
#logging.getLogger('requests').setLevel(logging.WARNING)
#logging.basicConfig(level=logging.DEBUG)
import sys
from colorama import init as color_init
from socketIO_client import SocketIO
from settings import COIN_CAP_HOST, COIN_CAP_PORT, YB_HOST
from libs.trading_centers.coincap.namespaces import CoinNamespace
from libs.trading_centers.yunbi.ticker import BaseTicker
from libs.trading_centers.poloniex.subscriber import BaseSubscriber, SubscribeTrollbox, SubscribeTicker
from utils.db.models import db, CoinCapBTCTrans, CoinCapAltTrans, YunbiTrans, PoloniexTrans
from utils.db.models import user_db, User
from utils.watcher import getPrice
from twisted.python import log
from twisted.internet import reactor
from autobahn.twisted.websocket import WebSocketClientFactory
from libs.websocket.server import BroadcastServerProtocol, BroadcastServerFactory
from libs.websocket.client import SyncClientProtocol
# Initialize Color Library
color_init()
db.connect()
# Initialize DataBase
try:
db.create_tables([PoloniexTrans])
db.create_tables([YunbiTrans])
db.create_tables([CoinCapBTCTrans, CoinCapAltTrans])
except Exception:
print "[USING EXISITNG TABLE]"
try:
do = sys.argv[1]
target = sys.argv[2]
except Exception:
print "Error"
def init_user_db():
user_db.connect()
# Initialize DataBase
try:
db.create_tables([User])
except Exception:
print "[USING EXISITNG TABLE]"
####################
# Market Input
####################
# t1 = threading.Thread(target=BaseSubscriber.run, args=(SubscribeTicker))
# t1.start()
# t1.join()
# Poloniex
def runPoloniex():
# BaseSubscriber.run(SubscribeTrollbox)
BaseSubscriber.run(SubscribeTicker)
# CoinCap
def runCoinCap():
coin_socketIO = SocketIO(COIN_CAP_HOST, COIN_CAP_PORT, CoinNamespace)
coin_socketIO.wait()
# Yunbi
def runYunbi():
yb = BaseTicker(YB_HOST)
yb.init()
yb.tickforever()
# Watch
def watch():
print 'yunbi btc/sc', format(getPrice('yunbi', 'sc/cny') / getPrice('yunbi', 'btc/cny'), '.10f')
print 'polon btc/sc', format(getPrice('poloniex', 'btc/sc'), '.10f')
print 'yunbi btc/cny', format(getPrice('yunbi', 'btc/cny'), '.10f')
print 'polon btc/usd', format(getPrice('poloniex', 'usdt/btc'), '.10f')
def start_ws_server(url):
factory = BroadcastServerFactory(url)
factory.protocol = BroadcastServerProtocol
reactor.listenTCP(9000, factory)
reactor.run()
def start_ws_client(url):
factory = WebSocketClientFactory(url)
factory.protocol = SyncClientProtocol
reactor.connectTCP("sync.vaul.tech", 9000, factory)
reactor.run()
if do == 'initialize':
if target == 'user_db':
init_user_db()
if do == 'sync':
if target == 'yunbi':
runYunbi()
if target == 'poloniex':
runPoloniex()
if target == 'coincap':
runCoinCap()
if do == 'start':
if target == 'watch':
watch()
if target == 'wsserver':
try:
url = sys.argv[3]
start_ws_server(url)
except Exception:
print "error"
if target == 'wsclient':
try:
url = sys.argv[3]
start_ws_client(url)
except Exception:
print "error"