Skip to content

Commit b7efaf1

Browse files
authored
Merge branch 'main' into fix-flake8-F841
2 parents d472d95 + 2f24061 commit b7efaf1

40 files changed

+2367
-1925
lines changed

samples/adexchangeseller/generate_report.py

Lines changed: 68 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"""
2323
from __future__ import print_function
2424

25-
__author__ = '[email protected] (Sérgio Gomes)'
25+
__author__ = "[email protected] (Sérgio Gomes)"
2626

2727
import argparse
2828
import sys
@@ -33,54 +33,78 @@
3333
# Declare command-line flags.
3434
argparser = argparse.ArgumentParser(add_help=False)
3535
argparser.add_argument(
36-
'--ad_client_id',
37-
help='The ID of the ad client for which to generate a report')
38-
argparser.add_argument(
39-
'--report_id',
40-
help='The ID of the saved report to generate')
36+
"--ad_client_id", help="The ID of the ad client for which to generate a report"
37+
)
38+
argparser.add_argument("--report_id", help="The ID of the saved report to generate")
4139

4240

4341
def 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')
42+
# Authenticate and construct service.
43+
service, flags = sample_tools.init(
44+
argv,
45+
"adexchangeseller",
46+
"v1.1",
47+
__doc__,
48+
__file__,
49+
parents=[argparser],
50+
scope="https://www.googleapis.com/auth/adexchange.seller.readonly",
51+
)
52+
53+
# Process flags and read their values.
54+
ad_client_id = flags.ad_client_id
55+
saved_report_id = flags.report_id
4856

49-
# Process flags and read their values.
50-
ad_client_id = flags.ad_client_id
51-
saved_report_id = flags.report_id
57+
try:
58+
# Retrieve report.
59+
if saved_report_id:
60+
result = (
61+
service.reports()
62+
.saved()
63+
.generate(savedReportId=saved_report_id)
64+
.execute()
65+
)
66+
elif ad_client_id:
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+
)
86+
.execute()
87+
)
88+
else:
89+
argparser.print_help()
90+
sys.exit(1)
91+
# Display headers.
92+
for header in result["headers"]:
93+
print("%25s" % header["name"], end=" ")
94+
print()
5295

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()
96+
# Display results.
97+
for row in result["rows"]:
98+
for column in row:
99+
print("%25s" % column, end=" ")
100+
print()
74101

75-
# Display results.
76-
for row in result['rows']:
77-
for column in row:
78-
print('%25s' % column, end=' ')
79-
print()
102+
except client.AccessTokenRefreshError:
103+
print(
104+
"The credentials have been revoked or expired, please re-run the "
105+
"application to re-authorize"
106+
)
80107

81-
except client.AccessTokenRefreshError:
82-
print ('The credentials have been revoked or expired, please re-run the '
83-
'application to re-authorize')
84108

85-
if __name__ == '__main__':
86-
main(sys.argv)
109+
if __name__ == "__main__":
110+
main(sys.argv)

samples/adexchangeseller/generate_report_with_paging.py

Lines changed: 79 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"""
2727
from __future__ import print_function
2828

29-
__author__ = '[email protected] (Sérgio Gomes)'
29+
__author__ = "[email protected] (Sérgio Gomes)"
3030

3131
import argparse
3232
import sys
@@ -40,61 +40,84 @@
4040

4141
# Declare command-line flags.
4242
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+
)
4546

4647

4748
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)

samples/adexchangeseller/get_all_ad_clients.py

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"""
2121
from __future__ import print_function
2222

23-
__author__ = '[email protected] (Sérgio Gomes)'
23+
__author__ = "[email protected] (Sérgio Gomes)"
2424

2525
import sys
2626

@@ -31,30 +31,47 @@
3131

3232

3333
def main(argv):
34-
# Authenticate and construct service.
35-
service, flags = sample_tools.init(
36-
argv, 'adexchangeseller', 'v1.1', __doc__, __file__, parents=[],
37-
scope='https://www.googleapis.com/auth/adexchange.seller.readonly')
34+
# Authenticate and construct service.
35+
service, flags = sample_tools.init(
36+
argv,
37+
"adexchangeseller",
38+
"v1.1",
39+
__doc__,
40+
__file__,
41+
parents=[],
42+
scope="https://www.googleapis.com/auth/adexchange.seller.readonly",
43+
)
3844

39-
try:
40-
# Retrieve ad client list in pages and display data as we receive it.
41-
request = service.adclients().list(maxResults=MAX_PAGE_SIZE)
45+
try:
46+
# Retrieve ad client list in pages and display data as we receive it.
47+
request = service.adclients().list(maxResults=MAX_PAGE_SIZE)
4248

43-
while request is not None:
44-
result = request.execute()
45-
ad_clients = result['items']
46-
for ad_client in ad_clients:
47-
print(('Ad client for product "%s" with ID "%s" was found. '
48-
% (ad_client['productCode'], ad_client['id'])))
49+
while request is not None:
50+
result = request.execute()
51+
ad_clients = result["items"]
52+
for ad_client in ad_clients:
53+
print(
54+
(
55+
'Ad client for product "%s" with ID "%s" was found. '
56+
% (ad_client["productCode"], ad_client["id"])
57+
)
58+
)
4959

50-
print(('\tSupports reporting: %s' %
51-
(ad_client['supportsReporting'] and 'Yes' or 'No')))
60+
print(
61+
(
62+
"\tSupports reporting: %s"
63+
% (ad_client["supportsReporting"] and "Yes" or "No")
64+
)
65+
)
5266

53-
request = service.adclients().list_next(request, result)
67+
request = service.adclients().list_next(request, result)
5468

55-
except client.AccessTokenRefreshError:
56-
print ('The credentials have been revoked or expired, please re-run the '
57-
'application to re-authorize')
69+
except client.AccessTokenRefreshError:
70+
print(
71+
"The credentials have been revoked or expired, please re-run the "
72+
"application to re-authorize"
73+
)
5874

59-
if __name__ == '__main__':
60-
main(sys.argv)
75+
76+
if __name__ == "__main__":
77+
main(sys.argv)

0 commit comments

Comments
 (0)