Skip to content

Commit a257aaa

Browse files
committed
Adds Python samples for the AdSense Management API v1.4
Reviewed in http://codereview.appspot.com/37900043
1 parent aa2f6c9 commit a257aaa

17 files changed

+107
-45
lines changed

samples/adsense/generate_report.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
def main(argv):
4343
# Authenticate and construct service.
4444
service, flags = sample_tools.init(
45-
argv, 'adsense', 'v1.3', __doc__, __file__, parents=[argparser],
45+
argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
4646
scope='https://www.googleapis.com/auth/adsense.readonly')
4747

4848
# Process flags and read their values.
@@ -77,6 +77,10 @@ def main(argv):
7777
print '%25s' % column,
7878
print
7979

80+
# Display date range.
81+
print 'Report from %s to %s.' % (result['startDate'], result['endDate'])
82+
print
83+
8084
except client.AccessTokenRefreshError:
8185
print ('The credentials have been revoked or expired, please re-run the '
8286
'application to re-authorize')

samples/adsense/generate_report_with_paging.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@
3939

4040
# Declare command-line flags.
4141
argparser = argparse.ArgumentParser(add_help=False)
42-
argparser.add_argument('ad_client_id',
42+
argparser.add_argument(
43+
'ad_client_id',
4344
help='The ID of the ad client for which to generate a report')
4445

4546

4647
def main(argv):
4748
# Authenticate and construct service.
4849
service, flags = sample_tools.init(
49-
argv, 'adsense', 'v1.3', __doc__, __file__, parents=[argparser],
50+
argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
5051
scope='https://www.googleapis.com/auth/adsense.readonly')
5152

5253
ad_client_id = flags.ad_client_id
@@ -88,7 +89,7 @@ def main(argv):
8889
if rows_to_obtain <= 0:
8990
break
9091

91-
if (start_index >= int(result['totalMatchedRows'])):
92+
if start_index >= int(result['totalMatchedRows']):
9293
break
9394

9495
except client.AccessTokenRefreshError:

samples/adsense/get_account_tree.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@
3131

3232
# Declare command-line flags, and set them as required.
3333
argparser = argparse.ArgumentParser(add_help=False)
34-
argparser.add_argument('account_id',
35-
help='The ID of the account to use as the root of the tree')
34+
argparser.add_argument(
35+
'account_id',
36+
help='The ID of the account to use as the root of the tree')
3637

3738

3839
def main(argv):
3940
# Authenticate and construct service.
4041
service, flags = sample_tools.init(
41-
argv, 'adsense', 'v1.3', __doc__, __file__, parents=[argparser],
42+
argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
4243
scope='https://www.googleapis.com/auth/adsense.readonly')
4344

4445
# Process flags and read their values.
@@ -60,7 +61,7 @@ def main(argv):
6061
def display_tree(account, level=0):
6162
print (' ' * level * 2 +
6263
'Account with ID "%s" and name "%s" was found. ' %
63-
(account['id'], account['name']))
64+
(account['id'], account['name']))
6465

6566
if 'subAccounts' in account:
6667
for sub_account in account['subAccounts']:

samples/adsense/get_all_accounts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131

3232
def main(argv):
3333
# Authenticate and construct service.
34-
service, flags = sample_tools.init(
35-
argv, 'adsense', 'v1.3', __doc__, __file__, parents=[],
34+
service, unused_flags = sample_tools.init(
35+
argv, 'adsense', 'v1.4', __doc__, __file__, parents=[],
3636
scope='https://www.googleapis.com/auth/adsense.readonly')
3737

3838
try:

samples/adsense/get_all_ad_clients.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131

3232
def main(argv):
3333
# Authenticate and construct service.
34-
service, flags = sample_tools.init(
35-
argv, 'adsense', 'v1.3', __doc__, __file__, parents=[],
34+
service, unused_flags = sample_tools.init(
35+
argv, 'adsense', 'v1.4', __doc__, __file__, parents=[],
3636
scope='https://www.googleapis.com/auth/adsense.readonly')
3737

3838
try:

samples/adsense/get_all_ad_clients_for_account.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232
# Declare command-line flags.
3333
argparser = argparse.ArgumentParser(add_help=False)
3434
argparser.add_argument('account_id',
35-
help='The ID of the account for which to get ad clients')
35+
help='The ID of the account for which to get ad clients')
3636

3737

3838
def main(argv):
3939
# Authenticate and construct service.
4040
service, flags = sample_tools.init(
41-
argv, 'adsense', 'v1.3', __doc__, __file__, parents=[argparser],
41+
argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
4242
scope='https://www.googleapis.com/auth/adsense.readonly')
4343

4444
account_id = flags.account_id

samples/adsense/get_all_ad_units.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131

3232
# Declare command-line flags.
3333
argparser = argparse.ArgumentParser(add_help=False)
34-
argparser.add_argument('ad_client_id',
34+
argparser.add_argument(
35+
'ad_client_id',
3536
help='The ID of the ad client for which to generate a report')
3637

3738
MAX_PAGE_SIZE = 50
@@ -48,7 +49,7 @@ def main(argv):
4849
try:
4950
# Retrieve ad unit list in pages and display data as we receive it.
5051
request = service.adunits().list(adClientId=ad_client_id,
51-
maxResults=MAX_PAGE_SIZE)
52+
maxResults=MAX_PAGE_SIZE)
5253

5354
while request is not None:
5455
result = request.execute()

samples/adsense/get_all_ad_units_for_custom_channel.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@
3131

3232
# Declare command-line flags.
3333
argparser = argparse.ArgumentParser(add_help=False)
34-
argparser.add_argument('account_id',
34+
argparser.add_argument(
35+
'account_id',
3536
help='The ID of the account with the specified custom channel')
36-
argparser.add_argument('ad_client_id',
37+
argparser.add_argument(
38+
'ad_client_id',
3739
help='The ID of the ad client with the specified custom channel')
38-
argparser.add_argument('custom_channel_id',
40+
argparser.add_argument(
41+
'custom_channel_id',
3942
help='The ID of the custom channel for which to get ad units')
4043

4144
MAX_PAGE_SIZE = 50
@@ -44,7 +47,7 @@
4447
def main(argv):
4548
# Authenticate and construct service.
4649
service, flags = sample_tools.init(
47-
argv, 'adsense', 'v1.3', __doc__, __file__, parents=[argparser],
50+
argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
4851
scope='https://www.googleapis.com/auth/adsense.readonly')
4952

5053
# Process flags and read their values.

samples/adsense/get_all_alerts.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
__author__ = 'jalc@google.com (Jose Alcerreca)'
2323

24-
import argparse
2524
import sys
2625

2726
from apiclient import sample_tools
@@ -30,9 +29,9 @@
3029

3130
def main(argv):
3231
# Authenticate and construct service.
33-
service, flags = sample_tools.init(
34-
argv, 'adsense', 'v1.3', __doc__, __file__, parents=[],
35-
scope='https://www.googleapis.com/auth/adsense.readonly')
32+
service, unused_flags = sample_tools.init(
33+
argv, 'adsense', 'v1.4', __doc__, __file__, parents=[],
34+
scope='https://www.googleapis.com/auth/adsense')
3635

3736
try:
3837
# Retrieve alerts list in pages and display data as we receive it.
@@ -45,6 +44,8 @@ def main(argv):
4544
for alert in alerts:
4645
print ('Alert id "%s" with severity "%s" and type "%s" was found. '
4746
% (alert['id'], alert['severity'], alert['type']))
47+
# Uncomment to dismiss alert. Note that this cannot be undone.
48+
# service.alerts().delete(alertId=alert['id']).execute()
4849
else:
4950
print 'No alerts found!'
5051
except client.AccessTokenRefreshError:

samples/adsense/get_all_custom_channels.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,23 @@
3232
# Declare command-line flags.
3333
argparser = argparse.ArgumentParser(add_help=False)
3434
argparser.add_argument('ad_client_id',
35-
help='The ad client ID for which to get custom channels')
35+
help='The ad client ID for which to get custom channels')
3636

3737
MAX_PAGE_SIZE = 50
3838

3939

4040
def main(argv):
4141
# Authenticate and construct service.
4242
service, flags = sample_tools.init(
43-
argv, 'adsense', 'v1.3', __doc__, __file__, parents=[argparser],
43+
argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
4444
scope='https://www.googleapis.com/auth/adsense.readonly')
4545

4646
ad_client_id = flags.ad_client_id
4747

4848
try:
4949
# Retrieve custom channel list in pages and display data as we receive it.
5050
request = service.customchannels().list(adClientId=ad_client_id,
51-
maxResults=MAX_PAGE_SIZE)
51+
maxResults=MAX_PAGE_SIZE)
5252

5353
while request is not None:
5454
result = request.execute()

0 commit comments

Comments
 (0)