diff --git a/.gitignore b/.gitignore index ade7961..5298228 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ .direnv/* __pycache__/* +build/* +dist/* +venv/* .env diff --git a/README.md b/README.md index 155fde0..6138fc2 100644 --- a/README.md +++ b/README.md @@ -29,15 +29,27 @@ pip install -r requirements.txt edit `.env` and update accordingly +add to user crontab to update site every 5 minutes +``` +*/5 * * * * cd ~/python/solar_edge && direnv exec . python solar_edge.py > /dev/null 2>&1 +``` + + +# Create pyinstaller +``` +pyinstaller --onefile solar_edge.spec +``` 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 +*/5 * * * * ~/bin/solar_edge > /dev/null 2>&1 ``` + # Sample .env if installed on the server ``` SSH_HOST= +SSH_PORT= SSH_USER= SSH_FOLDER=/var/www/wordpress URL=https://monitoringapi.solaredge.com/site/55555/envBenefits?systemUnits=Imperial&api_key= @@ -49,6 +61,7 @@ FONT_FACE=Biko_Regular.otf # Sample .env file for scp ``` SSH_HOST=myserver.com +SSH_PORT=22 SSH_USER=www-data SSH_FOLDER=/var/www/wordpress URL=https://monitoringapi.solaredge.com/site/55555/envBenefits?systemUnits=Imperial&api_key= diff --git a/env.sample b/env.sample index 4d13c16..7ec29ae 100644 --- a/env.sample +++ b/env.sample @@ -1,4 +1,5 @@ SSH_HOST=myserver.com +SSH_PORT=22 SSH_USER=www-data SSH_FOLDER=/var/www/wordpress URL=https://monitoringapi.solaredge.com/site/55555/envBenefits?systemUnits=Imperial&api_key= diff --git a/requirements.txt b/requirements.txt index 810567b..cef2caf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +altgraph==0.17 asn1crypto==0.24.0 bcrypt==3.1.6 cffi==1.12.2 @@ -8,6 +9,8 @@ Pillow==6.0.0 pkg-resources==0.0.0 pyasn1==0.4.5 pycparser==2.19 +pyinstaller==4.0 +pyinstaller-hooks-contrib==2020.9 PyNaCl==1.3.0 python-dotenv==0.10.1 scp==0.13.2 diff --git a/solar_edge.py b/solar_edge.py index 34bf4e9..c93e505 100755 --- a/solar_edge.py +++ b/solar_edge.py @@ -11,12 +11,24 @@ from dotenv import load_dotenv from paramiko import SSHClient from scp import SCPClient +#### HEAR BE DRAGONS +# set python environment +if getattr(sys, 'frozen', False): + bundle_dir = sys._MEIPASS +else: + # we are running in a normal Python environment + bundle_dir = os.path.dirname(os.path.abspath(__file__)) + +# load environmental variables +load_dotenv(bundle_dir + "/.env") + ## get JSON from solar edge server -url = os.getenv('URL') +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_port = os.getenv('SSH_PORT') ssh_user = os.getenv('SSH_USER') ssh_folder = os.getenv('SSH_FOLDER') req = urllib.request.Request(url) @@ -31,8 +43,8 @@ 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) +image = Image.open(bundle_dir + "/" + infile) +font = ImageFont.truetype(bundle_dir + "/" + font_face, 19) draw = ImageDraw.Draw(image) draw.text((xpos, 81), co2, font=font, fill=fill_color) @@ -44,7 +56,7 @@ image.save(outfile) if ssh_host: ssh = SSHClient() ssh.load_system_host_keys() - ssh.connect(hostname=ssh_host, username=ssh_user) + ssh.connect(hostname=ssh_host, port=ssh_port, username=ssh_user) scp = SCPClient(ssh.get_transport()) scp.put(outfile, ssh_folder) diff --git a/solar_edge.spec b/solar_edge.spec new file mode 100644 index 0000000..5476e6a --- /dev/null +++ b/solar_edge.spec @@ -0,0 +1,33 @@ +# -*- mode: python ; coding: utf-8 -*- + +block_cipher = None + + +a = Analysis(['solar_edge.py'], + pathex=['/home/fwarren/builds/solar_edge'], + binaries=[], + datas=[('.env','.'),('Biko_Regular.otf','.'),('environmental_benefits_blank.png','.')], + hiddenimports=[], + hookspath=[], + runtime_hooks=[], + excludes=[], + win_no_prefer_redirects=False, + win_private_assemblies=False, + cipher=block_cipher, + noarchive=False) +pyz = PYZ(a.pure, a.zipped_data, + cipher=block_cipher) +exe = EXE(pyz, + a.scripts, + a.binaries, + a.zipfiles, + a.datas, + [], + name='solar_edge', + debug=False, + bootloader_ignore_signals=False, + strip=False, + upx=True, + upx_exclude=[], + runtime_tmpdir=None, + console=True )