• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2#
3# Copyright 2014 Google Inc. All Rights Reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Retrieves a saved report, or a report for the specified ad client.
18
19To get ad clients, run get_all_ad_clients.py.
20
21Tags: reports.generate
22"""
23from __future__ import print_function
24
25__author__ = 'sgomes@google.com (Sérgio Gomes)'
26
27import argparse
28import sys
29
30from googleapiclient import sample_tools
31from oauth2client import client
32
33# Declare command-line flags.
34argparser = argparse.ArgumentParser(add_help=False)
35argparser.add_argument(
36    '--ad_client_id',
37    help='The ID of the ad client for which to generate a report')
38argparser.add_argument(
39    '--report_id',
40    help='The ID of the saved report to generate')
41
42
43def main(argv):
44  # Authenticate and construct service.
45  service, flags = sample_tools.init(
46      argv, 'adexchangeseller', 'v1.1', __doc__, __file__, parents=[argparser],
47      scope='https://www.googleapis.com/auth/adexchange.seller.readonly')
48
49  # Process flags and read their values.
50  ad_client_id = flags.ad_client_id
51  saved_report_id = flags.report_id
52
53  try:
54    # Retrieve report.
55    if saved_report_id:
56      result = service.reports().saved().generate(
57          savedReportId=saved_report_id).execute()
58    elif ad_client_id:
59      result = service.reports().generate(
60          startDate='2011-01-01', endDate='2011-08-31',
61          filter=['AD_CLIENT_ID==' + ad_client_id],
62          metric=['PAGE_VIEWS', 'AD_REQUESTS', 'AD_REQUESTS_COVERAGE',
63                  'CLICKS', 'AD_REQUESTS_CTR', 'COST_PER_CLICK',
64                  'AD_REQUESTS_RPM', 'EARNINGS'],
65          dimension=['DATE'],
66          sort=['+DATE']).execute()
67    else:
68      argparser.print_help()
69      sys.exit(1)
70    # Display headers.
71    for header in result['headers']:
72      print('%25s' % header['name'], end=' ')
73    print()
74
75    # Display results.
76    for row in result['rows']:
77      for column in row:
78        print('%25s' % column, end=' ')
79      print()
80
81  except client.AccessTokenRefreshError:
82    print ('The credentials have been revoked or expired, please re-run the '
83           'application to re-authorize')
84
85if __name__ == '__main__':
86  main(sys.argv)
87