· 5 years ago · Oct 17, 2019, 04:26 PM
1import requests
2import json
3import pandas as pd
4import numpy as np
5from pandas import Series,DataFrame
6
7import sys
8
9if sys.version_info < (3,):
10 def u(x):
11 try:
12 return x.encode("utf8")
13 except UnicodeDecodeError:
14 return x
15else:
16 def u(x):
17 if type(x) == type(b''):
18 return x.decode('utf8')
19 else:
20 return x
21
22class Market():
23 def __init__(self, token, fromDate, toDate):
24 self.token = token
25 self.fromDate = fromDate
26 self.toDate = toDate
27 self.client_id = '61a29aeaecb74f7cb3fbc5cb65e92b4b'
28 self.headers = {
29 'Authorization': 'OAuth oauth_token=' + token + ', oauth_client_id=' + self.client_id,
30 }
31
32 def campaigns(self):
33 url = 'https://api.partner.market.yandex.ru/v2/campaigns.json'
34 r = requests.get(url, headers=self.headers)
35 data = json.loads(r.text)
36 for item in data['campaigns']:
37 ids = item['clientId']
38 ids = str(ids)
39 self.stats(ids)
40
41 def stats(self, ids):
42 url = 'https://api.partner.market.yandex.ru/v2/campaigns/' + ids + '/stats/main.json?fromDate=' + self.fromDate + '&toDate=' + self.toDate
43 r = requests.get(url, headers=self.headers)
44 data = r.text
45 temp = json.loads(data)
46 df = pd.DataFrame(temp['mainStats'])
47 print(df)
48
49my_stat = Market("AQAAAAABICwvAADtAOwL0hPVsU1um5erAMR73nI", "8-10-2019", "10-10-2019")
50
51print my_stat.campaigns()