Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tothemoon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Taddeüs Kroes
tothemoon
Commits
407be6e0
Commit
407be6e0
authored
8 years ago
by
Taddeüs Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Implement simple polling server that updates files in shared memory
parent
1b914140
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
api.py
+72
-45
72 additions, 45 deletions
api.py
with
72 additions
and
45 deletions
api.py
100644 → 100755
+
72
−
45
View file @
407be6e0
import
requests
#!/usr/bin/env python
import
sys
import
os
import
atexit
import
time
import
grequests
from
Queue
import
PriorityQueue
class
APIError
(
RuntimeError
):
pass
interval
=
3
# poll exchange tickers every 3 seconds
cap_interval
=
60
# poll check market cap every minute
def
get_check_status
(
errstr
,
url
,
*
args
):
r
=
requests
.
get
(
url
,
timeout
=
10.0
,
*
args
)
if
r
.
status_code
!=
200
:
raise
APIError
(
errstr
)
return
r
.
json
()
exchanges
=
{
'
https://btc-e.com/api/3/ticker/btc_usd
'
:
(
'
btce
'
,
(
'
btc_usd
'
,
'
last
'
),
'
USD
'
,
interval
),
'
https://api.gdax.com/products/BTC-USD/ticker
'
:
(
'
coinbase
'
,
(
'
price
'
,),
'
USD
'
,
interval
),
'
https://www.bitstamp.net/api/v2/ticker/btcusd/
'
:
(
'
bitstamp
'
,
(
'
last
'
,),
'
USD
'
,
interval
),
'
https://api.bitfinex.com/v1/pubticker/btcusd
'
:
(
'
bitfinex
'
,
(
'
last_price
'
,),
'
USD
'
,
interval
),
'
https://data.btcchina.com/data/ticker?market=btccny
'
:
(
'
btcchina
'
,
(
'
ticker
'
,
'
last
'
),
'
CNY
'
,
interval
),
'
http://api.huobi.com/staticmarket/ticker_btc_json.js
'
:
(
'
huobi
'
,
(
'
ticker
'
,
'
last
'
),
'
CNY
'
,
interval
),
'
https://www.okcoin.com/api/v1/ticker.do?symbol=btc_usd
'
:
(
'
okcoin
'
,
(
'
ticker
'
,
'
last
'
),
'
CNY
'
,
interval
),
'
https://api.kraken.com/0/public/Ticker?pair=XXBTZEUR
'
:
(
'
kraken
'
,
(
'
result
'
,
'
XXBTZEUR
'
,
'
c
'
,
0
),
'
EUR
'
,
interval
),
'
https://api.coinmarketcap.com/v1/ticker/bitcoin/
'
:
(
'
coinmarketcap
'
,
(
0
,
'
market_cap_usd
'
),
'
USD
'
,
cap_interval
),
}
def
poll_btce
():
url
=
'
https://btc-e.com/api/3/ticker/btc_usd
'
return
float
(
get_check_status
(
'
BTC-e
'
,
url
)[
'
btc_usd
'
][
'
last
'
]),
'
USD
'
queue
=
PriorityQueue
(
len
(
exchanges
))
def
poll_coinbase
():
url
=
'
https://api.gdax.com/products/BTC-USD/ticker
'
return
float
(
get_check_status
(
'
Coinbase
'
,
url
)[
'
price
'
]),
'
USD
'
def
generate_requests
():
while
True
:
scheduled
,
url
=
queue
.
get
()
wait
=
scheduled
-
time
.
time
()
def
poll_bitstamp
():
url
=
'
https://www.bitstamp.net/api/v2/ticker/btcusd/
'
return
float
(
get_check_status
(
'
Bitstamp
'
,
url
)[
'
last
'
]),
'
USD
'
if
wait
>
0
:
time
.
sleep
(
wait
)
def
poll_bitfinex
():
url
=
'
https://api.bitfinex.com/v1/pubticker/btcusd
'
return
float
(
get_check_status
(
'
Bitfinex
'
,
url
)[
'
last_price
'
]),
'
USD
'
yield
grequests
.
get
(
url
,
timeout
=
5
)
def
poll_btcchina
()
:
url
=
'
https://data.btcchina.com/data/ticker?market=btccny
'
return
float
(
get_check_status
(
'
BTCChina
'
,
url
)[
'
ticker
'
][
'
last
'
]),
'
CNY
'
if
__name__
==
'
__main__
'
:
for
url
in
exchanges
.
iterkeys
():
queue
.
put
((
0
,
url
))
def
poll_huobi
():
url
=
'
http://api.huobi.com/staticmarket/ticker_btc_json.js
'
return
float
(
get_check_status
(
'
Huobi
'
,
url
)[
'
ticker
'
][
'
last
'
]),
'
CNY
'
root
=
sys
.
argv
[
1
]
if
len
(
sys
.
argv
)
==
2
else
'
/dev/shm/tothemoon
'
create_root
=
not
os
.
path
.
isdir
(
root
)
def
poll_okcoin
():
url
=
'
https://www.okcoin.com/api/v1/ticker.do?symbol=btc_usd
'
return
float
(
get_check_status
(
'
OKcoin
'
,
url
)[
'
ticker
'
][
'
last
'
]),
'
CNY
'
def
remove_files
():
for
ex
in
exchanges
.
itervalues
():
path
=
root
+
'
/
'
+
ex
[
0
]
if
os
.
path
.
exists
(
path
):
os
.
remove
(
path
)
def
poll_kraken
():
url
=
'
https://api.kraken.com/0/public/Ticker?pair=XXBTZEUR
'
return
float
(
get_check_status
(
'
Kraken
'
,
url
)[
'
result
'
][
'
XXBTZEUR
'
][
'
c
'
][
0
]),
'
EUR
'
if
create_root
:
os
.
rmdir
(
root
)
def
poll_coinmarketcap
():
url
=
'
https://api.coinmarketcap.com/v1/ticker/bitcoin
'
return
get_check_status
(
'
Coinmarketcap
'
,
url
)[
0
][
'
market_cap_usd
'
],
'
USD
'
atexit
.
register
(
remove_files
)
if
__name__
==
'
__main__
'
:
print
'
btc-e
'
,
poll_btce
()
print
'
coinbase
'
,
poll_coinbase
()
print
'
bitstamp
'
,
poll_bitstamp
()
print
'
bitfinex
'
,
poll_bitfinex
()
print
'
btcchina
'
,
poll_btcchina
()
print
'
huobi
'
,
poll_huobi
()
print
'
okcoin
'
,
poll_okcoin
()
print
'
kraken
'
,
poll_kraken
()
print
'
coinmarketcap
'
,
poll_coinmarketcap
()
if
create_root
:
try
:
os
.
mkdir
(
root
)
except
OSError
as
e
:
print
>>
sys
.
stderr
,
'
Could not create root directory %s:
'
%
root
,
print
>>
sys
.
stderr
,
e
.
strerror
sys
.
exit
(
1
)
try
:
for
res
in
grequests
.
imap
(
generate_requests
()):
exid
,
indices
,
currency
,
interval
=
exchanges
[
res
.
request
.
url
]
queue
.
put
((
time
.
time
()
+
interval
,
res
.
request
.
url
))
last
=
res
.
json
()
for
i
in
indices
:
last
=
last
[
i
]
last
=
float
(
last
)
with
open
(
root
+
'
/
'
+
exid
,
'
w
'
)
as
f
:
print
>>
f
,
last
,
currency
except
KeyboardInterrupt
:
pass
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment