Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using nextLink attribute to get the next result page

I'm using the Google APIs python client to download some data from Google Analytics. I basically copied one of their exampels and modified it to do exactly what I need.

I took this piece of code from the examples:

request = service.data().ga().get(
    ids=ids,
    start_date=str(start_date),
    end_date=str(end_date),
    dimensions=','.join(dimensions),
    filters=filters,
    sort="ga:date",
    metrics=','.join(metrics)
)

Then add it to the batch object, and execute it once it has collected 10 requests. This all works well, but the problem is, some of those requests return a nextLink. Now I could just create a new request object (with the above code) with a different start-index, but isn't there a better way?

Is there a way to just parse the nextLink into a new request object?

like image 537
Blubber Avatar asked Dec 21 '22 17:12

Blubber


1 Answers

I'm using this approach:

firstRun = True
params = {'ids':'ga:00000001',
        'start_date':'2013-07-01',
        'end_date':'2013-07-31',
        'metrics':'ga:visits',
        'dimensions':'ga:source',
        'sort':'-ga:visits',
        'start_index':1,
        'max_results':10000}

while firstRun == True or result.get('nextLink'):
    if firstRun == False:
        params['start_index'] = int(params['start_index']) + int(params['max_results'])

    result = service.data().ga().get(**params).execute()
    firstRun = False
like image 175
chonduhvan Avatar answered Dec 26 '22 21:12

chonduhvan