Skip to content

Commit 238b0d7

Browse files
committed
Update AdSense Management API samples to V1.2.
Reviewed in https://codereview.appspot.com/7305086/.
1 parent 1dc5dc8 commit 238b0d7

File tree

4 files changed

+140
-11
lines changed

4 files changed

+140
-11
lines changed

samples/adsense/generate_report.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,38 @@
3232
gflags.DEFINE_string('ad_client_id', None,
3333
'The ID of the ad client for which to generate a report',
3434
short_name='c')
35-
gflags.MarkFlagAsRequired('ad_client_id')
35+
gflags.DEFINE_string('report_id', None,
36+
'The ID of the saved report to generate',
37+
short_name='r')
3638

3739

3840
def main(argv):
3941
# Process flags and read their values.
4042
sample_utils.process_flags(argv)
4143
ad_client_id = gflags.FLAGS.ad_client_id
44+
saved_report_id = gflags.FLAGS.report_id
4245

4346
# Authenticate and construct service.
4447
service = sample_utils.initialize_service()
4548

4649
try:
4750
# Retrieve report.
48-
result = service.reports().generate(
49-
startDate='2011-01-01', endDate='2011-08-31',
50-
filter=['AD_CLIENT_ID==' + ad_client_id],
51-
metric=['PAGE_VIEWS', 'AD_REQUESTS', 'AD_REQUESTS_COVERAGE',
52-
'CLICKS', 'AD_REQUESTS_CTR', 'COST_PER_CLICK',
53-
'AD_REQUESTS_RPM', 'EARNINGS'],
54-
dimension=['DATE'],
55-
sort=['+DATE']).execute()
56-
51+
if saved_report_id:
52+
result = service.reports().saved().generate(
53+
savedReportId=saved_report_id).execute()
54+
elif ad_client_id:
55+
result = service.reports().generate(
56+
startDate='2011-01-01', endDate='2011-08-31',
57+
filter=['AD_CLIENT_ID==' + ad_client_id],
58+
metric=['PAGE_VIEWS', 'AD_REQUESTS', 'AD_REQUESTS_COVERAGE',
59+
'CLICKS', 'AD_REQUESTS_CTR', 'COST_PER_CLICK',
60+
'AD_REQUESTS_RPM', 'EARNINGS'],
61+
dimension=['DATE'],
62+
sort=['+DATE']).execute()
63+
else:
64+
print ('Specify ad client id or saved report id!\nUsage: %s ARGS\\n%s'
65+
% (sys.argv[0], gflags.FLAGS))
66+
sys.exit(1)
5767
# Display headers.
5868
for header in result['headers']:
5969
print '%25s' % header['name'],
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright 2011 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+
"""This example gets all the saved ad styles for the logged in user's default
18+
account.
19+
20+
Tags: savedadstyles.list
21+
"""
22+
23+
__author__ = 'jalc@google.com (Jose Alcerreca)'
24+
25+
import sys
26+
from oauth2client.client import AccessTokenRefreshError
27+
import sample_utils
28+
29+
MAX_PAGE_SIZE = 50
30+
31+
32+
def main(argv):
33+
sample_utils.process_flags(argv)
34+
35+
# Authenticate and construct service.
36+
service = sample_utils.initialize_service()
37+
38+
try:
39+
# Retrieve ad client list in pages and display data as we receive it.
40+
request = service.savedadstyles().list(maxResults=MAX_PAGE_SIZE)
41+
42+
while request is not None:
43+
result = request.execute()
44+
saved_ad_styles = result['items']
45+
for saved_ad_style in saved_ad_styles:
46+
print ('Saved ad style with ID "%s" and background color "#%s" was '
47+
'found.'
48+
% (saved_ad_style['id'],
49+
saved_ad_style['adStyle']['colors']['background']))
50+
if ('corners' in saved_ad_style['adStyle']
51+
and 'font' in saved_ad_style['adStyle']):
52+
print ('It has "%s" corners and a "%s" size font.' %
53+
(saved_ad_style['adStyle']['corners'],
54+
saved_ad_style['adStyle']['font']['size']))
55+
56+
request = service.savedadstyles().list_next(request, result)
57+
58+
except AccessTokenRefreshError:
59+
print ('The credentials have been revoked or expired, please re-run the '
60+
'application to re-authorize')
61+
62+
if __name__ == '__main__':
63+
main(sys.argv)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright 2011 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+
"""This example gets all the saved reports for the logged
18+
in user's default account.
19+
20+
Tags: savedreports.list
21+
"""
22+
23+
__author__ = 'jalc@google.com (Jose Alcerreca)'
24+
25+
import sys
26+
from oauth2client.client import AccessTokenRefreshError
27+
import sample_utils
28+
29+
MAX_PAGE_SIZE = 50
30+
31+
32+
def main(argv):
33+
sample_utils.process_flags(argv)
34+
35+
# Authenticate and construct service.
36+
service = sample_utils.initialize_service()
37+
38+
try:
39+
# Retrieve ad client list in pages and display data as we receive it.
40+
request = service.reports().saved().list(maxResults=MAX_PAGE_SIZE)
41+
42+
while request is not None:
43+
result = request.execute()
44+
saved_reports = result['items']
45+
for saved_report in saved_reports:
46+
print ('Saved ad style with ID "%s" and name "%s" was found.'
47+
% (saved_report['id'], saved_report['name']))
48+
49+
request = service.reports().saved().list_next(request, result)
50+
51+
except AccessTokenRefreshError:
52+
print ('The credentials have been revoked or expired, please re-run the '
53+
'application to re-authorize')
54+
55+
if __name__ == '__main__':
56+
main(sys.argv)

samples/adsense/sample_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def retrieve_service(http):
100100
"""Retrieves an AdSense Management API service via the discovery service."""
101101

102102
# Construct a service object via the discovery service.
103-
service = build("adsense", "v1.1", http=http)
103+
service = build("adsense", "v1.2", http=http)
104104
return service
105105

106106

0 commit comments

Comments
 (0)