Last active
March 30, 2019 09:30
-
-
Save sweemeng/ef207a57ccf7de2cfc96ea161d470fa9 to your computer and use it in GitHub Desktop.
Generation CSV file from data for hazewatch KL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import csv | |
import dateparser | |
api_key="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InN3ZWVtZW5nIiwicmVhZF93cml0ZSI6ZmFsc2UsImlhdCI6MTU1MzIxMzkxMn0.PRofXuak0Do6hmn6rdulxmeIIZBV9P0GJbdTPe0IvZU" | |
dev_url="https://api.favoriot.com/v1/devices" | |
one_dev_url="https://api.favoriot.com/v1/devices/{dev_id}" | |
data_url_tmpl="https://api.favoriot.com/v1/devices/{dev_id}/streams" | |
headers={"apikey":api_key} | |
data_params = { "max": 1 } | |
def fetch_and_export_data(): | |
dev_req = requests.get(dev_url, headers=headers) | |
if dev_req.status_code == 200: | |
dev_data = dev_req.json() | |
with open("haze_measurement.csv", "wb") as csvfile: | |
fieldnames = ["timestamp", "device_id", "latitude", "longitude", "pm2.5", "pm10"] | |
writer = csv.DictWriter(csvfile, fieldnames=fieldnames) | |
writer.writeheader() | |
for device in dev_data["results"]: | |
dev_id = device["device_developer_id"] | |
lat = device["latitude"] | |
lon = device["longitude"] | |
data_url = data_url_tmpl.format(dev_id=dev_id) | |
data_req = requests.get(data_url, headers=headers, params=data_params) | |
print(data_req.status_code) | |
if data_req.status_code == 200: | |
data_results = data_req.json() | |
latest_date = None | |
latest_data = None | |
for result in data_results["results"]: | |
timestamp = result["stream_created_at"] | |
pm_10 = result["data"].get("pm10") | |
pm_2_5 = result["data"].get("pm2.5") | |
output = { | |
"timestamp": timestamp, | |
"device_id":dev_id, | |
"latitude": lat, | |
"longitude": lon, | |
"pm2.5": pm_2_5, | |
"pm10": pm_10 | |
} | |
if not latest_date: | |
latest_date = timestamp | |
latest_data = output | |
else: | |
if timestamp > latest_date: | |
latest_date = timestamp | |
latest_data = output | |
print(latest_date, latest_data) | |
if latest_data: | |
writer.writerow(latest_data) | |
if __name__ == "__main__": | |
fetch_and_export_data() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment