Skip to content

Commit

Permalink
Geoip (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
mj-xmr authored May 20, 2022
1 parent 3ff23cf commit 6ed9240
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
54 changes: 54 additions & 0 deletions src/geolocation.py
Original file line number Diff line number Diff line change
@@ -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()
11 changes: 8 additions & 3 deletions src/sunrise_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion src/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import profitability
import generator
import kraken
import geolocation

def test():
modules = []
Expand All @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions util/config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh -e

src/geolocation.py

0 comments on commit 6ed9240

Please sign in to comment.