Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
tothemoon
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
13
Issues
13
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
tothemoon
Commits
a9cd57fa
Commit
a9cd57fa
authored
Jun 14, 2016
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add command line arguments to poll script
parent
407be6e0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
13 deletions
+49
-13
api.py
api.py
+49
-13
No files found.
api.py
View file @
a9cd57fa
...
@@ -4,35 +4,41 @@ import os
...
@@ -4,35 +4,41 @@ import os
import
atexit
import
atexit
import
time
import
time
import
grequests
import
grequests
from
argparse
import
ArgumentParser
from
Queue
import
PriorityQueue
from
Queue
import
PriorityQueue
interval
=
3
# poll exchange tickers every 3
seconds
interval
=
2
# poll exchange tickers every 2
seconds
cap_interval
=
60
# poll check market cap every minute
cap_interval
=
60
# poll check market cap every minute
exchanges
=
{
exchanges
=
{
'https://btc-e.com/api/3/ticker/btc_usd'
:
'https://btc-e.com/api/3/ticker/btc_usd'
:
(
'btce'
,
(
'btc_usd'
,
'last'
),
'USD'
,
interval
)
,
[
'btce'
,
(
'btc_usd'
,
'last'
),
'USD'
,
interval
]
,
'https://api.gdax.com/products/BTC-USD/ticker'
:
'https://api.gdax.com/products/BTC-USD/ticker'
:
(
'coinbase'
,
(
'price'
,),
'USD'
,
interval
)
,
[
'coinbase'
,
(
'price'
,),
'USD'
,
interval
]
,
'https://www.bitstamp.net/api/v2/ticker/btcusd/'
:
'https://www.bitstamp.net/api/v2/ticker/btcusd/'
:
(
'bitstamp'
,
(
'last'
,),
'USD'
,
interval
)
,
[
'bitstamp'
,
(
'last'
,),
'USD'
,
interval
]
,
'https://api.bitfinex.com/v1/pubticker/btcusd'
:
'https://api.bitfinex.com/v1/pubticker/btcusd'
:
(
'bitfinex'
,
(
'last_price'
,),
'USD'
,
interval
)
,
[
'bitfinex'
,
(
'last_price'
,),
'USD'
,
interval
]
,
'https://data.btcchina.com/data/ticker?market=btccny'
:
'https://data.btcchina.com/data/ticker?market=btccny'
:
(
'btcchina'
,
(
'ticker'
,
'last'
),
'CNY'
,
interval
)
,
[
'btcchina'
,
(
'ticker'
,
'last'
),
'CNY'
,
interval
]
,
'http://api.huobi.com/staticmarket/ticker_btc_json.js'
:
'http://api.huobi.com/staticmarket/ticker_btc_json.js'
:
(
'huobi'
,
(
'ticker'
,
'last'
),
'CNY'
,
interval
)
,
[
'huobi'
,
(
'ticker'
,
'last'
),
'CNY'
,
interval
]
,
'https://www.okcoin.com/api/v1/ticker.do?symbol=btc_usd'
:
'https://www.okcoin.com/api/v1/ticker.do?symbol=btc_usd'
:
(
'okcoin'
,
(
'ticker'
,
'last'
),
'CNY'
,
interval
)
,
[
'okcoin'
,
(
'ticker'
,
'last'
),
'CNY'
,
interval
]
,
'https://api.kraken.com/0/public/Ticker?pair=XXBTZEUR'
:
'https://api.kraken.com/0/public/Ticker?pair=XXBTZEUR'
:
(
'kraken'
,
(
'result'
,
'XXBTZEUR'
,
'c'
,
0
),
'EUR'
,
interval
)
,
[
'kraken'
,
(
'result'
,
'XXBTZEUR'
,
'c'
,
0
),
'EUR'
,
interval
]
,
'https://api.coinmarketcap.com/v1/ticker/bitcoin/'
:
'https://api.coinmarketcap.com/v1/ticker/bitcoin/'
:
(
'coinmarketcap'
,
(
0
,
'market_cap_usd'
),
'USD'
,
cap_interval
)
,
[
'coinmarketcap'
,
(
0
,
'market_cap_usd'
),
'USD'
,
cap_interval
]
,
}
}
queue
=
PriorityQueue
(
len
(
exchanges
))
queue
=
PriorityQueue
(
len
(
exchanges
))
def
generate_requests
():
def
generate_requests
():
assert
queue
.
empty
()
for
url
in
exchanges
.
iterkeys
():
queue
.
put
((
0
,
url
))
while
True
:
while
True
:
scheduled
,
url
=
queue
.
get
()
scheduled
,
url
=
queue
.
get
()
wait
=
scheduled
-
time
.
time
()
wait
=
scheduled
-
time
.
time
()
...
@@ -43,12 +49,39 @@ def generate_requests():
...
@@ -43,12 +49,39 @@ def generate_requests():
yield
grequests
.
get
(
url
,
timeout
=
5
)
yield
grequests
.
get
(
url
,
timeout
=
5
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
for
url
in
exchanges
.
iterkeys
():
# parse command line options
queue
.
put
((
0
,
url
))
parser
=
ArgumentParser
(
description
=
'Poll bitcoin API services.'
)
parser
.
add_argument
(
'-d'
,
'--dir'
,
metavar
=
'PATH'
,
type
=
str
,
default
=
'/dev/shm/tothemoon'
,
help
=
'directory to save poll results in '
'(default dev/shm/tothemoon)'
)
parser
.
add_argument
(
'-i'
,
'--interval'
,
metavar
=
'SECONDS'
,
type
=
float
,
default
=
interval
,
help
=
'poll interval per exchange in seconds '
'(default %f)'
%
interval
)
parser
.
add_argument
(
'-l'
,
'--list'
,
action
=
'store_true'
,
default
=
False
,
help
=
'list exchange info'
)
args
=
parser
.
parse_args
()
root
=
args
.
dir
for
ex
in
exchanges
.
itervalues
():
if
ex
[
0
]
!=
'coinmarketcap'
:
ex
[
3
]
=
args
.
interval
if
args
.
list
:
for
url
,
(
exid
,
indices
,
currency
,
interval
)
in
exchanges
.
iteritems
():
print
exid
print
' url: '
,
url
print
' currency:'
,
currency
print
' lookup: response[%s]'
%
']['
.
join
(
repr
(
i
)
for
i
in
indices
)
print
' interval:'
,
interval
,
'seconds'
print
' path: %s/%s'
%
(
root
,
exid
)
sys
.
exit
(
0
)
root
=
sys
.
argv
[
1
]
if
len
(
sys
.
argv
)
==
2
else
'/dev/shm/tothemoon'
create_root
=
not
os
.
path
.
isdir
(
root
)
create_root
=
not
os
.
path
.
isdir
(
root
)
# clean up created files on exit
def
remove_files
():
def
remove_files
():
for
ex
in
exchanges
.
itervalues
():
for
ex
in
exchanges
.
itervalues
():
path
=
root
+
'/'
+
ex
[
0
]
path
=
root
+
'/'
+
ex
[
0
]
...
@@ -60,6 +93,7 @@ if __name__ == '__main__':
...
@@ -60,6 +93,7 @@ if __name__ == '__main__':
atexit
.
register
(
remove_files
)
atexit
.
register
(
remove_files
)
# create root directory if necessary
if
create_root
:
if
create_root
:
try
:
try
:
os
.
mkdir
(
root
)
os
.
mkdir
(
root
)
...
@@ -68,6 +102,8 @@ if __name__ == '__main__':
...
@@ -68,6 +102,8 @@ if __name__ == '__main__':
print
>>
sys
.
stderr
,
e
.
strerror
print
>>
sys
.
stderr
,
e
.
strerror
sys
.
exit
(
1
)
sys
.
exit
(
1
)
# keep updating URLs from priority queue, adding them back to the queue
# scheduled after their current interval
try
:
try
:
for
res
in
grequests
.
imap
(
generate_requests
()):
for
res
in
grequests
.
imap
(
generate_requests
()):
exid
,
indices
,
currency
,
interval
=
exchanges
[
res
.
request
.
url
]
exid
,
indices
,
currency
,
interval
=
exchanges
[
res
.
request
.
url
]
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment