• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2#
3# Copyright 2014 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 custom channels in an ad client.
18
19To get ad clients, run get_all_ad_clients.py.
20
21Tags: customchannels.list
22"""
23from __future__ import print_function
24
25__author__ = 'sgomes@google.com (Sérgio Gomes)'
26
27import argparse
28import sys
29
30from googleapiclient import sample_tools
31from oauth2client import client
32
33# Declare command-line flags.
34argparser = argparse.ArgumentParser(add_help=False)
35argparser.add_argument('ad_client_id',
36    help='The ad client ID for which to get custom channels')
37
38MAX_PAGE_SIZE = 50
39
40
41def main(argv):
42  # Authenticate and construct service.
43  service, flags = sample_tools.init(
44      argv, 'adexchangeseller', 'v1.1', __doc__, __file__, parents=[argparser],
45      scope='https://www.googleapis.com/auth/adexchange.seller.readonly')
46
47  ad_client_id = flags.ad_client_id
48
49  try:
50    # Retrieve custom channel list in pages and display data as we receive it.
51    request = service.customchannels().list(adClientId=ad_client_id,
52        maxResults=MAX_PAGE_SIZE)
53
54    while request is not None:
55      result = request.execute()
56      custom_channels = result['items']
57      for custom_channel in custom_channels:
58        print(('Custom channel with id "%s" and name "%s" was found. '
59               % (custom_channel['id'], custom_channel['name'])))
60
61        if 'targetingInfo' in custom_channel:
62          print('  Targeting info:')
63          targeting_info = custom_channel['targetingInfo']
64          if 'adsAppearOn' in targeting_info:
65            print('    Ads appear on: %s' % targeting_info['adsAppearOn'])
66          if 'location' in targeting_info:
67            print('    Location: %s' % targeting_info['location'])
68          if 'description' in targeting_info:
69            print('    Description: %s' % targeting_info['description'])
70          if 'siteLanguage' in targeting_info:
71            print('    Site language: %s' % targeting_info['siteLanguage'])
72
73      request = service.customchannels().list_next(request, result)
74
75  except client.AccessTokenRefreshError:
76    print ('The credentials have been revoked or expired, please re-run the '
77           'application to re-authorize')
78
79if __name__ == '__main__':
80  main(sys.argv)
81