Adds Python samples for the AdSense Management API v1.4

Reviewed in http://codereview.appspot.com/37900043
This commit is contained in:
Jose Alcerreca
2013-12-19 15:34:35 +00:00
parent aa2f6c9705
commit a257aaacd5
17 changed files with 107 additions and 45 deletions

View File

@@ -42,7 +42,7 @@ argparser.add_argument(
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 @@ def main(argv):
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')

View File

@@ -39,14 +39,15 @@ ROW_LIMIT = 5000
# 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 @@ def main(argv):
if rows_to_obtain <= 0:
break
if (start_index >= int(result['totalMatchedRows'])):
if start_index >= int(result['totalMatchedRows']):
break
except client.AccessTokenRefreshError:

View File

@@ -31,14 +31,15 @@ from oauth2client import client
# 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 main(argv):
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']:

View File

@@ -31,8 +31,8 @@ MAX_PAGE_SIZE = 50
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:

View File

@@ -31,8 +31,8 @@ MAX_PAGE_SIZE = 50
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:

View File

@@ -32,13 +32,13 @@ MAX_PAGE_SIZE = 50
# 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

View File

@@ -31,7 +31,8 @@ from oauth2client import client
# 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 @@ def main(argv):
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()

View File

@@ -31,11 +31,14 @@ from oauth2client import client
# 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 @@ MAX_PAGE_SIZE = 50
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.

View File

@@ -21,7 +21,6 @@ Tags: metadata.alerts.list
__author__ = 'jalc@google.com (Jose Alcerreca)'
import argparse
import sys
from apiclient import sample_tools
@@ -30,9 +29,9 @@ from oauth2client import client
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 @@ def main(argv):
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:

View File

@@ -32,7 +32,7 @@ from oauth2client import client
# 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 @@ MAX_PAGE_SIZE = 50
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 @@ def main(argv):
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()

View File

@@ -48,7 +48,7 @@ MAX_PAGE_SIZE = 50
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.

View File

@@ -21,7 +21,6 @@ Tags: metadata.dimensions.list
__author__ = 'jalc@google.com (Jose Alcerreca)'
import argparse
import sys
from apiclient import sample_tools
@@ -30,8 +29,8 @@ from oauth2client import client
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:

View File

@@ -21,7 +21,6 @@ Tags: metadata.metrics.list
__author__ = 'jalc@google.com (Jose Alcerreca)'
import argparse
import sys
from apiclient import sample_tools
@@ -30,8 +29,8 @@ from oauth2client import client
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:

View File

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

View File

@@ -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 @@ MAX_PAGE_SIZE = 50
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:

View File

@@ -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 @@ MAX_PAGE_SIZE = 50
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:

View File

@@ -34,13 +34,13 @@ MAX_PAGE_SIZE = 50
# 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 @@ def main(argv):
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()