I'm trying to make a google api downloader that will bypass api restrictions. Unfortunately, what I do does not work. As I do not introduce loops, unfortunately the change of dates by hand works when downloading historical data is very easy. For new data, no problem skypt generates data daily. Below is the code at which I try to download data from a specific range day by day.
The problem occurs in this part of the code
def get_top_keywords(service, profile_id):
for day_number in range(total_days):
return service.data().ga().get(
ids='ga:' + profile_id,
start_date='1daysAgo',
end_date=(start_date + dt.timedelta(days = day_number)).date(),
metrics='ga:sessions, ga:newUsers, ga:users, ga:organicSearches, ga:pageviews, ga:bounceRate',
dimensions='ga:date, ga:source, ga:medium',
max_results='10000').execute()
Below the whole code
import argparse
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import csv
from __future__ import print_function
from googleapiclient.errors import HttpError
from googleapiclient import sample_tools
from oauth2client.client import AccessTokenRefreshError
import datetime as dt
start_date = dt.datetime(2019, 01,1)
end_date = dt.datetime(2019, 01,5)
total_days = (end_date - start_date).days + 1
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'analytics', 'v3', __doc__, __file__,
scope='https://www.googleapis.com/auth/analytics.readonly')
# Try to make a request to the API. Print the results or handle errors.
try:
first_profile_id = '11111111111' # Hard Code View Profile ID Here
if not first_profile_id:
print('Could not find a valid profile for this user.')
else:
results = get_top_keywords(service, first_profile_id)
print_results(results)
except TypeError as error:
# Handle errors in constructing a query.
print(('There was an error in constructing your query : %s' % error))
except HttpError as error:
# Handle API errors.
print(('Arg, there was an API error : %s : %s' %
(error.resp.status, error._get_reason())))
except AccessTokenRefreshError:
# Handle Auth errors.
print ('The credentials have been revoked or expired, please re-run '
'the application to re-authorize')
def get_first_profile_id(service):
accounts = service.management().accounts().list().execute()
if accounts.get('items'):
firstAccountId = accounts.get('items')[0].get('id')
webproperties = service.management().webproperties().list(
accountId=firstAccountId).execute()
if webproperties.get('items'):
firstWebpropertyId = webproperties.get('items')[0].get('id')
profiles = service.management().profiles().list(
accountId=firstAccountId,
webPropertyId=firstWebpropertyId).execute()
if profiles.get('items'):
return profiles.get('items')[0].get('id')
return None
def get_top_keywords(service, profile_id):
for day_number in range(total_days):
return service.data().ga().get(
ids='ga:' + profile_id,
start_date='1daysAgo',
end_date=(start_date + dt.timedelta(days = day_number)).date(),
metrics='ga:sessions, ga:newUsers, ga:users, ga:organicSearches, ga:pageviews, ga:bounceRate',
dimensions='ga:date, ga:source, ga:medium',
max_results='10000').execute()
def print_results(results):
print()
print('Profile Name: %s' % results.get('profileInfo').get('profileName'))
print()
# Open a file.
filepath = '/temp/' #change this to your actual file path
filename = 'temp.csv' #change this to your actual file name
f = open(filepath + filename, 'wt')
# Wrap file with a csv.writer
writer = csv.writer(f, lineterminator='\n')
# Write header.
header = [h['name'][3:] for h in results.get('columnHeaders')] #this takes the column headers and gets rid of ga: prefix
writer.writerow(header)
print(''.join('%30s' %h for h in header))
# Write data table.
if results.get('rows', []):
for row in results.get('rows'):
writer.writerow(row)
print(''.join('%30s' %r for r in row))
print('\n')
print ('Success Data Written to CSV File')
print ('filepath = ' + filepath)
print ('filename = '+ filename)
else:
print ('No Rows Found')
# Close the file.
f.close()
if __name__ == '__main__':
main(sys.argv)
What should I improve to make the date automatically reach the end of the loop?
Why the loop doesn't work, I'm just starting with python but I'm trying