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
190cfd25
Commit
190cfd25
authored
Jun 21, 2016
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use grequests.imap in poll script
parent
78eab148
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
40 deletions
+48
-40
poll.py
poll.py
+48
-40
No files found.
poll.py
View file @
190cfd25
...
...
@@ -8,26 +8,26 @@ import json
from
argparse
import
ArgumentParser
# exchanges as (url, id, response_indices_last, currency)
exchanges
=
[
(
'https://btc-e.com/api/3/ticker/btc_usd'
,
'btce'
,
(
'btc_usd'
,
'last'
),
'USD'
),
(
'https://api.gdax.com/products/BTC-USD/ticker'
,
'coinbase'
,
(
'price'
,),
'USD'
),
(
'https://www.bitstamp.net/api/v2/ticker/btcusd/'
,
'bitstamp'
,
(
'last'
,),
'USD'
),
(
'https://api.bitfinex.com/v1/pubticker/btcusd'
,
'bitfinex'
,
(
'last_price'
,),
'USD'
),
(
'https://data.btcchina.com/data/ticker?market=btccny'
,
'btcchina'
,
(
'ticker'
,
'last'
),
'CNY'
),
(
'http://api.huobi.com/staticmarket/ticker_btc_json.js'
,
'huobi'
,
(
'ticker'
,
'last'
),
'CNY'
),
(
'https://www.okcoin.com/api/v1/ticker.do?symbol=btc_usd'
,
'okcoin'
,
(
'ticker'
,
'last'
),
'CNY'
),
(
'https://api.kraken.com/0/public/Ticker?pair=XXBTZEUR'
,
'kraken'
,
(
'result'
,
'XXBTZEUR'
,
'c'
,
0
),
'EUR'
),
(
'https://api.coinmarketcap.com/v1/ticker/bitcoin/'
,
'coinmarketcap'
,
(
0
,
'market_cap_usd'
),
'USD'
),
]
exchanges
=
{
'https://btc-e.com/api/3/ticker/btc_usd'
:
(
'btce'
,
(
'btc_usd'
,
'last'
),
'USD'
),
'https://api.gdax.com/products/BTC-USD/ticker'
:
(
'coinbase'
,
(
'price'
,),
'USD'
),
'https://www.bitstamp.net/api/v2/ticker/btcusd/'
:
(
'bitstamp'
,
(
'last'
,),
'USD'
),
'https://api.bitfinex.com/v1/pubticker/btcusd'
:
(
'bitfinex'
,
(
'last_price'
,),
'USD'
),
'https://data.btcchina.com/data/ticker?market=btccny'
:
(
'btcchina'
,
(
'ticker'
,
'last'
),
'CNY'
),
'http://api.huobi.com/staticmarket/ticker_btc_json.js'
:
(
'huobi'
,
(
'ticker'
,
'last'
),
'CNY'
),
'https://www.okcoin.com/api/v1/ticker.do?symbol=btc_usd'
:
(
'okcoin'
,
(
'ticker'
,
'last'
),
'CNY'
),
'https://api.kraken.com/0/public/Ticker?pair=XXBTZEUR'
:
(
'kraken'
,
(
'result'
,
'XXBTZEUR'
,
'c'
,
0
),
'EUR'
),
'https://api.coinmarketcap.com/v1/ticker/bitcoin/'
:
(
'coinmarketcap'
,
(
0
,
'market_cap_usd'
),
'USD'
),
}
def
handle_error
(
req
,
e
):
...
...
@@ -52,7 +52,7 @@ if __name__ == '__main__':
root
=
args
.
dir
if
args
.
list
:
for
url
,
exid
,
indices
,
currency
in
exchanges
:
for
url
,
(
exid
,
indices
,
currency
)
in
exchanges
.
iteritems
()
:
print
exid
print
' url: '
,
url
print
' currency:'
,
currency
...
...
@@ -63,7 +63,7 @@ if __name__ == '__main__':
# clean up created files on exit
def
remove_files
():
for
url
,
exid
,
indices
,
currency
in
exchanges
:
for
url
,
(
exid
,
indices
,
currency
)
in
exchanges
.
iteritems
()
:
path
=
root
+
'/'
+
exid
if
os
.
path
.
exists
(
path
):
os
.
remove
(
path
)
...
...
@@ -91,20 +91,27 @@ if __name__ == '__main__':
if
diff
<
args
.
interval
:
time
.
sleep
(
args
.
interval
-
diff
)
requests
=
(
grequests
.
get
(
ex
[
0
],
timeout
=
args
.
timeout
)
for
ex
in
exchanges
)
responses
=
grequests
.
map
(
requests
,
exception_handler
=
handle_error
)
requests
=
(
grequests
.
get
(
url
,
timeout
=
args
.
timeout
)
for
url
in
exchanges
.
iterkeys
())
for
(
url
,
exid
,
indices
,
currency
),
res
in
zip
(
exchanges
,
responses
):
for
res
in
grequests
.
imap
(
requests
,
exception_handler
=
handle_error
):
if
res
is
None
:
print
>>
sys
.
stderr
,
'Unknown error at %s'
%
exid
elif
res
.
status_code
!=
200
:
print
>>
sys
.
stderr
,
'Server error %d at %s'
%
\
(
res
.
status_code
,
exid
)
else
:
try
:
last
=
res
.
json
()
continue
exid
,
indices
,
currency
=
exchanges
[
res
.
request
.
url
]
try
:
res_json
=
res
.
json
()
if
res
.
status_code
!=
200
:
if
'error'
in
res_json
:
print
>>
sys
.
stderr
,
'Server error %d at %s: %s'
%
\
(
res
.
status_code
,
exid
,
res_json
[
'error'
])
else
:
print
>>
sys
.
stderr
,
'Server error %d at %s'
%
\
(
res
.
status_code
,
exid
)
else
:
last
=
res_json
for
i
in
indices
:
last
=
last
[
i
]
...
...
@@ -115,14 +122,15 @@ if __name__ == '__main__':
}
with
open
(
root
+
'/'
+
exid
,
'w'
)
as
f
:
print
>>
sys
.
stderr
,
'Updating'
,
exid
json
.
dump
(
status
,
f
)
except
ValueError
as
e
:
print
>>
sys
.
stderr
,
'Invalid response from %s:'
%
\
exid
,
e
.
message
except
KeyError
as
e
:
print
>>
sys
.
stderr
,
'Unexpected response '
\
'content from %s:'
%
exid
,
res
.
text
except
ValueError
as
e
:
print
>>
sys
.
stderr
,
'Invalid response from %s:'
%
\
exid
,
e
.
message
except
KeyError
as
e
:
print
>>
sys
.
stderr
,
'Unexpected response '
\
'content from %s:'
%
exid
,
res
.
text
last_update
=
time
.
time
()
except
KeyboardInterrupt
:
...
...
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