Summary

Requirements:

  • python 3

This notebook show how to download reports from the API and display some basic stats.


In [1]:
import os
from pynexus import ReportsAPI
from pynexus.settings import APPNEXUS_ACCOUNT
from pynexus.reports.settings import network_analytics_fields, segment_load_fields

# Modify your logs
APPNEXUS_ACCOUNT.update({
        'username': "",
        'password': ""
})

In [2]:
print("Network analytics: ")
network_analytics_fields


Network analytics: 
Out[2]:
{'report': {'columns': ['hour',
   'insertion_order_id',
   'line_item_id',
   'campaign_id',
   'advertiser_id',
   'pixel_id',
   'imps',
   'imps_viewed',
   'clicks',
   'cost',
   'cpm',
   'cpm_including_fees',
   'revenue',
   'revenue_including_fees',
   'total_convs',
   'geo_country'],
  'filters': [{'geo_country': 'FR'}],
  'format': 'csv',
  'groups': ['advertiser_id', 'hour'],
  'report_interval': 'last_30_days',
  'report_type': 'network_analytics'}}

In [3]:
api = ReportsAPI(**APPNEXUS_ACCOUNT)

1. Download report

In memory


In [9]:
report = api.get_report(network_analytics_fields)


2016-09-28 11:21:23 INFO [get_report] 2.03923106 min


In [10]:
report


Out[10]:
Report(type='network_analytics', path=None, file=<_io.BytesIO object at 0x7f89f413b0f8>)

To a file


In [11]:
report = api.save_report(report_name="test", reports_folder=".",
                         report_fields=network_analytics_fields)


2016-09-28 11:25:05 INFO [save_report] 3.56154648 min


In [12]:
print([x for x in os.listdir() if x.endswith('.csv')])


['test.csv']

2. Download many reports in memory


In [10]:
reports_to_download = {
    'report_1': network_analytics_fields,
    'report_2': segment_load_fields
}

In memory


In [24]:
reports = api.get_reports(reports_to_download)


2016-09-28 11:58:33 WARNING (HTTPSConnectionPool(host='api.appnexus.com', port=443): Read timed out. (read timeout=5))... retrying (2/10)
2016-09-28 12:00:06 INFO [get_report] 1.64378906 min
2016-09-28 12:00:14 INFO [get_report] 0.12247990 min
2016-09-28 12:00:14 INFO [get_reports] 1.76671870 min


In [26]:
reports


Out[26]:
{'report_1': Report(type='network_analytics', path=None, file=<_io.BytesIO object at 0x7f1aa8039d58>),
 'report_2': Report(type='segment_load', path=None, file=<_io.BytesIO object at 0x7f1a99d966d0>)}

To a file


In [11]:
reports = api.save_reports(reports_to_download, 
                           reports_folder=".", 
                           zip_reports=True, 
                           zip_name="reports")


2016-09-28 12:28:24 INFO [get_report] 1.23152360 min
2016-09-28 12:28:35 INFO [get_report] 0.18367785 min
2016-09-28 12:28:35 INFO [get_reports] 1.41572204 min
2016-09-28 12:28:35 INFO [save_reports] 1.41762309 min


In [12]:
print([x for x in os.listdir() if x.endswith('.zip')])


['reports.zip']

In [13]:
reports


Out[13]:
'./reports.zip'

In [ ]: