Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
S
smogonet
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dominik Rosiek
smogonet
Commits
9a35b066
Commit
9a35b066
authored
Dec 20, 2016
by
Dominik Rosiek
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
ssh://gitlab.telemabk.pl/dominik/smogonet
parents
e4c65369
f07a1cb3
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
25 deletions
+24
-25
cron_update_measurements.py
cron_update_measurements.py
+24
-25
No files found.
cron_update_measurements.py
View file @
9a35b066
...
...
@@ -3,20 +3,22 @@ import requests
import
time
from
tornado.ioloop
import
IOLoop
from
functools
import
partial
from
db
import
DDBstations
,
DDBtypes
,
DDBmeasurements
,
DDBlastmeasurements
from
db
import
DDBstations
,
DDBtypes
from
dateutil.parser
import
parse
import
models
from
tornado.gen
import
coroutine
def
get_station_by_id
(
station_id
,
station_data
):
for
station
in
station_data
:
if
str
(
station
[
'api_station_id'
])
==
station_id
:
return
station
[
'station_id'
]
return
models
.
Station
.
get
(
station
[
'station_id'
])
def
get_type_by_name
(
type_name
,
type_data
):
for
type
in
type_data
:
if
type
[
'shortname'
]
==
type_name
:
return
type
[
'type_id'
]
return
models
.
Type
.
get
(
type
[
'type_id'
])
def
get_timestamp_from_datetime
(
datetime_string
):
...
...
@@ -24,33 +26,30 @@ def get_timestamp_from_datetime(datetime_string):
return
time
.
mktime
(
dt
.
timetuple
())
ioloop
=
IOLoop
.
instance
()
measurements
=
DDBmeasurements
()
last_measurements
=
DDBlastmeasurements
()
stations
=
DDBstations
()
types
=
DDBtypes
()
@
coroutine
def
main
(
ioloop
):
stations
=
DDBstations
()
types
=
DDBtypes
()
apiURL
=
"http://powietrze.malopolska.pl/_powietrzeapi/api/dane?act=danemiasta&ci_id=1"
apiURL
=
"http://powietrze.malopolska.pl/_powietrzeapi/api/dane?act=danemiasta&ci_id=1"
r
=
requests
.
get
(
apiURL
)
actual_data
=
r
.
json
()[
'dane'
][
'actual'
]
r
=
requests
.
get
(
apiURL
)
actual_data
=
r
.
json
()[
'dane'
][
'actual'
]
station_data
=
ioloop
.
run_sync
(
partial
(
stations
.
get_all
))
type_data
=
ioloop
.
run_sync
(
partial
(
types
.
get_all
))
station_data
=
ioloop
.
run_sync
(
partial
(
stations
.
get_all
))
type_data
=
ioloop
.
run_sync
(
partial
(
types
.
get_all
))
for
station_measurements
in
actual_data
:
for
station_measurements
in
actual_data
:
for
measurement
in
station_measurements
[
'details'
]:
try
:
ready_station_id
=
get_station_by_id
(
measurement
[
'st_id'
],
station_data
)
ready_type_id
=
get_type_by_name
(
measurement
[
'par_name'
],
type_data
)
ready_station
=
get_station_by_id
(
measurement
[
'st_id'
],
station_data
)
ready_type
=
get_type_by_name
(
measurement
[
'par_name'
],
type_data
)
ready_measurement_value
=
measurement
[
'o_value'
]
ready_measurement_timestamp
=
get_timestamp_from_datetime
(
measurement
[
'o_czas'
])
if
ready_station_id
and
ready_type_id
and
ready_measurement_value
and
ready_measurement_timestamp
:
ioloop
.
run_sync
(
partial
(
measurements
.
add
,
station_id
=
ready_station_id
,
type_id
=
ready_type_id
,
value
=
ready_measurement_value
,
measurement_time
=
ready_measurement_timestamp
))
ioloop
.
run_sync
(
partial
(
last_measurements
.
add
,
station_id
=
ready_station_id
,
type_id
=
ready_type_id
,
value
=
ready_measurement_value
,
measurement_time
=
ready_measurement_timestamp
))
if
ready_station
and
ready_type
and
ready_measurement_value
and
ready_measurement_timestamp
:
measurement
=
models
.
Measurement
(
station
=
ready_station
,
pollution_type
=
ready_type
,
value
=
ready_measurement_value
,
time
=
ready_measurement_timestamp
)
yield
measurement
.
save
()
except
Exception
:
pass
ioloop
=
IOLoop
.
instance
()
IOLoop
.
current
()
.
run_sync
(
main
(
ioloop
))
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