#!/usr/bin/env python2
# pythonPostToSheet.py
# Jim Miller, 9:31 PM Tue March 21, 2023
import sys, os
import requests # HTML posting
import json
import pywintypes # exceptions names
# This pulls in JSON data from a file, as specified in a command line argument, and posts it to the spreadsheet.
# The JSON has a key for the sheet name.
# This is used by the Perl weather gleaners to post to the Google sheet.
sheet_url = "https://script.google.com/macros/s/AKfycbze77MbV3O3Trx2UuhX3Ru7xYIWcYocDDOCU4VW9VrRsgVy1PrMT4R3Ag1DRVnlBW6V/exec" # weather-perl
if (len( sys.argv) > 1):
# The single argument is a filename
fileName = sys.argv[1]
print "filename =", fileName
filePath = "C:\\Users\\Jim\\Documents\\webcontent\\waconia\\" + fileName;
f = open( filePath, "r")
json_string = f.read()
postDict = json.loads( json_string)
print "sheetName =", postDict["sheetName"]
try:
# Send with POST. Note: the postDict dictionary gets converted back to a JSON string.
jsonRequest = requests.post( sheet_url, json=postDict)
# Format and print the response
print json.dumps( jsonRequest.json(), indent=2)
except:
print "Error opening spreadsheet."