From 13082e3b5db0f4ab6f46ed2f82575d50f8f8d032 Mon Sep 17 00:00:00 2001 From: "Fredrick W. Warren" Date: Mon, 15 Apr 2019 16:26:14 -0700 Subject: [PATCH] added scp/local code --- README.md | 31 +++++++++++++++++++++++++------ env.sample | 8 ++++---- solar_edge.py | 21 +++++++++++++-------- 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 8fb394c..155fde0 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,8 @@ The package direnv and the follwing changes to the user account: * bash.rc (https://github.com/direnv/direnv/wiki/Python#bash) * venv (https://github.com/direnv/direnv/wiki/Python#venv-stdlib-module) -Assumes the use of direnv and ssh keys setup to upload graphic to website. If hosted on the website the ssh/scp portion can be replace with a copy. -` -import shutil -... -shutil.copy2(out_file, '/var/www/') -` +If `ssh_host` is blank will copy file locally instead of scping file to sever. To scp valid sshkeys are required. + # Bulid Instructions assumes installed to `~/python/solar_edge` @@ -38,3 +34,26 @@ add to user crontab to update site every 5 minutes ``` 05 * * * * cd ~/python/solar_edge && direnv exec . python solar_edge.py > /dev/null 2>&1 ``` + +# Sample .env if installed on the server +``` +SSH_HOST= +SSH_USER= +SSH_FOLDER=/var/www/wordpress +URL=https://monitoringapi.solaredge.com/site/55555/envBenefits?systemUnits=Imperial&api_key= +INFILE=environmental_benefits_blank.png +OUTFILE=solar.png +FONT_FACE=Biko_Regular.otf +``` + +# Sample .env file for scp +``` +SSH_HOST=myserver.com +SSH_USER=www-data +SSH_FOLDER=/var/www/wordpress +URL=https://monitoringapi.solaredge.com/site/55555/envBenefits?systemUnits=Imperial&api_key= +INFILE=environmental_benefits_blank.png +OUTFILE=solar.png +FONT_FACE=Biko_Regular.otf +``` + diff --git a/env.sample b/env.sample index aa3c56d..4d13c16 100644 --- a/env.sample +++ b/env.sample @@ -1,7 +1,7 @@ -SSH_HOST= -SSH_USER= -SSH_FOLDER= +SSH_HOST=myserver.com +SSH_USER=www-data +SSH_FOLDER=/var/www/wordpress URL=https://monitoringapi.solaredge.com/site/55555/envBenefits?systemUnits=Imperial&api_key= INFILE=environmental_benefits_blank.png -OUTFILE= +OUTFILE=solar.png FONT_FACE=Biko_Regular.otf diff --git a/solar_edge.py b/solar_edge.py index 000a2e5..34bf4e9 100755 --- a/solar_edge.py +++ b/solar_edge.py @@ -3,6 +3,7 @@ import urllib.request import json import os +import shutil from PIL import ImageFont from PIL import ImageDraw from PIL import Image @@ -38,14 +39,18 @@ draw.text((xpos, 81), co2, font=font, fill=fill_color) draw.text((xpos, 157), trees, font=font, fill=fill_color) image.save(outfile) -## upload image to website -ssh = SSHClient() -ssh.load_system_host_keys() -ssh.connect(hostname=ssh_host, username=ssh_user) -scp = SCPClient(ssh.get_transport()) -scp.put(outfile, ssh_folder) -scp.close() -ssh.close() +## upload image to website or copy to folder +if ssh_host: + ssh = SSHClient() + ssh.load_system_host_keys() + ssh.connect(hostname=ssh_host, username=ssh_user) + + scp = SCPClient(ssh.get_transport()) + scp.put(outfile, ssh_folder) + scp.close() + ssh.close() +else: + shutil.copy2(outfile, ssh_folder) os.remove(outfile)