Adds Python samples for the AdSense Management API v1.4
Reviewed in http://codereview.appspot.com/37900043
diff --git a/samples/adsense/generate_report.py b/samples/adsense/generate_report.py
index 4ccc128..e1ca837 100644
--- a/samples/adsense/generate_report.py
+++ b/samples/adsense/generate_report.py
@@ -42,7 +42,7 @@
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
- argv, 'adsense', 'v1.3', __doc__, __file__, parents=[argparser],
+ argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
scope='https://www.googleapis.com/auth/adsense.readonly')
# Process flags and read their values.
@@ -77,6 +77,10 @@
print '%25s' % column,
print
+ # Display date range.
+ print 'Report from %s to %s.' % (result['startDate'], result['endDate'])
+ print
+
except client.AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')
diff --git a/samples/adsense/generate_report_with_paging.py b/samples/adsense/generate_report_with_paging.py
index ca69763..620d9ed 100644
--- a/samples/adsense/generate_report_with_paging.py
+++ b/samples/adsense/generate_report_with_paging.py
@@ -39,14 +39,15 @@
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
-argparser.add_argument('ad_client_id',
+argparser.add_argument(
+ 'ad_client_id',
help='The ID of the ad client for which to generate a report')
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
- argv, 'adsense', 'v1.3', __doc__, __file__, parents=[argparser],
+ argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
scope='https://www.googleapis.com/auth/adsense.readonly')
ad_client_id = flags.ad_client_id
@@ -88,7 +89,7 @@
if rows_to_obtain <= 0:
break
- if (start_index >= int(result['totalMatchedRows'])):
+ if start_index >= int(result['totalMatchedRows']):
break
except client.AccessTokenRefreshError:
diff --git a/samples/adsense/get_account_tree.py b/samples/adsense/get_account_tree.py
index 83d99c8..ef38252 100644
--- a/samples/adsense/get_account_tree.py
+++ b/samples/adsense/get_account_tree.py
@@ -31,14 +31,15 @@
# Declare command-line flags, and set them as required.
argparser = argparse.ArgumentParser(add_help=False)
-argparser.add_argument('account_id',
- help='The ID of the account to use as the root of the tree')
+argparser.add_argument(
+ 'account_id',
+ help='The ID of the account to use as the root of the tree')
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
- argv, 'adsense', 'v1.3', __doc__, __file__, parents=[argparser],
+ argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
scope='https://www.googleapis.com/auth/adsense.readonly')
# Process flags and read their values.
@@ -60,7 +61,7 @@
def display_tree(account, level=0):
print (' ' * level * 2 +
'Account with ID "%s" and name "%s" was found. ' %
- (account['id'], account['name']))
+ (account['id'], account['name']))
if 'subAccounts' in account:
for sub_account in account['subAccounts']:
diff --git a/samples/adsense/get_all_accounts.py b/samples/adsense/get_all_accounts.py
index ab0328e..3683e20 100644
--- a/samples/adsense/get_all_accounts.py
+++ b/samples/adsense/get_all_accounts.py
@@ -31,8 +31,8 @@
def main(argv):
# Authenticate and construct service.
- service, flags = sample_tools.init(
- argv, 'adsense', 'v1.3', __doc__, __file__, parents=[],
+ service, unused_flags = sample_tools.init(
+ argv, 'adsense', 'v1.4', __doc__, __file__, parents=[],
scope='https://www.googleapis.com/auth/adsense.readonly')
try:
diff --git a/samples/adsense/get_all_ad_clients.py b/samples/adsense/get_all_ad_clients.py
index ae76aa1..5bc1ea6 100644
--- a/samples/adsense/get_all_ad_clients.py
+++ b/samples/adsense/get_all_ad_clients.py
@@ -31,8 +31,8 @@
def main(argv):
# Authenticate and construct service.
- service, flags = sample_tools.init(
- argv, 'adsense', 'v1.3', __doc__, __file__, parents=[],
+ service, unused_flags = sample_tools.init(
+ argv, 'adsense', 'v1.4', __doc__, __file__, parents=[],
scope='https://www.googleapis.com/auth/adsense.readonly')
try:
diff --git a/samples/adsense/get_all_ad_clients_for_account.py b/samples/adsense/get_all_ad_clients_for_account.py
index c9bcb5a..68a2cf1 100644
--- a/samples/adsense/get_all_ad_clients_for_account.py
+++ b/samples/adsense/get_all_ad_clients_for_account.py
@@ -32,13 +32,13 @@
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument('account_id',
- help='The ID of the account for which to get ad clients')
+ help='The ID of the account for which to get ad clients')
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
- argv, 'adsense', 'v1.3', __doc__, __file__, parents=[argparser],
+ argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
scope='https://www.googleapis.com/auth/adsense.readonly')
account_id = flags.account_id
diff --git a/samples/adsense/get_all_ad_units.py b/samples/adsense/get_all_ad_units.py
index 88f1486..b4a273b 100644
--- a/samples/adsense/get_all_ad_units.py
+++ b/samples/adsense/get_all_ad_units.py
@@ -31,7 +31,8 @@
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
-argparser.add_argument('ad_client_id',
+argparser.add_argument(
+ 'ad_client_id',
help='The ID of the ad client for which to generate a report')
MAX_PAGE_SIZE = 50
@@ -48,7 +49,7 @@
try:
# Retrieve ad unit list in pages and display data as we receive it.
request = service.adunits().list(adClientId=ad_client_id,
- maxResults=MAX_PAGE_SIZE)
+ maxResults=MAX_PAGE_SIZE)
while request is not None:
result = request.execute()
diff --git a/samples/adsense/get_all_ad_units_for_custom_channel.py b/samples/adsense/get_all_ad_units_for_custom_channel.py
index 7ee629f..7bad7b5 100644
--- a/samples/adsense/get_all_ad_units_for_custom_channel.py
+++ b/samples/adsense/get_all_ad_units_for_custom_channel.py
@@ -31,11 +31,14 @@
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
-argparser.add_argument('account_id',
+argparser.add_argument(
+ 'account_id',
help='The ID of the account with the specified custom channel')
-argparser.add_argument('ad_client_id',
+argparser.add_argument(
+ 'ad_client_id',
help='The ID of the ad client with the specified custom channel')
-argparser.add_argument('custom_channel_id',
+argparser.add_argument(
+ 'custom_channel_id',
help='The ID of the custom channel for which to get ad units')
MAX_PAGE_SIZE = 50
@@ -44,7 +47,7 @@
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
- argv, 'adsense', 'v1.3', __doc__, __file__, parents=[argparser],
+ argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
scope='https://www.googleapis.com/auth/adsense.readonly')
# Process flags and read their values.
diff --git a/samples/adsense/get_all_alerts.py b/samples/adsense/get_all_alerts.py
index 14a7164..100ca24 100644
--- a/samples/adsense/get_all_alerts.py
+++ b/samples/adsense/get_all_alerts.py
@@ -21,7 +21,6 @@
__author__ = 'jalc@google.com (Jose Alcerreca)'
-import argparse
import sys
from apiclient import sample_tools
@@ -30,9 +29,9 @@
def main(argv):
# Authenticate and construct service.
- service, flags = sample_tools.init(
- argv, 'adsense', 'v1.3', __doc__, __file__, parents=[],
- scope='https://www.googleapis.com/auth/adsense.readonly')
+ service, unused_flags = sample_tools.init(
+ argv, 'adsense', 'v1.4', __doc__, __file__, parents=[],
+ scope='https://www.googleapis.com/auth/adsense')
try:
# Retrieve alerts list in pages and display data as we receive it.
@@ -45,6 +44,8 @@
for alert in alerts:
print ('Alert id "%s" with severity "%s" and type "%s" was found. '
% (alert['id'], alert['severity'], alert['type']))
+ # Uncomment to dismiss alert. Note that this cannot be undone.
+ # service.alerts().delete(alertId=alert['id']).execute()
else:
print 'No alerts found!'
except client.AccessTokenRefreshError:
diff --git a/samples/adsense/get_all_custom_channels.py b/samples/adsense/get_all_custom_channels.py
index a9a9b9a..2c51e9a 100644
--- a/samples/adsense/get_all_custom_channels.py
+++ b/samples/adsense/get_all_custom_channels.py
@@ -32,7 +32,7 @@
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument('ad_client_id',
- help='The ad client ID for which to get custom channels')
+ help='The ad client ID for which to get custom channels')
MAX_PAGE_SIZE = 50
@@ -40,7 +40,7 @@
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
- argv, 'adsense', 'v1.3', __doc__, __file__, parents=[argparser],
+ argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
scope='https://www.googleapis.com/auth/adsense.readonly')
ad_client_id = flags.ad_client_id
@@ -48,7 +48,7 @@
try:
# Retrieve custom channel list in pages and display data as we receive it.
request = service.customchannels().list(adClientId=ad_client_id,
- maxResults=MAX_PAGE_SIZE)
+ maxResults=MAX_PAGE_SIZE)
while request is not None:
result = request.execute()
diff --git a/samples/adsense/get_all_custom_channels_for_ad_unit.py b/samples/adsense/get_all_custom_channels_for_ad_unit.py
index 18d1632..4c0a638 100644
--- a/samples/adsense/get_all_custom_channels_for_ad_unit.py
+++ b/samples/adsense/get_all_custom_channels_for_ad_unit.py
@@ -48,7 +48,7 @@
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
- argv, 'adsense', 'v1.3', __doc__, __file__, parents=[argparser],
+ argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
scope='https://www.googleapis.com/auth/adsense.readonly')
# Process flags and read their values.
diff --git a/samples/adsense/get_all_dimensions.py b/samples/adsense/get_all_dimensions.py
index 1d647a0..fe83e76 100644
--- a/samples/adsense/get_all_dimensions.py
+++ b/samples/adsense/get_all_dimensions.py
@@ -21,7 +21,6 @@
__author__ = 'jalc@google.com (Jose Alcerreca)'
-import argparse
import sys
from apiclient import sample_tools
@@ -30,8 +29,8 @@
def main(argv):
# Authenticate and construct service.
- service, flags = sample_tools.init(
- argv, 'adsense', 'v1.3', __doc__, __file__, parents=[],
+ service, unused_flags = sample_tools.init(
+ argv, 'adsense', 'v1.4', __doc__, __file__, parents=[],
scope='https://www.googleapis.com/auth/adsense.readonly')
try:
diff --git a/samples/adsense/get_all_metrics.py b/samples/adsense/get_all_metrics.py
index ce1b7d3..0ae2811 100644
--- a/samples/adsense/get_all_metrics.py
+++ b/samples/adsense/get_all_metrics.py
@@ -21,7 +21,6 @@
__author__ = 'jalc@google.com (Jose Alcerreca)'
-import argparse
import sys
from apiclient import sample_tools
@@ -30,8 +29,8 @@
def main(argv):
# Authenticate and construct service.
- service, flags = sample_tools.init(
- argv, 'adsense', 'v1.3', __doc__, __file__, parents=[],
+ service, unused_flags = sample_tools.init(
+ argv, 'adsense', 'v1.4', __doc__, __file__, parents=[],
scope='https://www.googleapis.com/auth/adsense.readonly')
try:
diff --git a/samples/adsense/get_all_payments.py b/samples/adsense/get_all_payments.py
new file mode 100644
index 0000000..1146f0e
--- /dev/null
+++ b/samples/adsense/get_all_payments.py
@@ -0,0 +1,55 @@
+#!/usr/bin/python
+#
+# Copyright 2013 Google Inc. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Gets all payments available for the logged in user's default account.
+
+Tags: payments.list
+"""
+
+__author__ = 'jalc@google.com (Jose Alcerreca)'
+
+import sys
+
+from apiclient import sample_tools
+from oauth2client import client
+
+def main(argv):
+ # Authenticate and construct service.
+ service, unused_flags = sample_tools.init(
+ argv, 'adsense', 'v1.4', __doc__, __file__, parents=[],
+ scope='https://www.googleapis.com/auth/adsense.readonly')
+ try:
+ # Retrieve payments list in pages and display data as we receive it.
+ request = service.payments().list()
+ if request is not None:
+ result = request.execute()
+ print result
+ if 'items' in result:
+ payments = result['items']
+ for payment in payments:
+ print ('Payment with id "%s" of %s %s and date %s was found. '
+ % (str(payment['id']),
+ payment['paymentAmountMicros'],
+ payment['paymentAmountCurrencyCode'],
+ payment.get('paymentDate', 'unknown')))
+ else:
+ print 'No payments found!'
+ except client.AccessTokenRefreshError:
+ print ('The credentials have been revoked or expired, please re-run the '
+ 'application to re-authorize')
+
+if __name__ == '__main__':
+ main(sys.argv)
diff --git a/samples/adsense/get_all_saved_ad_styles.py b/samples/adsense/get_all_saved_ad_styles.py
index 961c125..bc2e9ed 100644
--- a/samples/adsense/get_all_saved_ad_styles.py
+++ b/samples/adsense/get_all_saved_ad_styles.py
@@ -14,8 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-"""This example gets all the saved ad styles for the logged in user's default
-account.
+"""Gets all the saved ad styles for the user's default account.
Tags: savedadstyles.list
"""
@@ -32,8 +31,8 @@
def main(argv):
# Authenticate and construct service.
- service, flags = sample_tools.init(
- argv, 'adsense', 'v1.3', __doc__, __file__, parents=[],
+ service, unused_flags = sample_tools.init(
+ argv, 'adsense', 'v1.4', __doc__, __file__, parents=[],
scope='https://www.googleapis.com/auth/adsense.readonly')
try:
diff --git a/samples/adsense/get_all_saved_reports.py b/samples/adsense/get_all_saved_reports.py
index a8fcc6a..6a15f3e 100644
--- a/samples/adsense/get_all_saved_reports.py
+++ b/samples/adsense/get_all_saved_reports.py
@@ -14,8 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-"""This example gets all the saved reports for the logged
-in user's default account.
+"""Gets all the saved reports for user's default account.
Tags: savedreports.list
"""
@@ -32,8 +31,8 @@
def main(argv):
# Authenticate and construct service.
- service, flags = sample_tools.init(
- argv, 'adsense', 'v1.3', __doc__, __file__, parents=[],
+ service, unused_flags = sample_tools.init(
+ argv, 'adsense', 'v1.4', __doc__, __file__, parents=[],
scope='https://www.googleapis.com/auth/adsense.readonly')
try:
diff --git a/samples/adsense/get_all_url_channels.py b/samples/adsense/get_all_url_channels.py
index 5d2d3d1..9ffc814 100644
--- a/samples/adsense/get_all_url_channels.py
+++ b/samples/adsense/get_all_url_channels.py
@@ -34,13 +34,13 @@
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument('ad_client_id',
- help='The ad client ID for which to get URL channels')
+ help='The ad client ID for which to get URL channels')
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
- argv, 'adsense', 'v1.3', __doc__, __file__, parents=[argparser],
+ argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
scope='https://www.googleapis.com/auth/adsense.readonly')
ad_client_id = flags.ad_client_id
@@ -48,7 +48,7 @@
try:
# Retrieve URL channel list in pages and display data as we receive it.
request = service.urlchannels().list(adClientId=ad_client_id,
- maxResults=MAX_PAGE_SIZE)
+ maxResults=MAX_PAGE_SIZE)
while request is not None:
result = request.execute()