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
Hide 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
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'
),
...
...
@@ -42,6 +43,8 @@ if __name__ == '__main__':
'(default dev/shm/tothemoon)'
)
parser
.
add_argument
(
'-i'
,
'--interval'
,
metavar
=
'SECONDS=2'
,
type
=
float
,
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
,
help
=
'list api info'
)
args
=
parser
.
parse_args
()
...
...
@@ -79,8 +82,7 @@ if __name__ == '__main__':
print
>>
sys
.
stderr
,
e
.
strerror
sys
.
exit
(
1
)
# keep updating URLs from priority queue, adding them back to the queue
# scheduled after their current interval
# poll APIs in endless loop
try
:
last_update
=
0
...
...
@@ -89,34 +91,38 @@ if __name__ == '__main__':
if
diff
<
args
.
interval
:
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
)
responses
=
grequests
.
map
(
requests
,
exception_handler
=
handle_error
)
for
(
url
,
exid
,
indices
,
currency
),
res
in
zip
(
exchanges
,
responses
):
status
=
{
'last'
:
None
,
'currency'
:
currency
,
'updated'
:
0
}
if
res
is
None
:
print
>>
sys
.
stderr
,
exid
,
'unknown error'
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
)
print
>>
sys
.
stderr
,
'Server error %d at %s'
%
\
(
res
.
status_code
,
exid
)
else
:
try
:
last
=
res
.
json
()
for
i
in
indices
:
last
=
last
[
i
]
last
=
float
(
last
)
status
[
'updated'
]
=
time
.
time
()
status
[
'last'
]
=
last
status
=
{
'last'
:
float
(
last
),
'currency'
:
currency
,
'updated'
:
time
.
time
()
}
with
open
(
root
+
'/'
+
exid
,
'w'
)
as
f
:
json
.
dump
(
status
,
f
)
except
ValueError
as
e
:
print
>>
sys
.
stderr
,
'invalid response from %s:'
%
exid
,
e
.
message
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
with
open
(
root
+
'/'
+
exid
,
'w'
)
as
f
:
json
.
dump
(
status
,
f
)
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