From 6ed924058dc6729548af9347868bf08c256caa2a Mon Sep 17 00:00:00 2001 From: mj-xmr <63722585+mj-xmr@users.noreply.github.com> Date: Fri, 20 May 2022 11:28:10 +0200 Subject: [PATCH] Geoip (#26) --- .github/workflows/linux.yml | 2 ++ README.md | 1 + requirements.txt | 3 ++- src/geolocation.py | 54 +++++++++++++++++++++++++++++++++++++ src/sunrise_lib.py | 11 +++++--- src/tests.py | 3 ++- util/config.sh | 4 +++ 7 files changed, 73 insertions(+), 5 deletions(-) create mode 100755 src/geolocation.py create mode 100755 util/config.sh diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index d8f1e15..9400111 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -54,6 +54,8 @@ jobs: run: util/deps-pull.sh - name: build & install the unmanaged dependencies run: util/deps-build.sh + - name: Configure your rig + run: util/config.sh - name: build run: ./ci-default - name: run demo diff --git a/README.md b/README.md index f2d40fa..f31df33 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ pip install -r requirements.txt # Install Python packages (use this command or i ./util/prep-env.sh # Prepare the environment - downloads example data and creates useful symlinks ./util/deps-pull.sh # Download the maintaned dependencies ./util/deps-build.sh # Build and install the unmanaged dependencies (uses sudo for installation) +./util/config.sh # Configure your rig ``` ## Building & running diff --git a/requirements.txt b/requirements.txt index 5207fdf..c899e33 100644 --- a/requirements.txt +++ b/requirements.txt @@ -36,4 +36,5 @@ beautifulsoup4==4.11.1 wget==3.2 cairosvg>=0.7.0 Pillow==9.1.0 -# +# Geo location +geocoder>=1.38.1 diff --git a/src/geolocation.py b/src/geolocation.py new file mode 100755 index 0000000..814e1b7 --- /dev/null +++ b/src/geolocation.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# https://stackoverflow.com/a/11236372 +# apt install gfortran libffi-dev +# pip3 install pvlib ephem pytz beautifulsoup4 cairosvg wget requests Pillow + + +import os +from pytz import timezone +import sunrise_lib + +import geocoder + +def get_lat_lon(): + return geocoder.ip('me') + +def main(): + test() + path_local = sunrise_lib.get_config_path('geo') + config_geo = sunrise_lib.config_geo + print("Config city:", config_geo.geo.city) + print("Config lat:", config_geo.geo.lat) + print("Config lon:", config_geo.geo.lon) + + print() + g = get_lat_lon() + city = g.city.lower() + print("Detected city:", city) + print("Detected lat/lon:", g.latlng) + #print("Detected country:", g.country) + + if config_geo.geo.city != city: + print("Please update city") + if config_geo.geo.lat != g.latlng[0]: + print("Please update lat") + if config_geo.geo.lon != g.latlng[1]: + print("Please update lon") + + print("Your config file is stored at:") + print(path_local) + +def test(): + g = get_lat_lon() + assert len(g.latlng) == 2 + assert g.latlng[0] != 0 + assert g.latlng[1] != 0 + print("Detected lat/lon:", get_lat_lon().latlng) + #print("City:", g.city) + #run_main(elev, show_plots) + + +if __name__ == "__main__": + main() diff --git a/src/sunrise_lib.py b/src/sunrise_lib.py index be2ad6b..af9c93c 100644 --- a/src/sunrise_lib.py +++ b/src/sunrise_lib.py @@ -30,15 +30,20 @@ config_builder = ConfigBuilder() config = config_builder.parse_config('config.json') -def get_config(name): +def get_config_path(name): os.makedirs(HOME + config.sunrise_lib.DIR_CFG, exist_ok=True) path_local = HOME + config.sunrise_lib.DIR_CFG + "/{}.json".format(name) if not os.path.isfile(path_local): - cfg_template = 'src/system-cfg/{}-template.json'.format(name) + cfg_template = 'system-cfg/{}-template.json'.format(name) + if not os.path.isfile(cfg_template): + cfg_template = "src/" + cfg_template print("Not found: " + path_local) print("Copying " + cfg_template + " to " + path_local) shutil.copy(cfg_template, path_local) - + return path_local + +def get_config(name): + path_local = get_config_path(name) config_local = config_builder.parse_config(path_local) return config_local diff --git a/src/tests.py b/src/tests.py index 0cc0778..3cc35bd 100755 --- a/src/tests.py +++ b/src/tests.py @@ -7,6 +7,7 @@ import profitability import generator import kraken +import geolocation def test(): modules = [] @@ -18,7 +19,7 @@ def test(): modules.append(weather_lib) modules.append(profitability) modules.append(kraken) - + modules.append(geolocation) for module in modules: module.test() diff --git a/util/config.sh b/util/config.sh new file mode 100755 index 0000000..31dbc7f --- /dev/null +++ b/util/config.sh @@ -0,0 +1,4 @@ +#!/bin/sh -e + +src/geolocation.py +