diff --git a/.gitignore b/.gitignore index a1eee35..b4a01fb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,9 @@ sqlite_views/ __pycache__/ +maps/*.png +maps/map_200.svg +maps/map_300.svg +maps/map_all.svg test/ *.db config.cfg diff --git a/README.md b/README.md index f99aa52..846f4e0 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ + POR HACER: + Reportes: pais - codigo - fecha - + Generar Imagen: loc y codigo? # iplocate @@ -64,6 +63,9 @@ ej. alias `alias iploc='~/ruta/script/iplocate.py'` iploc --sync - Sincroniza logs del servidor (bash script). iploc -c - Carga logs en base de datos. iploc -g - Guarda ipinfo de IPs sin registro en la BD. + + Mapa de visitas: + iploc -M - Genera mapa según registro de la BD (cod. 200 y otros). ``` **`iploc --sync`** @@ -77,6 +79,11 @@ Poblar tabla **visita** de la base de datos. Carga los registros en archivos de Consulta a `ipinfo.io` por cada ip registrada en **visita** (una vez por ip). Guarda los datos en tabla **registro**. +**`iploc -M`** +Genera mapas según vistas registradas, visitas 'infructuosas' de color rojo. Directorio `maps/`. + +![img](./maps/map_thumb.svg) + ### Otras opciones **`iploc `** diff --git a/iplocate.py b/iplocate.py index d1791b5..6b085d1 100755 --- a/iplocate.py +++ b/iplocate.py @@ -205,6 +205,8 @@ def main(): case '-t': ip = sys.argv[2] print_ipinfo(ip) + case '-M': + sql_alch.mapsgen() case _: ip = sys.argv[1] print_ipinfo(ip, False) @@ -227,6 +229,7 @@ def main(): def uso(): ayuda = f""" [bold blue]ipLocate[/bold blue] + [deep_sky_blue1]Consulta información sobre IP(s) disponibles en ipinfo.io con o sin token. Carga logs de nginx en base de datos. Consulta con ipinfo.io y registra en base de datos. @@ -248,6 +251,9 @@ def uso(): [bold yellow]iploc -c [/bold yellow][green]- Carga logs en base de datos.[/green] [bold yellow]iploc -g [/bold yellow][green]- Guarda ipinfo de IPs sin registro en la BD.[/green] + [bold blue]Mapa de visitas:[/bold blue] + [bold yellow]iploc -M [/bold yellow][green]- Genera mapa según registro de la BD (cod. 200 y otros).[/green] + """ console.print(ayuda) diff --git a/maps/map_thumb.svg b/maps/map_thumb.svg new file mode 100644 index 0000000..8695610 --- /dev/null +++ b/maps/map_thumb.svg @@ -0,0 +1,5110 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Maps & Data (C) OpenStreetMap.org contributors + + diff --git a/mapsgen.py b/mapsgen.py new file mode 100644 index 0000000..5740c34 --- /dev/null +++ b/mapsgen.py @@ -0,0 +1,89 @@ +import staticmaps +from iplocate import selfpath + +#context.set_tile_provider(staticmaps.tile_provider_ArcGISWorldImagery) +#context.set_tile_provider(staticmaps.tile_provider_CartoDarkNoLabels) +cntxt200 = staticmaps.Context() +cntxt200.set_tile_provider(staticmaps.tile_provider_OSM) +cntxt300 = staticmaps.Context() +cntxt300.set_tile_provider(staticmaps.tile_provider_OSM) +cntxtAll = staticmaps.Context() +cntxtAll.set_tile_provider(staticmaps.tile_provider_OSM) + +def map200(geolocs): + for loc in geolocs: + lat = float(loc[0].split(',')[0]) + lon = float(loc[0].split(',')[1]) + marca200 = staticmaps.create_latlng(lat,lon) + cntxt200.add_object(staticmaps.Marker(marca200 ,color=staticmaps.parse_color('#00ff29'), size=7)) + + svg_image = cntxt200.render_svg(1920, 1080) + with open(f"{selfpath}/maps/map_200.svg", "w", encoding ="utf-8") as f: + svg_image.write(f, pretty=True) + + image = cntxt200.render_cairo(1920, 1080) + image.write_to_png(f"{selfpath}/maps/map_200.png") + + +def map300(geolocs): + for loc in geolocs: + lat = float(loc[0].split(',')[0]) + lon = float(loc[0].split(',')[1]) + marca300 = staticmaps.create_latlng(lat,lon) + cntxt300.add_object(staticmaps.Marker(marca300 ,color=staticmaps.parse_color('#b20101'), size=5)) + + svg_image = cntxt300.render_svg(1920, 1080) + with open(f"{selfpath}/maps/map_300.svg", "w", encoding ="utf-8") as f: + svg_image.write(f, pretty=True) + + image = cntxt300.render_cairo(1920, 1080) + image.write_to_png(f"{selfpath}/maps/map_300.png") + + +def maps_gen(locs_200, locs_300): + map200(locs_200) + map300(locs_300) + for loc in locs_300: + lat = float(loc[0].split(',')[0]) + lon = float(loc[0].split(',')[1]) + marca3 = staticmaps.create_latlng(lat,lon) + cntxtAll.add_object(staticmaps.Marker(marca3, color=staticmaps.parse_color('#b20101'), size=5)) + + for loc in locs_200: + lat = float(loc[0].split(',')[0]) + lon = float(loc[0].split(',')[1]) + marca3 = staticmaps.create_latlng(lat,lon) + cntxtAll.add_object(staticmaps.Marker(marca3, color=staticmaps.parse_color('#00ff29'), size=6)) + + svg_image = cntxtAll.render_svg(1920, 1080) + with open(f"{selfpath}/maps/map_all.svg", "w", encoding ="utf-8") as f: + svg_image.write(f, pretty=True) + + image = cntxtAll.render_cairo(1920, 1080) + image.write_to_png(f"{selfpath}/maps/map_all.png") + maps_thumbs(locs_200, locs_300) + + +def maps_thumbs(locs_200, locs_300): + cntxtdemo = staticmaps.Context() + cntxtdemo.set_tile_provider(staticmaps.tile_provider_OSM) + + for loc in locs_300: + lat = float(loc[0].split(',')[0]) + lon = float(loc[0].split(',')[1]) + demo300 = staticmaps.create_latlng(lat,lon) + cntxtdemo.add_object(staticmaps.Marker(demo300, color=staticmaps.parse_color('#b20101'), size=4)) + + for loc in locs_200: + lat = float(loc[0].split(',')[0]) + lon = float(loc[0].split(',')[1]) + demo200 = staticmaps.create_latlng(lat,lon) + cntxtdemo.add_object(staticmaps.Marker(demo200, color=staticmaps.parse_color('#00ff29'), size=5)) + + svg_thumb = cntxtdemo.render_svg(1024, 768) + with open(f"{selfpath}/maps/map_thumb.svg", "w", encoding ="utf-8") as f: + svg_thumb.write(f, pretty=True) + + image = cntxtdemo.render_cairo(1024, 768) + image.write_to_png(f"{selfpath}/maps/map_thumb.png") + diff --git a/sql_alch.py b/sql_alch.py index 0f847a3..21c01ee 100644 --- a/sql_alch.py +++ b/sql_alch.py @@ -9,10 +9,11 @@ from sqlalchemy.orm import relationship, sessionmaker from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, String, Sequence, update from sqlalchemy.orm.session import Session -from sqlalchemy.sql.expression import select +from sqlalchemy.sql.expression import distinct, select from sqlalchemy.sql.schema import ForeignKey from rich.progress import Progress, track from rich.console import Console +from mapsgen import maps_gen ip_regx = "^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$" logs_dir = parser.get('bash_script', 'destino_log') @@ -224,7 +225,6 @@ def carga_error_logs(log): nombre_log = log.split('/')[-1] console.print(f'[yellow]Registrando [[/yellow]{nombre_log}[yellow]][/yellow]') try: - #with Progress(), open(log, 'r') as lista: with open(log, 'r') as lista: try: largo = subprocess.run(['wc', '-l', log], capture_output=True, text=True) @@ -356,13 +356,11 @@ def carga_registro_ip(ip_info): session.commit() except Exception as ex: print('Exception: ', ex) - # aqui progress bar registro visita stmt = update(Visita).where(Visita.ip == ip_info['ip']).values(registro=1).\ execution_options(synchronize_session="fetch") #result = session.execute(stmt) try: session.execute(stmt) - # aqui progress bar update visita (registro = 1) session.commit() except Exception as ex: print('Exception: ', ex) @@ -414,3 +412,13 @@ def registro_ips(): progress.update(task1, advance=avance) console.print('\n[bold yellow]Registro en base de datos finalizado.[/bold yellow]') + +def mapsgen(): + try: + stmn = session.query(Registro.geoloc.distinct()).join('visitas').where(Visita.cod_html==200) + loc_200 = session.execute(stmn).all() + stmn = session.query(Registro.geoloc.distinct()).join('visitas').where(Visita.cod_html!=200) + loc_300 = session.execute(stmn).all() + maps_gen(loc_200, loc_300) + except Exception as ex: + print('Exception: ', ex)