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
7711df65
Commit
7711df65
authored
Jun 21, 2016
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve poll script
parent
a7a55962
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
15 deletions
+21
-15
poll.py
poll.py
+21
-15
No files found.
poll.py
View file @
7711df65
...
@@ -7,6 +7,7 @@ import grequests
...
@@ -7,6 +7,7 @@ import grequests
import
json
import
json
from
argparse
import
ArgumentParser
from
argparse
import
ArgumentParser
# exchanges as (url, id, response_indices_last, currency)
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'
),
'btce'
,
(
'btc_usd'
,
'last'
),
'USD'
),
...
@@ -42,6 +43,8 @@ if __name__ == '__main__':
...
@@ -42,6 +43,8 @@ if __name__ == '__main__':
'(default dev/shm/tothemoon)'
)
'(default dev/shm/tothemoon)'
)
parser
.
add_argument
(
'-i'
,
'--interval'
,
metavar
=
'SECONDS=2'
,
type
=
float
,
parser
.
add_argument
(
'-i'
,
'--interval'
,
metavar
=
'SECONDS=2'
,
type
=
float
,
default
=
2
,
help
=
'exchange poll interval'
)
default
=
2
,
help
=
'exchange poll interval'
)
parser
.
add_argument
(
'-t'
,
'--timeout'
,
metavar
=
'SECONDS=7'
,
type
=
float
,
default
=
5
,
help
=
'timeout for polling'
)
parser
.
add_argument
(
'-l'
,
'--list'
,
action
=
'store_true'
,
default
=
False
,
parser
.
add_argument
(
'-l'
,
'--list'
,
action
=
'store_true'
,
default
=
False
,
help
=
'list api info'
)
help
=
'list api info'
)
args
=
parser
.
parse_args
()
args
=
parser
.
parse_args
()
...
@@ -79,8 +82,7 @@ if __name__ == '__main__':
...
@@ -79,8 +82,7 @@ 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
# poll APIs in endless loop
# scheduled after their current interval
try
:
try
:
last_update
=
0
last_update
=
0
...
@@ -89,35 +91,39 @@ if __name__ == '__main__':
...
@@ -89,35 +91,39 @@ if __name__ == '__main__':
if
diff
<
args
.
interval
:
if
diff
<
args
.
interval
:
time
.
sleep
(
args
.
interval
-
diff
)
time
.
sleep
(
args
.
interval
-
diff
)
requests
=
(
grequests
.
get
(
ex
[
0
],
timeout
=
args
.
interval
*
1.5
)
requests
=
(
grequests
.
get
(
ex
[
0
],
timeout
=
args
.
timeout
)
for
ex
in
exchanges
)
for
ex
in
exchanges
)
responses
=
grequests
.
map
(
requests
,
exception_handler
=
handle_error
)
responses
=
grequests
.
map
(
requests
,
exception_handler
=
handle_error
)
for
(
url
,
exid
,
indices
,
currency
),
res
in
zip
(
exchanges
,
responses
):
for
(
url
,
exid
,
indices
,
currency
),
res
in
zip
(
exchanges
,
responses
):
status
=
{
'last'
:
None
,
'currency'
:
currency
,
'updated'
:
0
}
if
res
is
None
:
if
res
is
None
:
print
>>
sys
.
stderr
,
exid
,
'unknown error'
print
>>
sys
.
stderr
,
'Unknown error at %s'
%
exid
elif
res
.
status_code
!=
200
:
elif
res
.
status_code
!=
200
:
print
>>
sys
.
stderr
,
'server error %d at %s'
%
(
res
.
status_code
,
exid
)
print
>>
sys
.
stderr
,
'Server error %d at %s'
%
\
(
res
.
status_code
,
exid
)
else
:
else
:
try
:
try
:
last
=
res
.
json
()
last
=
res
.
json
()
for
i
in
indices
:
for
i
in
indices
:
last
=
last
[
i
]
last
=
last
[
i
]
last
=
float
(
last
)
status
[
'updated'
]
=
time
.
time
()
status
=
{
status
[
'last'
]
=
last
'last'
:
float
(
last
),
except
ValueError
as
e
:
'currency'
:
currency
,
print
>>
sys
.
stderr
,
'invalid response from %s:'
%
exid
,
e
.
message
'updated'
:
time
.
time
()
except
KeyError
as
e
:
}
print
>>
sys
.
stderr
,
'unexpected response content from %s:'
%
exid
,
res
.
text
with
open
(
root
+
'/'
+
exid
,
'w'
)
as
f
:
with
open
(
root
+
'/'
+
exid
,
'w'
)
as
f
:
json
.
dump
(
status
,
f
)
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
last_update
=
time
.
time
()
last_update
=
time
.
time
()
except
KeyboardInterrupt
:
except
KeyboardInterrupt
:
pass
pass
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