Skip to content

Commit

Permalink
Merge pull request #35 from trocafone/feature/rtbhouse-use-api-instea…
Browse files Browse the repository at this point in the history
…d-of-sdk

Use rest API instead of sdk in RTBHouse report
  • Loading branch information
gseva authored Aug 16, 2019
2 parents 5fd8ad0 + 5caf2d4 commit da26d38
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
3 changes: 0 additions & 3 deletions docs/reports.rst
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,6 @@ Example of facebook report:
RTBHouse
^^^^^^^^

.. note:: To use rtbhouse report you must install ``rtbhouse`` dependency:
``pip install laika-lib[rtbhouse]``

``type: rtbhouse``. Downloads marketing costs report from RTBHouse API.
Reported campaigns (advertisers) are all those created by the account.

Expand Down
45 changes: 31 additions & 14 deletions laika/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,24 +656,24 @@ class RTBHouseReport(FormattedReport):
"""
Retrieves marketing campaigns' costs for all the campaigns (advertisers)
for your account.
Requires rtbhouse_sdk package.
"""
group_by = None
convention_type = None
api_url = 'https://api.panel.rtbhouse.com/v3'
group_by = 'day'
convention_type = 'ATTRIBUTED'
include_dpa = True

_timeout = 60

campaign_names = {}
column_names = {}

def __init__(self, conf, **kwargs):
super(RTBHouseReport, self).__init__(conf, **kwargs)
from rtbhouse_sdk.reports_api import ReportsApiSession

logging.info('Starting RTBHouse report acquisition.')
self.formatter = FilenameFormatter(conf)

creds = get_json_credentials(self)
self.api = ReportsApiSession(creds['username'], creds['password'])
self.creds = get_json_credentials(self)

def process(self):
stats = []
Expand All @@ -692,20 +692,37 @@ def process(self):
costs_df.rename(columns=self.column_names, inplace=True)
return costs_df

def _get(self, path, **kwargs):
kwargs['timeout'] = self._timeout
kwargs['auth'] = (self.creds['username'], self.creds['password'])
res = requests.get(self.api_url + path, **kwargs)
if not res.ok:
raise ReportError(res)

res_json = res.json()
return res_json.get('data') or {}

def get_campaigns_info(self):
logging.info('Acquiring account campaigns.')
advertisers = self.api.get_advertisers()
advertisers = self._get('/advertisers')
logging.debug('{} campaigns available.'.format(len(advertisers)))
return advertisers

def get_campaigns_stats(self, campaign_id):
logging.info('Acquiring {} campaign stats.'.format(str(campaign_id)))
campaign_stats = self.api.get_campaign_stats_total(campaign_id,
self.formatter.format(self.day_from),
self.formatter.format(self.day_to),
self.group_by,
self.convention_type)
return list(campaign_stats)
params = {
'dayFrom': self.formatter.format(self.day_from),
'dayTo': self.formatter.format(self.day_to),
'groupBy': self.group_by,
'countConvention': self.convention_type,
}
if self.include_dpa:
params['includeDpa'] = 'true'

return list(self._get(
'/advertisers/' + campaign_id + '/rtb-stats',
params=params
))


class RakutenReport(FormattedReport):
Expand Down
4 changes: 1 addition & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@
adwords = ['googleads==19.0.0']
s3 = ['boto3==1.4.3']
sftp = ['paramiko==2.6.0']
rtbhouse = ['rtbhouse_sdk==3.0.1']

all_reports = excel + postgres + presto + drive + adwords + s3 + sftp + rtbhouse
all_reports = excel + postgres + presto + drive + adwords + s3 + sftp

test = ['mock==1.3.0']
docs = ['Sphinx>=1.7.1', 'sphinx-rtd-theme>=0.2.4']
Expand Down Expand Up @@ -72,7 +71,6 @@
'adwords': adwords,
's3': s3,
'sftp': sftp,
'rtbhouse': rtbhouse,

'all_reports': all_reports,

Expand Down

0 comments on commit da26d38

Please sign in to comment.