|
26 | 26 | """ |
27 | 27 | from __future__ import print_function |
28 | 28 |
|
29 | | -__author__ = 'sgomes@google.com (Sérgio Gomes)' |
| 29 | +__author__ = "sgomes@google.com (Sérgio Gomes)" |
30 | 30 |
|
31 | 31 | import argparse |
32 | 32 | import sys |
|
40 | 40 |
|
41 | 41 | # Declare command-line flags. |
42 | 42 | argparser = argparse.ArgumentParser(add_help=False) |
43 | | -argparser.add_argument('ad_client_id', |
44 | | - help='The ID of the ad client for which to generate a report') |
| 43 | +argparser.add_argument( |
| 44 | + "ad_client_id", help="The ID of the ad client for which to generate a report" |
| 45 | +) |
45 | 46 |
|
46 | 47 |
|
47 | 48 | def main(argv): |
48 | | - # Authenticate and construct service. |
49 | | - service, flags = sample_tools.init( |
50 | | - argv, 'adexchangeseller', 'v1.1', __doc__, __file__, parents=[argparser], |
51 | | - scope='https://www.googleapis.com/auth/adexchange.seller.readonly') |
52 | | - |
53 | | - ad_client_id = flags.ad_client_id |
54 | | - |
55 | | - try: |
56 | | - # Retrieve report in pages and display data as we receive it. |
57 | | - start_index = 0 |
58 | | - rows_to_obtain = MAX_PAGE_SIZE |
59 | | - while True: |
60 | | - result = service.reports().generate( |
61 | | - startDate='2011-01-01', endDate='2011-08-31', |
62 | | - filter=['AD_CLIENT_ID==' + ad_client_id], |
63 | | - metric=['PAGE_VIEWS', 'AD_REQUESTS', 'AD_REQUESTS_COVERAGE', |
64 | | - 'CLICKS', 'AD_REQUESTS_CTR', 'COST_PER_CLICK', |
65 | | - 'AD_REQUESTS_RPM', 'EARNINGS'], |
66 | | - dimension=['DATE'], |
67 | | - sort=['+DATE'], |
68 | | - startIndex=start_index, |
69 | | - maxResults=rows_to_obtain).execute() |
70 | | - |
71 | | - # If this is the first page, display the headers. |
72 | | - if start_index == 0: |
73 | | - for header in result['headers']: |
74 | | - print('%25s' % header['name'], end=' ') |
75 | | - print() |
76 | | - |
77 | | - # Display results for this page. |
78 | | - for row in result['rows']: |
79 | | - for column in row: |
80 | | - print('%25s' % column, end=' ') |
81 | | - print() |
82 | | - |
83 | | - start_index += len(result['rows']) |
84 | | - |
85 | | - # Check to see if we're going to go above the limit and get as many |
86 | | - # results as we can. |
87 | | - if start_index + MAX_PAGE_SIZE > ROW_LIMIT: |
88 | | - rows_to_obtain = ROW_LIMIT - start_index |
89 | | - if rows_to_obtain <= 0: |
90 | | - break |
91 | | - |
92 | | - if (start_index >= int(result['totalMatchedRows'])): |
93 | | - break |
94 | | - |
95 | | - except client.AccessTokenRefreshError: |
96 | | - print ('The credentials have been revoked or expired, please re-run the ' |
97 | | - 'application to re-authorize') |
98 | | - |
99 | | -if __name__ == '__main__': |
100 | | - main(sys.argv) |
| 49 | + # Authenticate and construct service. |
| 50 | + service, flags = sample_tools.init( |
| 51 | + argv, |
| 52 | + "adexchangeseller", |
| 53 | + "v1.1", |
| 54 | + __doc__, |
| 55 | + __file__, |
| 56 | + parents=[argparser], |
| 57 | + scope="https://www.googleapis.com/auth/adexchange.seller.readonly", |
| 58 | + ) |
| 59 | + |
| 60 | + ad_client_id = flags.ad_client_id |
| 61 | + |
| 62 | + try: |
| 63 | + # Retrieve report in pages and display data as we receive it. |
| 64 | + start_index = 0 |
| 65 | + rows_to_obtain = MAX_PAGE_SIZE |
| 66 | + while True: |
| 67 | + result = ( |
| 68 | + service.reports() |
| 69 | + .generate( |
| 70 | + startDate="2011-01-01", |
| 71 | + endDate="2011-08-31", |
| 72 | + filter=["AD_CLIENT_ID==" + ad_client_id], |
| 73 | + metric=[ |
| 74 | + "PAGE_VIEWS", |
| 75 | + "AD_REQUESTS", |
| 76 | + "AD_REQUESTS_COVERAGE", |
| 77 | + "CLICKS", |
| 78 | + "AD_REQUESTS_CTR", |
| 79 | + "COST_PER_CLICK", |
| 80 | + "AD_REQUESTS_RPM", |
| 81 | + "EARNINGS", |
| 82 | + ], |
| 83 | + dimension=["DATE"], |
| 84 | + sort=["+DATE"], |
| 85 | + startIndex=start_index, |
| 86 | + maxResults=rows_to_obtain, |
| 87 | + ) |
| 88 | + .execute() |
| 89 | + ) |
| 90 | + |
| 91 | + # If this is the first page, display the headers. |
| 92 | + if start_index == 0: |
| 93 | + for header in result["headers"]: |
| 94 | + print("%25s" % header["name"], end=" ") |
| 95 | + print() |
| 96 | + |
| 97 | + # Display results for this page. |
| 98 | + for row in result["rows"]: |
| 99 | + for column in row: |
| 100 | + print("%25s" % column, end=" ") |
| 101 | + print() |
| 102 | + |
| 103 | + start_index += len(result["rows"]) |
| 104 | + |
| 105 | + # Check to see if we're going to go above the limit and get as many |
| 106 | + # results as we can. |
| 107 | + if start_index + MAX_PAGE_SIZE > ROW_LIMIT: |
| 108 | + rows_to_obtain = ROW_LIMIT - start_index |
| 109 | + if rows_to_obtain <= 0: |
| 110 | + break |
| 111 | + |
| 112 | + if start_index >= int(result["totalMatchedRows"]): |
| 113 | + break |
| 114 | + |
| 115 | + except client.AccessTokenRefreshError: |
| 116 | + print( |
| 117 | + "The credentials have been revoked or expired, please re-run the " |
| 118 | + "application to re-authorize" |
| 119 | + ) |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + main(sys.argv) |
0 commit comments