Alex Fuss
Alex Fuss

Reputation: 125

Python TypeError: main() takes 0 positional arguments but 1 was given. Cloud function

I am trying to run a cloud function that pulls in data via the search analytics API. I've tested the code in both a Jupyter and Colab notebook. It runs in both just fine. However, when I deploy as a cloud function in Google Cloud I get the following error:

TypeError: main() takes 0 positional arguments but 1 was given

I don't believe I am passing a positional argument in my main function but I could be missing something here. Below is part of my code:

def main():
  def connect(key):
    scope = ["https://www.googleapis.com/auth/webmasters.readonly"]
    credentials = ServiceAccountCredentials.from_json_keyfile_name(key, scopes=scope)

    service = build("searchconsole", "v1", credentials=credentials)

    return service

  service = connect(key)

  def date_range(start_date, end_date, delta=timedelta(days=1)):
  
    current_date = start_date
    while current_date <= end_date:
        yield current_date
        current_date += delta

  for date in date_range(start_date, end_date):
    date = date.strftime("%Y-%m-%d")
    print(date)
    i = 0
    while True:

        request = {
            'startDate' : date,
            'endDate' : date,
            'dimensions' : ["query","page","country","device"],
            "searchType": "Web",
            'rowLimit' : maxRows,
            'startRow' : i * maxRows,
            'dimensionFilterGroups': [{
              'filters': [{
                'dimension': 'country',
                'operator': 'equals',
                'expression': 'usa'
              }]
          }]
        }

        response = service.searchanalytics().query(siteUrl=site_url, body=request).execute()
        print()
        if response is None:
            print("there is no response")
            break
        if 'rows' not in response:
            print("row not in response")
            break
        else:
            for row in response['rows']:
                keyword = row['keys'][0]
                page = row['keys'][1]
                country = row['keys'][2]
                device = row['keys'][3]
                output_row = [date, keyword, page, country, device, row['clicks'], row['impressions'], row['ctr'], row['position']]
                output_rows.append(output_row)
            i = i + 1
          
  df = pd.DataFrame(output_rows, columns=['date','query','page', 'country', 'device', 'clicks', 'impressions', 'ctr', 'avg_position'])
  csv_file = df.to_csv()

  def upload_blob(bucket_name, data, destination_blob_name):
    
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(destination_blob_name)
    blob.upload_from_string(data, content_type='text/csv')
    
  upload_blob('gsc-data', csv_file, 'gsc_output.csv')

Any help is much appreciated!

Upvotes: 1

Views: 685

Answers (1)

David
David

Reputation: 9721

Google Cloud Functions entry point must take an argument (a request object) as shown here.

If you don't want to use the argument, you can ignore it like this:

def main(_):
  # ...

If you want it to be optional, you can use:

def main(*args, **kwargs):
  # ...

Upvotes: 3

Related Questions