+: mapsgen.py, maps/

Añadida opción para generar mapas de visitas "-M"
This commit is contained in:
jp.av.dev 2022-05-26 01:30:51 -04:00
parent 72fb322e98
commit 3b606ede28
6 changed files with 5229 additions and 5 deletions

4
.gitignore vendored
View File

@ -1,5 +1,9 @@
sqlite_views/ sqlite_views/
__pycache__/ __pycache__/
maps/*.png
maps/map_200.svg
maps/map_300.svg
maps/map_all.svg
test/ test/
*.db *.db
config.cfg config.cfg

View File

@ -1,6 +1,5 @@
+ POR HACER: + POR HACER:
+ Reportes: pais - codigo - fecha + Reportes: pais - codigo - fecha
+ Generar Imagen: loc y codigo?
# iplocate # iplocate
@ -64,6 +63,9 @@ ej. alias `alias iploc='~/ruta/script/iplocate.py'`
iploc --sync - Sincroniza logs del servidor (bash script). iploc --sync - Sincroniza logs del servidor (bash script).
iploc -c - Carga logs en base de datos. iploc -c - Carga logs en base de datos.
iploc -g - Guarda ipinfo de IPs sin registro en la BD. 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`** **`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). Consulta a `ipinfo.io` por cada ip registrada en **visita** (una vez por ip).
Guarda los datos en tabla **registro**. 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 ### Otras opciones
**`iploc <IP>`** **`iploc <IP>`**

View File

@ -205,6 +205,8 @@ def main():
case '-t': case '-t':
ip = sys.argv[2] ip = sys.argv[2]
print_ipinfo(ip) print_ipinfo(ip)
case '-M':
sql_alch.mapsgen()
case _: case _:
ip = sys.argv[1] ip = sys.argv[1]
print_ipinfo(ip, False) print_ipinfo(ip, False)
@ -227,6 +229,7 @@ def main():
def uso(): def uso():
ayuda = f""" ayuda = f"""
[bold blue]ipLocate[/bold blue] [bold blue]ipLocate[/bold blue]
[deep_sky_blue1]Consulta información sobre IP(s) disponibles en ipinfo.io con o sin token. [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 Carga logs de nginx en base de datos. Consulta con ipinfo.io y registra
en base de datos. 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 -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 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) console.print(ayuda)

5110
maps/map_thumb.svg Normal file

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 514 KiB

89
mapsgen.py Normal file
View File

@ -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")

View File

@ -9,10 +9,11 @@ from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Sequence, update from sqlalchemy import Column, Integer, String, Sequence, update
from sqlalchemy.orm.session import Session 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 sqlalchemy.sql.schema import ForeignKey
from rich.progress import Progress, track from rich.progress import Progress, track
from rich.console import Console 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])$" 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') logs_dir = parser.get('bash_script', 'destino_log')
@ -224,7 +225,6 @@ def carga_error_logs(log):
nombre_log = log.split('/')[-1] nombre_log = log.split('/')[-1]
console.print(f'[yellow]Registrando [[/yellow]{nombre_log}[yellow]][/yellow]') console.print(f'[yellow]Registrando [[/yellow]{nombre_log}[yellow]][/yellow]')
try: try:
#with Progress(), open(log, 'r') as lista:
with open(log, 'r') as lista: with open(log, 'r') as lista:
try: try:
largo = subprocess.run(['wc', '-l', log], capture_output=True, text=True) largo = subprocess.run(['wc', '-l', log], capture_output=True, text=True)
@ -356,13 +356,11 @@ def carga_registro_ip(ip_info):
session.commit() session.commit()
except Exception as ex: except Exception as ex:
print('Exception: ', ex) print('Exception: ', ex)
# aqui progress bar registro visita
stmt = update(Visita).where(Visita.ip == ip_info['ip']).values(registro=1).\ stmt = update(Visita).where(Visita.ip == ip_info['ip']).values(registro=1).\
execution_options(synchronize_session="fetch") execution_options(synchronize_session="fetch")
#result = session.execute(stmt) #result = session.execute(stmt)
try: try:
session.execute(stmt) session.execute(stmt)
# aqui progress bar update visita (registro = 1)
session.commit() session.commit()
except Exception as ex: except Exception as ex:
print('Exception: ', ex) print('Exception: ', ex)
@ -414,3 +412,13 @@ def registro_ips():
progress.update(task1, advance=avance) progress.update(task1, advance=avance)
console.print('\n[bold yellow]Registro en base de datos finalizado.[/bold yellow]') 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)