# Copyright 2021 Google LLC
#
# 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
#
#     https://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.


#!/usr/bin/env python3

"""
This is the entry point for the cronjob for updating database information at the
end of each day UTC time.
"""

# Imports the method for fetching formatted data from google trends
from scripts.fetch_trends import get_trending_searches, get_updated_daily_data
from scripts.database_updates import get_investment_data, update_investment_database, update_trending_database
from scripts.config import Secret

# Imports the Google Cloud client library
from google.cloud import datastore

import requests


if __name__ == "__main__":
    # Update data in Datastore:

    # Instantiates a client
    datastore_client = datastore.Client()
    # Retrieve relevant data from datastore
    entities = get_investment_data(datastore_client)
    for entity in entities:
        # Retrieve up to date trends data for each search term
        daily_data = get_updated_daily_data(entity)
        # Add up to date data do datastore
        update_investment_database(daily_data, datastore_client)

    trending_data = get_trending_searches()
    update_trending_database(trending_data, datastore_client)

    # Update data in Cloud SQL: 

    # password prevents 3rd parties running the cron servlet at undesired times. Stored in a Secret() class
    password = Secret().password
    # create a connection request for the cron servlet, with password as a parameter
    req = requests.get("http://localhost:8080/cron", params={"password" : password}) # replace this link with the googleplex link in prod
    # close the request
    req.close()
@@ -1017,39 +1017,29 @@
 rts 
-the Google Cloud client library
+external dependencies
 %0Afro
@@ -1070,17 +1070,16 @@
 tastore%0A
-%0A
 import r
@@ -1090,35 +1090,71 @@
 sts%0A
-%0A%0Aif __name__ == %22__main__%22
+from flask import Flask%0A%0A@app.route('/start')%0Adef run_updates()
 :%0A  
@@ -2147,8 +2147,619 @@
 .close()
+%0A    return %7B%22success%22 : True%7D%0A%0Aapp = Flask(__name__)%0A%0A%0Aif __name__ == '__main__':%0A    # This is used when running locally only. When deploying to Google App%0A    # Engine, a webserver process such as Gunicorn will serve the app. This%0A    # can be configured by adding an %60entrypoint%60 to app.yaml.%0A    # Flask's development server will automatically serve static files in%0A    # the %22static%22 directory. See:%0A    # http://flask.pocoo.org/docs/1.0/quickstart/#static-files. Once deployed,%0A    # App Engine itself will serve those files as configured in app.yaml.%0A    app.run(host='127.0.0.1', port=8081, debug=True)

Make main scripts into flask app