-
Notifications
You must be signed in to change notification settings - Fork 0
/
topvisor.py
47 lines (41 loc) · 1.63 KB
/
topvisor.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
43
44
45
46
47
import os
from datetime import date, timedelta
import json
import requests
TOKEN_TOPVISOR = os.environ['TOKEN_TOPVISOR']
USER_ID_TOPVISOR = os.environ['USER_ID_TOPVISOR']
class TopVisor:
headers = {
"User-Id": str(USER_ID_TOPVISOR),
"Authorization": 'bearer ' + TOKEN_TOPVISOR,
"Content-Type": "application/json",
}
projects_url = 'https://api.topvisor.com/v2/json/get/projects_2/projects'
summary_url = 'https://api.topvisor.com/v2/json/get/positions_2/summary'
@staticmethod
def get_data():
json_data = {
"fields": ["url"],
"dates": [str(date.today() - timedelta(days=7)), str(date.today())],
"limit": 200,
"show_searchers_and_regions": True,
"include_positions_summary_params": {
"show_tops": 1
}
}
response_top = requests.get(url=TopVisor.projects_url, json=json_data, headers=TopVisor.headers)
return json.loads(response_top.text) if response_top.status_code == 200 else None
@staticmethod
def get_detail_data(project_id, region_index):
json_data = {
"project_id": str(project_id),
"region_index": str(region_index),
"dates": [str(date.today() - timedelta(days=7)), str(date.today())],
"limit": 200,
"show_tops": 1,
"show_visibility": 1,
"show_avg": 1,
"show_dynamics": 1,
}
response_top = requests.get(url=TopVisor.summary_url, json=json_data, headers=TopVisor.headers)
return json.loads(response_top.text) if response_top.status_code == 200 else None