#!/usr/bin/env python3 import urllib.request import json import os import shutil from PIL import ImageFont from PIL import ImageDraw from PIL import Image from dotenv import load_dotenv from paramiko import SSHClient from scp import SCPClient ## get JSON from solar edge server url = os.getenv('URL') infile = os.getenv('INFILE') outfile = os.getenv('OUTFILE') font_face = os.getenv('FONT_FACE') ssh_host = os.getenv('SSH_HOST') ssh_user = os.getenv('SSH_USER') ssh_folder = os.getenv('SSH_FOLDER') req = urllib.request.Request(url) # parsing response r = urllib.request.urlopen(req).read() cont = json.loads(r.decode('utf-8')) co2 = str(round(cont['envBenefits']['gasEmissionSaved']['co2']))+" lb" trees = str(round(cont['envBenefits']['treesPlanted'])) ## create image for website fill_color = "#298246" xpos = 91 image = Image.open(infile) font = ImageFont.truetype(font_face, 19) draw = ImageDraw.Draw(image) 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 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)