def main(argv):
  # Authenticate and construct service.
  service, flags = sample_tools.init(
      argv, 'adsense', 'v1.3', __doc__, __file__, parents=[argparser],
      scope='https://www.googleapis.com/auth/adsense.readonly')

  ad_client_id = flags.ad_client_id

  try:
    # Let the user pick account if more than one.
    account_id = get_account_id(service)
    # Retrieve ad unit list in pages and display data as we receive it.
    request = service.accounts().adunits().list(accountId=account_id,
                                                adClientId=ad_client_id,
                                                maxResults=MAX_PAGE_SIZE)

    while request is not None:
      result = request.execute()
      if 'items' in result:
        ad_units = result['items']
        for ad_unit in ad_units:
          print (
              'Ad unit with code "%s", name "%s" and status "%s" was found. ' %
              (ad_unit['code'], ad_unit['name'], ad_unit['status']))

      request = service.adunits().list_next(request, result)

  except client.AccessTokenRefreshError:
    print ('The credentials have been revoked or expired, please re-run the '
           'application to re-authorize')
Пример #2
0
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:
    account_id = get_account_id(service)

    # Retrieve ad client list in pages and display data as we receive it.
    request = service.accounts().adclients().list(accountId=account_id,
                                                  maxResults=MAX_PAGE_SIZE)

    while request is not None:
      result = request.execute()
      ad_clients = result['items']
      for ad_client in ad_clients:
        print ('Ad client for product "%s" with ID "%s" was found. '
               % (ad_client['productCode'], ad_client['id']))

        print ('\tSupports reporting: %s' %
               (ad_client['supportsReporting'] and 'Yes' or 'No'))

      request = service.accounts().adclients().list_next(request, result)

  except client.AccessTokenRefreshError:
    print ('The credentials have been revoked or expired, please re-run the '
           'application to re-authorize')
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:
    # Let the user pick account if more than one.
    account_id = get_account_id(service)

    # Retrieve ad client list in pages and display data as we receive it.
    request = service.accounts().savedadstyles().list(accountId=account_id,
                                                      maxResults=MAX_PAGE_SIZE)

    while request is not None:
      result = request.execute()
      if 'items' in result:
        saved_ad_styles = result['items']
        for saved_ad_style in saved_ad_styles:
          print ('Saved ad style with ID "%s" and background color "#%s" was '
                 'found.'
                 % (saved_ad_style['id'],
                    saved_ad_style['adStyle']['colors']['background']))
          if ('corners' in saved_ad_style['adStyle']
                  and 'font' in saved_ad_style['adStyle']):
            print ('It has "%s" corners and a "%s" size font.' %
                   (saved_ad_style['adStyle']['corners'],
                    saved_ad_style['adStyle']['font']['size']))

      request = service.savedadstyles().list_next(request, result)

  except client.AccessTokenRefreshError:
    print ('The credentials have been revoked or expired, please re-run the '
           'application to re-authorize')
Пример #4
0
def main(argv):
  # Authenticate and construct service.
  credentials = adsense_util.get_adsense_credentials()
  with discovery.build('adsense', 'v2', credentials = credentials) as service:
    try:
      # Select and retrieve account.
      account_id = adsense_util.get_account_id(service)
      # Retrieve saved report list in pages and display data as we receive it.
      request = service.accounts().reports().saved().list(
          parent=account_id, pageSize=MAX_PAGE_SIZE)

      while request is not None:
        result = request.execute()
        if 'savedReports' in result:
          for saved_report in result['savedReports']:
            print('Saved report with ID "%s" ' % saved_report['name'], end='')
            if 'title' in saved_report:
              print('and title "%s" ' % saved_report['title'], end='')
            print('was found.')
        else:
          print('No saved reports were found.')

        request = service.accounts().reports().saved().list_next(
            request, result)

    except google.auth.exceptions.RefreshError:
      print('The credentials have been revoked or expired, please delete the '
            '"%s" file and re-run the application to re-authorize.' %
            adsense_util.CREDENTIALS_FILE)
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')

  try:
    # Let the user pick account if more than one.
    account_id = get_account_id(service)

    # Retrieve alerts list in pages and display data as we receive it.
    request = service.accounts().alerts().list(accountId=account_id)

    if request is not None:
      result = request.execute()
      if 'items' in result:
        alerts = result['items']
        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:
    print ('The credentials have been revoked or expired, please re-run the '
           'application to re-authorize')
Пример #6
0
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')

    try:
        # Let the user pick account if more than one.
        account_id = get_account_id(service)

        # Retrieve alerts list in pages and display data as we receive it.
        request = service.accounts().alerts().list(accountId=account_id)

        if request is not None:
            result = request.execute()
            if 'items' in result:
                alerts = result['items']
                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:
        print(
            'The credentials have been revoked or expired, please re-run the '
            'application to re-authorize')
Пример #7
0
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:
        # Let the user pick account if more than one.
        account_id = get_account_id(service)

        # Retrieve ad client list in pages and display data as we receive it.
        request = service.accounts().reports().saved().list(
            accountId=account_id, maxResults=MAX_PAGE_SIZE)

        while request is not None:
            result = request.execute()
            if 'items' in result:
                saved_reports = result['items']
                for saved_report in saved_reports:
                    print(
                        'Saved ad style with ID "%s" and name "%s" was found.'
                        % (saved_report['id'], saved_report['name']))

            request = service.reports().saved().list_next(request, result)

    except client.AccessTokenRefreshError:
        print(
            'The credentials have been revoked or expired, please re-run the '
            'application to re-authorize')
def main(argv):
    # Authenticate and construct service.
    credentials = adsense_util.get_adsense_credentials()
    with discovery.build('adsense', 'v2', credentials=credentials) as service:
        try:
            # Select and retrieve account.
            account_id = adsense_util.get_account_id(service)
            # Retrieve ad client list in pages and display data as we receive it.
            request = service.accounts().adclients().list(
                parent=account_id, pageSize=MAX_PAGE_SIZE)

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

                if 'adClients' in result:
                    ad_clients = result['adClients']
                    for ad_client in ad_clients:
                        print(
                            'Ad client for product "%s" with ID "%s" was found.'
                            % (ad_client.get('productCode',
                                             'N/A'), ad_client['name']))
                        print('\tSupports reporting: %s' %
                              ('Yes' if ad_client.get('reportingDimensionId')
                               else 'No'))
                else:
                    print('No ad clients were found.')

                request = service.accounts().adclients().list_next(
                    request, result)

        except google.auth.exceptions.RefreshError:
            print(
                'The credentials have been revoked or expired, please delete the '
                '"%s" file and re-run the application to re-authorize.' %
                adsense_util.CREDENTIALS_FILE)
def main(argv):
  # Authenticate and construct service.
  service, flags = sample_tools.init(
      argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
      scope='https://www.googleapis.com/auth/adsense.readonly')

  ad_client_id = flags.ad_client_id

  try:
    # Let the user pick account if more than one.
    account_id = get_account_id(service)

    # Retrieve URL channel list in pages and display data as we receive it.
    request = service.accounts().urlchannels().list(accountId=account_id,
                                                    adClientId=ad_client_id,
                                                    maxResults=MAX_PAGE_SIZE)

    while request is not None:
      result = request.execute()
      if 'items' in result:
        url_channels = result['items']
        for url_channel in url_channels:
          print ('URL channel with URL pattern "%s" was found.'
                 % url_channel['urlPattern'])

      request = service.customchannels().list_next(request, result)

  except client.AccessTokenRefreshError:
    print ('The credentials have been revoked or expired, please re-run the '
           'application to re-authorize')
def main(argv):
    # Authenticate and construct service.
    credentials = adsense_util.get_adsense_credentials()
    with discovery.build('adsense', 'v2', credentials=credentials) as service:
        try:
            # Select and retrieve account.
            account_id = adsense_util.get_account_id(service)
            # Retrieve ad client list in pages and display data as we receive it.
            request = service.accounts().payments().list(parent=account_id)

            if request is not None:
                result = request.execute()
                if 'payments' in result:
                    for payment in result['payments']:
                        if 'date' in payment:
                            payment_date = datetime.date(
                                payment['date']['year'],
                                payment['date']['month'],
                                payment['date']['day']).strftime('%Y-%m-%d')
                        else:
                            payment_date = 'unknown'
                        print(
                            'Payment with ID "%s" of %s and date %s was found'
                            %
                            (payment['name'], payment['amount'], payment_date))
                else:
                    print('No payments found.')

        except google.auth.exceptions.RefreshError:
            print(
                'The credentials have been revoked or expired, please delete the '
                '"%s" file and re-run the application to re-authorize.' %
                adsense_util.CREDENTIALS_FILE)
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:
    # Let the user pick account if more than one.
    account_id = get_account_id(service)
    # Retrieve payments list in pages and display data as we receive it.
    request = service.accounts().payments().list(accountId=account_id)
    if request is not None:
      result = request.execute()
      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['paymentAmount'],
                    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')
Пример #12
0
def main(argv):
    # Authenticate and construct service.
    credentials = adsense_util.get_adsense_credentials()
    with discovery.build('adsense', 'v2', credentials=credentials) as service:
        try:
            # Select and retrieve account.
            account_id = adsense_util.get_account_id(service)
            # Retrieve alert list in pages and display data as we receive it.
            request = service.accounts().alerts().list(parent=account_id)

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

                if 'alerts' in result:
                    for alert in result['alerts']:
                        print(
                            'Alert ID "%s" with severity "%s" and type "%s" was found. '
                            %
                            (alert['name'], alert['severity'], alert['type']))
                else:
                    print('No alerts found!')

                request = service.accounts().list_next(request, result)

        except google.auth.exceptions.RefreshError:
            print(
                'The credentials have been revoked or expired, please delete the '
                '"%s" file and re-run the application to re-authorize.' %
                adsense_util.CREDENTIALS_FILE)
Пример #13
0
def main(argv):
    # Authenticate and construct service.
    service, flags = sample_tools.init(
        argv,
        'adsense',
        'v1.4',
        __doc__,
        __file__,
        parents=[argparser],
        scope='https://www.googleapis.com/auth/adsense.readonly')

    ad_client_id = flags.ad_client_id

    try:
        # Let the user pick account if more than one.
        account_id = get_account_id(service)

        # Retrieve report in pages and display data as we receive it.
        start_index = 0
        rows_to_obtain = MAX_PAGE_SIZE
        result_all_pages = None
        while True:
            result = service.reports().generate(
                accountId=account_id,
                startDate='today-2y',
                endDate='today',
                filter=['AD_CLIENT_ID==' + ad_client_id],
                metric=[
                    'PAGE_VIEWS', 'AD_REQUESTS', 'AD_REQUESTS_COVERAGE',
                    'CLICKS', 'AD_REQUESTS_CTR', 'COST_PER_CLICK',
                    'AD_REQUESTS_RPM', 'EARNINGS'
                ],
                dimension=['DATE'],
                sort=['+DATE'],
                startIndex=start_index,
                maxResults=rows_to_obtain).execute()

            if result_all_pages is None:
                result_all_pages = result
            else:
                result_all_pages['rows'].extend(result['rows'])

            start_index += len(result['rows'])

            # Check to see if we're going to go above the limit and get as many
            # results as we can.
            if start_index + MAX_PAGE_SIZE > ROW_LIMIT:
                rows_to_obtain = ROW_LIMIT - start_index
                if rows_to_obtain <= 0:
                    break

            if start_index >= int(result['totalMatchedRows']):
                break

        print_result(fill_date_gaps(result_all_pages))

    except client.AccessTokenRefreshError:
        print(
            'The credentials have been revoked or expired, please re-run the '
            'application to re-authorize')
Пример #14
0
def main(argv):
    # Authenticate and construct service.
    service, flags = sample_tools.init(
        argv,
        'adsense',
        'v1.4',
        __doc__,
        __file__,
        parents=[argparser],
        scope='https://www.googleapis.com/auth/adsense.readonly')

    # Process flags and read their values.
    saved_report_id = flags.report_id

    try:
        # Let the user pick account if more than one.
        account_id = get_account_id(service)

        # Retrieve report.
        if saved_report_id:
            result = service.accounts().reports().saved().generate(
                accountId=account_id, savedReportId=saved_report_id).execute()
        else:
            result = service.accounts().reports().generate(
                accountId=account_id,
                startDate='today-2m',
                endDate='today',
                metric=[
                    'PAGE_VIEWS', 'AD_REQUESTS', 'AD_REQUESTS_COVERAGE',
                    'CLICKS', 'AD_REQUESTS_CTR', 'COST_PER_CLICK',
                    'AD_REQUESTS_RPM', 'EARNINGS'
                ],
                dimension=['MONTH', 'PLATFORM_TYPE_NAME'],
                sort=['+MONTH']).execute()

        result = DataCollator([result]).collate_data()

        # Display headers.
        for header in result['headers']:
            print '%25s' % header['name'],
        print

        # Display results.
        for row in result['rows']:
            for column in row:
                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')
def main(argv):
    # Authenticate and construct service.
    service, flags = sample_tools.init(
        argv,
        'adsense',
        'v1.4',
        __doc__,
        __file__,
        parents=[],
        scope='https://www.googleapis.com/auth/adsense.readonly')

    try:
        account_id = get_account_id(service)

        start_date = get_month_start()
        end_date = get_month_end()

        start_index = 0
        rows_to_obtain = MAX_PAGE_SIZE
        result_all_pages = None
        while True:
            result = service.reports().generate(
                accountId=account_id,
                startDate=start_date,
                endDate=end_date,
                filter=['AD_CLIENT_ID==ca-' + account_id],
                metric=METRICS,
                dimension=DIMENSION,
                useTimezoneReporting=True,
                startIndex=start_index,
                maxResults=rows_to_obtain).execute()

            if result_all_pages is None:
                result_all_pages = result
            else:
                result_all_pages['rows'].extend(result['rows'])

            if 'rows' in result:
                start_index += len(result['rows'])

            # Check to see if we're going to go above the limit and get as many
            # results as we can.
            if start_index + MAX_PAGE_SIZE > ROW_LIMIT:
                rows_to_obtain = ROW_LIMIT - start_index
                if rows_to_obtain <= 0:
                    break

            if start_index >= int(result['totalMatchedRows']):
                break

        store_report_in_db(result_all_pages, start_date)

    except client.AccessTokenRefreshError:
        print(
            'The credentials have been revoked or expired, please re-run the '
            'application to re-authorize')
Пример #16
0
def main(argv):
    # Authenticate and construct service.
    credentials = adsense_util.get_adsense_credentials()
    with discovery.build('adsense', 'v2', credentials=credentials) as service:
        try:
            # Let the user pick account if more than one.
            account_id = adsense_util.get_account_id(service)

            # Retrieve report.
            if saved_report_id:
                result = service.accounts().reports().saved().generate(
                    name=saved_report_id, dateRange='LAST_7_DAYS').execute()
            else:
                result = service.accounts().reports().generate(
                    account=account_id,
                    dateRange='CUSTOM',
                    startDate_year=2021,
                    startDate_month=3,
                    startDate_day=1,
                    endDate_year=2021,
                    endDate_month=3,
                    endDate_day=31,
                    metrics=[
                        'PAGE_VIEWS', 'AD_REQUESTS', 'AD_REQUESTS_COVERAGE',
                        'CLICKS', 'AD_REQUESTS_CTR', 'COST_PER_CLICK',
                        'AD_REQUESTS_RPM', 'ESTIMATED_EARNINGS'
                    ],
                    dimensions=['MONTH', 'PLATFORM_TYPE_NAME'],
                    orderBy=['+MONTH']).execute()

            print(result)

            # Display headers.
            for header in result['headers']:
                print('%25s' % header['name'], end=''),
            print()

            # Display results.
            if 'rows' in result:
                for row in result['rows']:
                    for cell in row['cells']:
                        print('%25s' % cell['value'], end='')
            print()

            # Display date range.
            print('Report from %s to %s.' %
                  (result['startDate'], result['endDate']))
            print()

        except google.auth.exceptions.RefreshError:
            print(
                'The credentials have been revoked or expired, please delete the '
                '"%s" file and re-run the application to re-authorize.' %
                adsense_util.CREDENTIALS_FILE)
def main(argv):
    # Authenticate and construct service.
    service, flags = sample_tools.init(
        argv,
        'adsense',
        'v1.4',
        __doc__,
        __file__,
        parents=[argparser],
        scope='https://www.googleapis.com/auth/adsense.readonly')

    ad_client_id = flags.ad_client_id

    try:
        # Let the user pick account if more than one.
        account_id = get_account_id(service)

        # Retrieve custom channel list in pages and display data as we receive it.
        request = service.accounts().customchannels().list(
            accountId=account_id,
            adClientId=ad_client_id,
            maxResults=MAX_PAGE_SIZE)

        while request is not None:
            result = request.execute()
            if 'items' in result:
                custom_channels = result['items']
                for custom_channel in custom_channels:
                    print(
                        'Custom channel with id "%s" and name "%s" was found. '
                        % (custom_channel['id'], custom_channel['name']))

                    if 'targetingInfo' in custom_channel:
                        print '  Targeting info:'
                        targeting_info = custom_channel['targetingInfo']
                        if 'adsAppearOn' in targeting_info:
                            print '    Ads appear on: %s' % targeting_info[
                                'adsAppearOn']
                        if 'location' in targeting_info:
                            print '    Location: %s' % targeting_info[
                                'location']
                        if 'description' in targeting_info:
                            print '    Description: %s' % targeting_info[
                                'description']
                        if 'siteLanguage' in targeting_info:
                            print '    Site language: %s' % targeting_info[
                                'siteLanguage']

            request = service.customchannels().list_next(request, result)

    except client.AccessTokenRefreshError:
        print(
            'The credentials have been revoked or expired, please re-run the '
            'application to re-authorize')
def main(argv):
  # Authenticate and construct service.
  service, flags = sample_tools.init(
      argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
      scope='https://www.googleapis.com/auth/adsense.readonly')

  ad_client_id = flags.ad_client_id

  try:
    # Let the user pick account if more than one.
    account_id = get_account_id(service)

    # Retrieve report in pages and display data as we receive it.
    start_index = 0
    rows_to_obtain = MAX_PAGE_SIZE
    result_all_pages = None
    while True:
      result = service.reports().generate(
          accountId=account_id,
          startDate='today-2y', endDate='today',
          filter=['AD_CLIENT_ID==' + ad_client_id],
          metric=['PAGE_VIEWS', 'AD_REQUESTS', 'AD_REQUESTS_COVERAGE',
                  'CLICKS', 'AD_REQUESTS_CTR', 'COST_PER_CLICK',
                  'AD_REQUESTS_RPM', 'EARNINGS'],
          dimension=['DATE'],
          sort=['+DATE'],
          startIndex=start_index,
          maxResults=rows_to_obtain).execute()

      if result_all_pages is None:
        result_all_pages = result
      else:
        result_all_pages['rows'].extend(result['rows'])

      start_index += len(result['rows'])

      # Check to see if we're going to go above the limit and get as many
      # results as we can.
      if start_index + MAX_PAGE_SIZE > ROW_LIMIT:
        rows_to_obtain = ROW_LIMIT - start_index
        if rows_to_obtain <= 0:
          break

      if start_index >= int(result['totalMatchedRows']):
        break

    print_result(fill_date_gaps(result_all_pages))

  except client.AccessTokenRefreshError:
    print ('The credentials have been revoked or expired, please re-run the '
           'application to re-authorize')
def main(argv):
  # Authenticate and construct service.
  service, flags = sample_tools.init(
      argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
      scope='https://www.googleapis.com/auth/adsense.readonly')

  # Process flags and read their values.
  saved_report_id = flags.report_id

  try:
    # Let the user pick account if more than one.
    account_id = get_account_id(service)

    # Retrieve report.
    if saved_report_id:
      result = service.accounts().reports().saved().generate(
          accountId=account_id, savedReportId=saved_report_id).execute()
    else:
      result = service.accounts().reports().generate(
          accountId=account_id, startDate='today-2m', endDate='today',
          metric=['PAGE_VIEWS', 'AD_REQUESTS', 'AD_REQUESTS_COVERAGE',
                  'CLICKS', 'AD_REQUESTS_CTR', 'COST_PER_CLICK',
                  'AD_REQUESTS_RPM', 'EARNINGS'],
          dimension=['MONTH', 'PLATFORM_TYPE_NAME'],
          sort=['+MONTH']).execute()

    result = DataCollator([result]).collate_data()

    # Display headers.
    for header in result['headers']:
      print '%25s' % header['name'],
    print

    # Display results.
    for row in result['rows']:
      for column in row:
        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')
def main(argv):
    # Authenticate and construct service.
    service, _ = sample_tools.init(
        argv, "adsense", "v1.4", __doc__, __file__, parents=[], scope="https://www.googleapis.com/auth/adsense.readonly"
    )

    try:
        account_id = get_account_id(service)
        # Retrieve account.
        request = service.accounts().get(accountId=account_id, tree=True)
        account = request.execute()

        if account:
            display_tree(account)

    except client.AccessTokenRefreshError:
        print("The credentials have been revoked or expired, please re-run the " "application to re-authorize")
def main(argv):
  # Authenticate and construct service.
  service, flags = sample_tools.init(
      argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
      scope='https://www.googleapis.com/auth/adsense.readonly')

  ad_client_id = flags.ad_client_id

  try:
    # Let the user pick account if more than one.
    account_id = get_account_id(service)

    # Retrieve custom channel list in pages and display data as we receive it.
    request = service.accounts().customchannels().list(
        accountId=account_id,
        adClientId=ad_client_id,
        maxResults=MAX_PAGE_SIZE)

    while request is not None:
      result = request.execute()
      if 'items' in result:
        custom_channels = result['items']
        for custom_channel in custom_channels:
          print ('Custom channel with id "%s" and name "%s" was found. '
                 % (custom_channel['id'], custom_channel['name']))

          if 'targetingInfo' in custom_channel:
            print '  Targeting info:'
            targeting_info = custom_channel['targetingInfo']
            if 'adsAppearOn' in targeting_info:
              print '    Ads appear on: %s' % targeting_info['adsAppearOn']
            if 'location' in targeting_info:
              print '    Location: %s' % targeting_info['location']
            if 'description' in targeting_info:
              print '    Description: %s' % targeting_info['description']
            if 'siteLanguage' in targeting_info:
              print '    Site language: %s' % targeting_info['siteLanguage']

      request = service.customchannels().list_next(request, result)

  except client.AccessTokenRefreshError:
    print ('The credentials have been revoked or expired, please re-run the '
           'application to re-authorize')
Пример #22
0
def main(argv):
    # Authenticate and construct service.
    service, flags = sample_tools.init(
        argv,
        'adsense',
        'v1.3',
        __doc__,
        __file__,
        parents=[argparser],
        scope='https://www.googleapis.com/auth/adsense.readonly')

    ad_client_id = flags.ad_client_id

    try:
        # Let the user pick account if more than one.
        account_id = get_account_id(service)
        # Retrieve ad unit list in pages and display data as we receive it.
        request = service.accounts().adunits().list(accountId=account_id,
                                                    adClientId=ad_client_id,
                                                    maxResults=MAX_PAGE_SIZE)

        while request is not None:
            result = request.execute()
            if 'items' in result:
                ad_units = result['items']
                for ad_unit in ad_units:
                    print(
                        'Ad unit with code "%s", name "%s" and status "%s" was found. '
                        %
                        (ad_unit['code'], ad_unit['name'], ad_unit['status']))

            request = service.adunits().list_next(request, result)

    except client.AccessTokenRefreshError:
        print(
            'The credentials have been revoked or expired, please re-run the '
            'application to re-authorize')
Пример #23
0
def main(argv):
    # Authenticate and construct service.
    service, flags = sample_tools.init(
        argv,
        'adsense',
        'v1.4',
        __doc__,
        __file__,
        parents=[argparser],
        scope='https://www.googleapis.com/auth/adsense.readonly')

    ad_client_id = flags.ad_client_id

    try:
        # Let the user pick account if more than one.
        account_id = get_account_id(service)

        # Retrieve URL channel list in pages and display data as we receive it.
        request = service.accounts().urlchannels().list(
            accountId=account_id,
            adClientId=ad_client_id,
            maxResults=MAX_PAGE_SIZE)

        while request is not None:
            result = request.execute()
            if 'items' in result:
                url_channels = result['items']
                for url_channel in url_channels:
                    print('URL channel with URL pattern "%s" was found.' %
                          url_channel['urlPattern'])

            request = service.customchannels().list_next(request, result)

    except client.AccessTokenRefreshError:
        print(
            'The credentials have been revoked or expired, please re-run the '
            'application to re-authorize')
def main(argv):
    # Authenticate and construct service.
    service, _ = sample_tools.init(
        argv,
        'adsense',
        'v1.4',
        __doc__,
        __file__,
        parents=[],
        scope='https://www.googleapis.com/auth/adsense.readonly')

    try:
        account_id = get_account_id(service)
        # Retrieve account.
        request = service.accounts().get(accountId=account_id, tree=True)
        account = request.execute()

        if account:
            display_tree(account)

    except client.AccessTokenRefreshError:
        print(
            'The credentials have been revoked or expired, please re-run the '
            'application to re-authorize')
def main(argv):
    # Authenticate and construct service.
    service, flags = sample_tools.init(
        argv,
        "adsense",
        "v1.4",
        __doc__,
        __file__,
        parents=[argparser],
        scope="https://www.googleapis.com/auth/adsense.readonly",
    )

    # Process flags and read their values.
    saved_report_id = flags.report_id

    try:
        # Let the user pick account if more than one.
        account_id = get_account_id(service)

        # Retrieve report.
        if saved_report_id:
            result = (
                service.accounts()
                .reports()
                .saved()
                .generate(accountId=account_id, savedReportId=saved_report_id)
                .execute()
            )
        else:
            result = (
                service.accounts()
                .reports()
                .generate(
                    accountId=account_id,
                    startDate="today-1y",
                    endDate="today",
                    metric=[
                        "PAGE_VIEWS",
                        "AD_REQUESTS",
                        "AD_REQUESTS_COVERAGE",
                        "CLICKS",
                        "AD_REQUESTS_CTR",
                        "COST_PER_CLICK",
                        "AD_REQUESTS_RPM",
                        "EARNINGS",
                    ],
                    dimension=["DATE", "MONTH", "WEEK"],
                    sort=["+DATE"],
                )
                .execute()
            )

        result = fill_date_gaps(result)

        # Display headers.
        for header in result["headers"]:
            print "%25s" % header["name"],
        print

        # Display results.
        for row in result["rows"]:
            for column in row:
                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")
Пример #26
0
def main(argv):
    # Authenticate and construct service.
    service, flags = sample_tools.init(
        argv,
        'adsense',
        'v1.4',
        __doc__,
        __file__,
        parents=[argparser],
        scope='https://www.googleapis.com/auth/adsense.readonly')

    # Process flags and read their values.
    saved_report_id = flags.report_id

    try:
        # Let the user pick account if more than one.
        account_id = get_account_id(service)

        # Retrieve report.
        if saved_report_id:
            result = service.accounts().reports().saved().generate(
                accountId=account_id, savedReportId=saved_report_id).execute()
        else:
            result = service.accounts().reports().generate(
                accountId=account_id,
                startDate='today-3d',
                endDate='today-1d',
                metric=[
                    'INDIVIDUAL_AD_IMPRESSIONS',
                    'INDIVIDUAL_AD_IMPRESSIONS_RPM', 'EARNINGS'
                ],
                dimension=[
                    'DATE', 'APP_NAME', 'AD_UNIT_NAME', 'COUNTRY_CODE',
                    'APP_PLATFORM'
                ],
                sort=['+DATE']).execute()

        result = DataCollator([result]).collate_data()

        # Display headers.
        for header in result['headers']:
            print('%25s' % header['name'], )
        #print(result)
        with open('out_3d.csv', 'w') as f:
            # Display results.
            for row in result['rows']:
                i = 0
                for column in row:
                    print('%s' % column)
                    f.write('%s' % column)
                    i += 1
                    if i < 8:
                        print(',')
                        f.write(',')
                    else:
                        f.write('\n')

            f.close()

        # 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')