<commit_before>#!/usr/bin/env python2.7

# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Command-line tool for interacting with the Google Consumer Surveys API.

To run, generate a client secret using https://console.developers.google.com/
under the APIs and Auth Tab for your project. Then download the JSON object
and save it as client_secrets.json

For more instructions on how to obtain the local files necessary for OAuth
authorization, please see https://github.com/google/consumer-surveys

Download and install the python Google Oauth Library:
https://code.google.com/p/google-api-python-client/downloads/list

Or install it with PIP:
$ pip install google-api-python-client

To create a survey:
$ ./example_client.py create --owner_email <email1> <email2> \
    --client_secrets_file <file>

To set the number of desired responses on a survey:
$ ./example_client.py set_response_count --survey_id <id> \
    --client_secrets_file <file>

To start the survey:
$ ./example_client.py start --survey_id <id> --client_secrets_file <file>


To download survey results:
$ ./example_client.py fetch --survey_id <id> --results_file=~/my_results.xls \
    --client_secrets_file <file>

Alternatively, to download survey results with a Service Account:
$ ./example_client.py fetch --survey_id <id> --results_file=~/my_results.xls \
    --service_account <email>  --service_account_secrets_file <file>
"""

import argparse
import httplib2
import json
import os

from apiclient.discovery import build_from_document
import googleapiclient

from oauth2client import client
from oauth2client import clientsecrets
from oauth2client import tools
from oauth2client import file as oauth_file
from oauth2client.client import flow_from_clientsecrets
from oauth2client.client import OAuth2Credentials
from oauth2client.client import SignedJwtAssertionCredentials

_SERVICE_ACCOUNT_SECRETS = 'robot_account_secret.json'
_OAUTH_CLIENT_SECRETS = 'client_secrets.json'
OAUTH2_STORAGE = 'oauth2.dat'
SCOPES = [
    'https://www.googleapis.com/auth/consumersurveys',
    'https://www.googleapis.com/auth/consumersurveys.readonly',
    'https://www.googleapis.com/auth/userinfo.email',
]

# Constants that enumerate the various operations that the client allows.

# Create a new survey.
_CREATE = 'create'
# Set the desired response count of an existing survey.
_SET_RESPONSE_COUNT = 'set_response_count'
# List the surveys that this user owns.
_LIST = 'list'
# Fetch the results of an existing survey.
_FETCH = 'fetch'
# Fetch the results of an existing survey.
_START = 'start'

_OPERATIONS = [
    _CREATE,
    _SET_RESPONSE_COUNT,
    _START,
    _FETCH,
    _LIST,
]

_DESCRIPTION = """

You must choose one of the following operations:
  - create: To create a new survey.
  - set_response_count: Set the number of desired responses for a given survey.
  - start: Start the given survey.
  - fetch: Fetch the results in .xls format for a given survey.
  - list: List the surveys that are owned by this user.

For a full list of available flags, use the --help flag.
"""


def main():
    parser = argparse.ArgumentParser(
        usage=_DESCRIPTION,
        )
    parser.add_argument('operation', choices=_OPERATIONS,
                        help='The operation to perform.')
    parser.add_argument('--survey_id',
                        help='Survey ID to operate on.')
    parser.add_argument('--owner_emails',
                        nargs='+',
                        help='List of survey owners (space separated) for a '
                             'new survey.')
    parser.add_argument('--results_file',
                        default='results.xls',
                        help='filename to store excel results.')

    # Service Account flags.
    parser.add_argument('--service_account',
                        help='Service account email to use.  Make sure that '
                             '--service_account_secrets_file is set correctly '
                             '.')
    parser.add_argument('--service_account_secrets_file',
                        default=_SERVICE_ACCOUNT_SECRETS,
                        help='Path to the Service Account secrets JSON file.')

    # OAuth2 client ID flags.
    parser.add_argument('--client_secrets_file',
                        default=_OAUTH_CLIENT_SECRETS,
                        help='Path to the OAuth client secrets JSON file.')

    # Arguments required by tools.run_flow
    parser.add_argument('--logging_level',
                        default='INFO',
                        help='default logging level to use.')
    parser.add_argument('--auth_host_name',
                        default='localhost',
                        help='Hostname for redirects during the OAuth flow.')
    parser.add_argument('--auth_host_port',
                        default=[8080],
                        help='Port for redirects during the OAuth flow.')
    parser.add_argument('--noauth_local_webserver',
                        action='store_true',
                        default=False,
                        help='Run a local webserver to handle redirects.')

    args = parser.parse_args()

    try:
        auth_http = setup_auth(args)
    except clientsecrets.InvalidClientSecretsError, e:
        print ('Unable to setup authorization with the given credentials.  %s'
               % e)
        return

    # Load the local copy of the discovery document
    f = file(os.path.join(
        os.path.dirname(__file__),
        "consumersurveys_v2beta_discovery.json"), "r")
    discovery_file = f.read()
    f.close()

    # Construct a service from the local documents
    try:
        cs = build_from_document(service=discovery_file, http=auth_http)
    except ValueError, e:<commit_msg>update example app<commit_after>#!/usr/bin/env python2.7

# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Command-line tool for interacting with the Google Consumer Surveys API.

To run, generate a client secret using https://console.developers.google.com/
under the APIs and Auth Tab for your project. Then download the JSON object
and save it as client_secrets.json

For more instructions on how to obtain the local files necessary for OAuth
authorization, please see https://github.com/google/consumer-surveys

Download and install the python Google Oauth Library:
https://code.google.com/p/google-api-python-client/downloads/list

Or install it with PIP:
$ pip install google-api-python-client

To create a survey:
$ ./example_client.py create --owner_email <email1> <email2> \
    --client_secrets_file <file>

To set the number of desired responses on a survey:
$ ./example_client.py set_response_count --survey_id <id> \
    --client_secrets_file <file>

To start the survey:
$ ./example_client.py start --survey_id <id> --client_secrets_file <file>


To download survey results:
$ ./example_client.py fetch --survey_id <id> --results_file=~/my_results.xls \
    --client_secrets_file <file>

Alternatively, to download survey results with a Service Account:
$ ./example_client.py fetch --survey_id <id> --results_file=~/my_results.xls \
    --service_account <email>  --service_account_secrets_file <file>
"""

import argparse
import httplib2
import json
import os

from apiclient.discovery import build_from_document
import googleapiclient

from oauth2client import client
from oauth2client import clientsecrets
from oauth2client import tools
from oauth2client import file as oauth_file
from oauth2client.client import flow_from_clientsecrets
from oauth2client.client import OAuth2Credentials
from oauth2client.client import SignedJwtAssertionCredentials

_SERVICE_ACCOUNT_SECRETS = 'robot_account_secret.json'
_OAUTH_CLIENT_SECRETS = 'client_secrets.json'
OAUTH2_STORAGE = 'oauth2.dat'
SCOPES = [
    'https://www.googleapis.com/auth/consumersurveys',
    'https://www.googleapis.com/auth/consumersurveys.readonly',
    'https://www.googleapis.com/auth/userinfo.email',
]

# Constants that enumerate the various operations that the client allows.

# Create a new survey.
_CREATE = 'create'
# Set the desired response count of an existing survey.
_SET_RESPONSE_COUNT = 'set_response_count'
# List the surveys that this user owns.
_LIST = 'list'
# Fetch the results of an existing survey.
_FETCH = 'fetch'
# Fetch the results of an existing survey.
_START = 'start'

_OPERATIONS = [
    _CREATE,
    _SET_RESPONSE_COUNT,
    _START,
    _FETCH,
    _LIST,
]

_DESCRIPTION = """

You must choose one of the following operations:
  - create: To create a new survey.
  - set_response_count: Set the number of desired responses for a given survey.
  - start: Start the given survey.
  - fetch: Fetch the results in .xls format for a given survey.
  - list: List the surveys that are owned by this user.

For a full list of available flags, use the --help flag.
"""


def main():
    parser = argparse.ArgumentParser(
        usage=_DESCRIPTION,
        )
    parser.add_argument('operation', choices=_OPERATIONS,
                        help='The operation to perform.')
    parser.add_argument('--survey_id',
                        help='Survey ID to operate on.')
    parser.add_argument('--owner_emails',
                        nargs='+',
                        help='List of survey owners (space separated) for a '
                             'new survey.')
    parser.add_argument('--results_file',
                        default='results.xls',
                        help='filename to store excel results.')

    # Service Account flags.
    parser.add_argument('--service_account',
                        help='Service account email to use.  Make sure that '
                             '--service_account_secrets_file is set correctly '
                             '.')
    parser.add_argument('--service_account_secrets_file',
                        default=_SERVICE_ACCOUNT_SECRETS,
                        help='Path to the Service Account secrets JSON file.')

    # OAuth2 client ID flags.
    parser.add_argument('--client_secrets_file',
                        default=_OAUTH_CLIENT_SECRETS,
                        help='Path to the OAuth client secrets JSON file.')

    # Arguments required by tools.run_flow
    parser.add_argument('--logging_level',
                        default='INFO',
                        help='default logging level to use.')
    parser.add_argument('--auth_host_name',
                        default='localhost',
                        help='Hostname for redirects during the OAuth flow.')
    parser.add_argument('--auth_host_port',
                        default=[8080],
                        help='Port for redirects during the OAuth flow.')
    parser.add_argument('--noauth_local_webserver',
                        action='store_true',
                        default=False,
                        help='Run a local webserver to handle redirects.')

    args = parser.parse_args()

    try:
        auth_http = setup_auth(args)
    except clientsecrets.InvalidClientSecretsError, e:
        print ('Unable to setup authorization with the given credentials.  %s'
               % e)
        return

    # Load the local copy of the discovery document
    f = file(os.path.join(
        os.path.dirname(__file__),
        "consumersurveys_v2_discovery.json"), "r")
    discovery_file = f.read()
    f.close()

    # Construct a service from the local documents
    try:
        cs = build_from_document(service=discovery_file, http=auth_http)
    except ValueError, e:
{'fraction_non_alphanumeric': 0.0855257928622526, 'fraction_numerical': 0.004610126381050791, 'mean_word_length': 2.7413024085637825, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 12, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2857943', 'n_tokens_mistral': 3584, 'n_tokens_neox': 3459, 'n_words': 1133}