-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyandex.py
42 lines (37 loc) · 1.38 KB
/
yandex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import os
from datetime import date, timedelta
import requests
import json
TOKEN_METRIC = os.environ['TOKEN_METRIC']
class Yandex:
url = 'https://api-metrika.yandex.ru/stat/v1/data'
headers = {
"Host": "api-metrika.yandex.net",
"Authorization": 'OAuth ' + TOKEN_METRIC,
"Content-Type": "application/x-yametrika+json",
}
@staticmethod
def get_data(metric_id, metrics):
payload_data = {
'ids': int(metric_id),
'attribution': 'first',
'metrics': ','.join(str(x) for x in metrics),
'dimensions': 'ym:s:TrafficSource',
'date1': date.today() - timedelta(days=31),
'date2': date.today() - timedelta(days=1),
'limit': 100000,
'accuracy': 'full',
'filters': "ym:s:lastSignTrafficSource=='organic' AND ym:s:isRobot=='No'"
}
response_yan = requests.get(url=Yandex.url, params=payload_data, headers=Yandex.headers)
response = []
if response_yan.status_code == 200:
result = json.loads(response_yan.text)['totals']
for metric in metrics:
if 'visits' in metric:
response.append(result[metrics.index(metric)])
else:
response.append(round(result[metrics.index(metric)], 2))
return response
else:
return None