import requests
from datetime import datetime, timedelta
headers = {
'Authorization': 'Bearer my_api_key',
'Content-Type': 'application/json'
}
# Определение временного интервала за последние 24 часа
end_time = datetime.utcnow()
start_time = end_time - timedelta(days=1)
# Преобразование временных меток в строковый формат для использования в запросе GraphQL
end_time_str = end_time.strftime('%Y-%m-%dT%H:%M:%SZ')
start_time_str = start_time.strftime('%Y-%m-%dT%H:%M:%SZ')
data = """
{
viewer {
zones(filter: {zoneTag: "my_zone_id"}) {
httpRequests1hGroups(limit: 100, filter: {datetime_geq: "%s", datetime_lt: "%s"}) {
uniq {
uniques
}
sum {
requests
}
}
}
}
}
""" % (start_time_str, end_time_str)
response = requests.post(
'https://api.cloudflare.com/client/v4/graphql',
headers=headers,
json={'query': data}
)
if response.status_code == 200:
data = response.json()
total_requests = data['data']['viewer']['zones'][0]['httpRequests1hGroups'][0]['sum']['requests']
unique_visitors = data['data']['viewer']['zones'][0]['httpRequests1hGroups'][0]['uniq']['uniques']
print("Total Requests:", total_requests)
print("Unique Visitors:", unique_visitors)
else:
print("Error:", response.status_code)