Перейти к основному содержимому

Примеры работы с API на Python

Для запуска примеров вам понадобится API-ключ, а также установленный python3 с библиотекой requests.

Посты мониторинга

Список доступных постов

import json
import os

import requests

api_url = 'https://mycityair.ru/harvester/v2/Posts'
api_key = os.getenv('CITYAIR_TOKEN') # or set your api key directly

assert api_key, 'API key is empty'

headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}',
}
response = requests.get(api_url, headers=headers)
response.raise_for_status()

formatted_response = json.dumps(response.json(), indent=2, ensure_ascii=False)
print(formatted_response)

Данные измерений отдельного поста


import datetime
import os
import json

import requests

post_list_url = 'https://mycityair.ru/harvester/v2/Posts'
api_key = os.getenv('CITYAIR_TOKEN') # or set your api key directly

assert api_key, 'API key is empty'

headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}',
}
response = requests.get(post_list_url, headers=headers)
response.raise_for_status()
posts = response.json()
assert len(posts) > 0, 'At least one post is required'
post = posts[0]
print(f'Got post {post["id"]}:{post["name"]}')


interval = '1h'
date_start = (datetime.datetime.utcnow() - datetime.timedelta(days=1)).isoformat()
post_measurements_url = f'https://mycityair.ru/harvester/v2/Posts/{post["id"]}/measurements?' \
f'interval={interval}&' \
f'date__gt={date_start}'
response = requests.get(post_measurements_url, headers=headers)
assert response.status_code == 200, f'Server responded with error: {response.text}'
response.raise_for_status()

formatted_response = json.dumps(response.json(), indent=2, ensure_ascii=False)
print(formatted_response)

Станции

Работа с источниками данных.

Список станций


import os
import json

import requests

station_list_url = 'https://mycityair.ru/harvester/v2/Stations'
api_key = os.getenv('CITYAIR_TOKEN') # or set your api key directly

assert api_key, 'API key is empty'

headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}',
}
response = requests.get(station_list_url, headers=headers)
response.raise_for_status()

formatted_response = json.dumps(response.json(), indent=2, ensure_ascii=False)
print(formatted_response)

Данные измерений станции

import os
import json

import requests
import datetime

station_list_url = 'https://mycityair.ru/harvester/v2/Stations'
api_key = os.getenv('CITYAIR_TOKEN') # or set your api key directly
assert api_key, 'API key is empty'

headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}',
}
response = requests.get(station_list_url, headers=headers)
response.raise_for_status()
stations = response.json()
assert len(stations) > 0, 'At least one station is required'
station = stations[0]
print(f'Got station {station["id"]}:{station["name"]}')

date__gt = (datetime.datetime.utcnow() - datetime.timedelta(hours=1)).isoformat()
date__lt =datetime.datetime.utcnow().isoformat()
# The default limit is 1000 packets in the data section, so use shorter intervals
post_measurements_url = f'https://mycityair.ru/harvester/v2/Stations/{station["id"]}/measurements?' \
f'date__gt={date__gt}&' \
f'date__lt={date__lt}'

response = requests.get(post_measurements_url, headers=headers)
assert response.status_code == 200, f'Server responded with error: {response.text}'
response.raise_for_status()
formatted_response = json.dumps(response.json(), indent=2, ensure_ascii=False)
print(formatted_response)