!pip install git+https://github.com/google/starthinker


"""
Explanation: 1. Install Dependencies
First install the libraries needed to execute recipes, this only needs to be done once, then click play.
End of explanation
"""


CLOUD_PROJECT = 'PASTE PROJECT ID HERE'

print("Cloud Project Set To: %s" % CLOUD_PROJECT)


"""
Explanation: 2. Get Cloud Project ID
To run this recipe requires a Google Cloud Project, this only needs to be done once, then click play.
End of explanation
"""


CLIENT_CREDENTIALS = 'PASTE CREDENTIALS HERE'

print("Client Credentials Set To: %s" % CLIENT_CREDENTIALS)


"""
Explanation: 3. Get Client Credentials
To read and write to various endpoints requires downloading client credentials, this only needs to be done once, then click play.
End of explanation
"""


FIELDS = {
  'auth_read': 'user',  # Credentials used for reading data.
  'id_dataset': '',
  'id_query': 'SELECT * FROM `Dataset.Table`;',
  'id_legacy': False,
  'destination_dataset': '',
  'destination_table': '',
}

print("Parameters Set To: %s" % FIELDS)


"""
Explanation: 4. Enter Line Item To BigQuery Via Query Parameters
Move using an Id query.
 1. Specify the query that will pull the lineitem ids to download.
 1. Specify the dataset and table where the lineitems will be written.
 1. The schema will match <a href='https://developers.google.com/bid-manager/guides/entity-write/format' target='_blank'>Entity Write Format</a>.
Modify the values below for your use case, can be done multiple times, then click play.
End of explanation
"""


from starthinker.util.project import project
from starthinker.script.parse import json_set_fields

USER_CREDENTIALS = '/content/user.json'

TASKS = [
  {
    'lineitem': {
      'auth': 'user',
      'read': {
        'line_items': {
          'single_cell': True,
          'bigquery': {
            'dataset': {'field': {'name': 'id_dataset','kind': 'string','order': 1,'default': ''}},
            'query': {'field': {'name': 'id_query','kind': 'string','order': 2,'default': 'SELECT * FROM `Dataset.Table`;'}},
            'legacy': {'field': {'name': 'id_legacy','kind': 'boolean','order': 3,'default': False}}
          }
        },
        'out': {
          'bigquery': {
            'dataset': {'field': {'name': 'destination_dataset','kind': 'string','order': 4,'default': ''}},
            'table': {'field': {'name': 'destination_table','kind': 'string','order': 5,'default': ''}}
          }
        }
      }
    }
  }
]

json_set_fields(TASKS, FIELDS)

project.initialize(_recipe={ 'tasks':TASKS }, _project=CLOUD_PROJECT, _user=USER_CREDENTIALS, _client=CLIENT_CREDENTIALS, _verbose=True, _force=True)
project.execute(_force=True)


"""
Explanation: 5. Execute Line Item To BigQuery Via Query
This does NOT need to be modified unless you are changing the recipe, click play.
End of explanation
"""


