Files
daytrade/gappsscript/repository.py

116 lines
3.5 KiB
Python

import json
from loguru import logger
from gappsscript.session import session_appsscript
from config import envvar
def run_function(deployment_id, name, params=None):
request = {
'function': name,
'devMode': False # Set to False for production
}
if params:
request['parameters'] = params
response = session_appsscript.scripts().run(body=request, scriptId=deployment_id).execute()
return response
def create_project(title):
request = {"title": title}
response = session_appsscript.projects()\
.create(body=request)\
.execute()
return response
def create_apps_script():
SAMPLE_CODE = f"""
function helloWorld() {{
var spreadsheet = SpreadsheetApp.openById({envvar.ASSET_SHEET_ID});
var sheet = spreadsheet.getSheetByName('system');
var now = new Date();
sheet.getRange("I1").setValue(now.toLocaleString());
Logger.log("Timestamp: " + now.toLocaleString());
}}
function doPost(e) {{
helloWorld();
return ContentService.createTextOutput("Hello World script executed and written to cell I1.");
}}
""".strip()
SAMPLE_MANIFEST = """{ "timeZone": "Europe/Istanbul", "exceptionLogging": "CLOUD" }""".strip()
request = {
"files": [
{"name": "hello", "type": "SERVER_JS", "source": SAMPLE_CODE},
{"name": "appsscript", "type": "JSON", "source": SAMPLE_MANIFEST, },
]
}
response = session_appsscript.projects()\
.updateContent(body=request, scriptId=envvar.DEPLOYMENT_ID)\
.execute()
web_console_url = f"https://script.google.com/d/{response['scriptId']}"
return response
def update_apps_script(function_path, file_id):
with open(function_path, 'r') as file:
function_to_update = file.read().strip()
function_to_update = function_to_update.replace("FILE_ID", file_id)
manifest = """{
"timeZone": "Europe/Istanbul",
"dependencies": {},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"executionApi": {
"access": "MYSELF"
}
}""".strip()
request_body = {
"files": [
{"name": "Code.gs", "type": "SERVER_JS", "source": function_to_update},
{"name": "appsscript", "type": "JSON", "source": manifest}
]
}
response = session_appsscript.projects()\
.updateContent(scriptId="1wgbCNiC6pNu6rD3Pudd5MLmnJvukHuj7pcVCk1Snw8y1e5EdJtRjIn1I", body=request_body)\
.execute()
return response
def upload_html_to_appscript(html_file_path, file_name):
with open(html_file_path, 'r') as file:
html_content = file.read()
# Define the HTML file for the Apps Script project
html_file = {
'name': file_name, # The file name for the HTML file in Apps Script
'type': 'HTML', # File type (HTML for HTML files)
'source': html_content # The HTML content
}
manifest_file = {
'name': 'appsscript', # The manifest file name
'type': 'JSON', # The manifest file type
'source': '{ "timeZone": "Europe/Istanbul", "exceptionLogging": "CLOUD" }'.strip() # The manifest content
}
# Prepare the request payload
request = {'files': [html_file, manifest_file]}
# Make an API request to update the content of the Apps Script project
response = session_appsscript.projects().updateContent(
scriptId=envvar.DEPLOYMENT_ID,
body=request
).execute()
logger.success(f"HTML chart file file '{file_name}' uploaded successfully!")
return response