updt: map_thumb + format

This commit is contained in:
devfzn 2023-12-26 02:11:15 -03:00
parent 60a4bce725
commit 691d045547
5 changed files with 112 additions and 99 deletions

View File

@ -9,6 +9,7 @@ console = Console()
conn = sqlite3.connect(f'{selfpath}/../ipinfo.db') conn = sqlite3.connect(f'{selfpath}/../ipinfo.db')
c = conn.cursor() c = conn.cursor()
# GeoLoc # GeoLoc
def get_geoloc(codigo): def get_geoloc(codigo):
if codigo == 200: if codigo == 200:
@ -41,6 +42,7 @@ def vistas_pais_detalle(pais, codigo=''):
resp = c.fetchall() resp = c.fetchall()
return resp return resp
def pt_visita_pais_detalle(pais, codigo=''): def pt_visita_pais_detalle(pais, codigo=''):
respuesta = vistas_pais_detalle(pais, codigo) respuesta = vistas_pais_detalle(pais, codigo)
if codigo != '': if codigo != '':
@ -49,8 +51,8 @@ def pt_visita_pais_detalle(pais, codigo=''):
titulo = f"[bold][blue]Detalle visitas pais: [/blue][yellow]{pais}[/yellow][/bold]" titulo = f"[bold][blue]Detalle visitas pais: [/blue][yellow]{pais}[/yellow][/bold]"
tbl_v = Table( tbl_v = Table(
title=titulo, title=titulo,
box = box.ROUNDED, box=box.ROUNDED,
show_lines = False, show_lines=False,
row_styles=["dim", ""], row_styles=["dim", ""],
border_style="dark_magenta") border_style="dark_magenta")
tbl_v.add_column("Fecha", justify="center", style="bright_yellow") tbl_v.add_column("Fecha", justify="center", style="bright_yellow")
@ -63,6 +65,7 @@ def pt_visita_pais_detalle(pais, codigo=''):
console.print(tbl_v) console.print(tbl_v)
# Formato fecha -- Convertir fecha 'unixepoch' a 'localtime' # Formato fecha -- Convertir fecha 'unixepoch' a 'localtime'
def unix_to_local_date(): def unix_to_local_date():
consulta = """ consulta = """
@ -71,6 +74,7 @@ def unix_to_local_date():
c.execute(consulta) c.execute(consulta)
return c.fetchall() return c.fetchall()
# Select geoloc by cod html -- SELECT all from registro where ip=(SELECT ip from visita where cod_html=200); # Select geoloc by cod html -- SELECT all from registro where ip=(SELECT ip from visita where cod_html=200);
def select_cod(codigo): def select_cod(codigo):
consulta = f""" consulta = f"""
@ -81,6 +85,7 @@ def select_cod(codigo):
resp = c.fetchall() resp = c.fetchall()
return resp return resp
# Select fecha mayor que (ej. 1661226920) # Select fecha mayor que (ej. 1661226920)
def sel_pais_desde(pais, unix_e): def sel_pais_desde(pais, unix_e):
consulta = f""" consulta = f"""
@ -91,14 +96,15 @@ def sel_pais_desde(pais, unix_e):
resp = c.fetchall() resp = c.fetchall()
return resp return resp
def pt_sel_pais_fecha(pais, fecha_ux, fecha_loc): def pt_sel_pais_fecha(pais, fecha_ux, fecha_loc):
fecha = fecha_loc.split('/') fecha = fecha_loc.split('/')
fecha = fecha[2] +'/'+ fecha[1] +'/'+ fecha[0] fecha = fecha[2] + '/' + fecha[1] + '/' + fecha[0]
respuesta = sel_pais_desde(pais, fecha_ux) respuesta = sel_pais_desde(pais, fecha_ux)
tbl_v = Table( tbl_v = Table(
title=f"[bold][blue]Visitas {pais}, desde {fecha}[/blue][/bold]", title=f"[bold][blue]Visitas {pais}, desde {fecha}[/blue][/bold]",
box = box.ROUNDED, box=box.ROUNDED,
show_lines = False, show_lines=False,
row_styles=["dim", ""], row_styles=["dim", ""],
border_style="dark_magenta") border_style="dark_magenta")
tbl_v.add_column("Fecha", justify="center", style="bright_yellow") tbl_v.add_column("Fecha", justify="center", style="bright_yellow")
@ -111,6 +117,7 @@ def pt_sel_pais_fecha(pais, fecha_ux, fecha_loc):
console.print(tbl_v) console.print(tbl_v)
# Top 50 paises # Top 50 paises
def top_paises(top): def top_paises(top):
consulta = f""" consulta = f"""
@ -131,8 +138,8 @@ def pt_top_paises(top):
respuesta = top_paises(top) respuesta = top_paises(top)
tbl_v = Table( tbl_v = Table(
title=f"[bold][blue]Vistas Top {top}[/blue][/bold]", title=f"[bold][blue]Vistas Top {top}[/blue][/bold]",
box = box.ROUNDED, box=box.ROUNDED,
show_lines = False, show_lines=False,
row_styles=["dim", ""], row_styles=["dim", ""],
border_style="dark_magenta") border_style="dark_magenta")
tbl_v.add_column("País", justify="center", style="bright_yellow") tbl_v.add_column("País", justify="center", style="bright_yellow")
@ -142,6 +149,7 @@ def pt_top_paises(top):
console.print(tbl_v) console.print(tbl_v)
# respuesta HTML para pais=? # respuesta HTML para pais=?
def cod_html_pais(pais): def cod_html_pais(pais):
consulta = f""" consulta = f"""
@ -161,8 +169,8 @@ def pt_html_cod_pais(pais):
respuesta = cod_html_pais(pais) respuesta = cod_html_pais(pais)
tbl_v = Table( tbl_v = Table(
title=f"[bold][blue]Códigos html: [/blue][green]{pais}[/bold][/green]", title=f"[bold][blue]Códigos html: [/blue][green]{pais}[/bold][/green]",
box = box.ROUNDED, box=box.ROUNDED,
show_lines = False, show_lines=False,
row_styles=["dim", ""], row_styles=["dim", ""],
border_style="dark_magenta") border_style="dark_magenta")
tbl_v.add_column("Código", justify="center", style="bright_yellow") tbl_v.add_column("Código", justify="center", style="bright_yellow")

View File

@ -17,36 +17,40 @@ import logging
selfpath = os.path.abspath(os.path.dirname(__file__)) selfpath = os.path.abspath(os.path.dirname(__file__))
parser = cfg.ConfigParser() parser = cfg.ConfigParser()
parser.read(f'{selfpath}/config.cfg') parser.read(f'{selfpath}/config.cfg')
token = parser.get('iplocate','token') token = parser.get('iplocate', 'token')
token = token.strip("'") token = token.strip("'")
muevelog = f'{selfpath}/muevelog.sh ' muevelog = f'{selfpath}/muevelog.sh '
log_file = f'{selfpath}/log/iplocate.log' log_file = f'{selfpath}/log/iplocate.log'
console = Console() console = Console()
#tkn=True # tkn=True
# IP validate https://stackoverflow.com/questions/319279/how-to-validate-ip-address-in-python # stackoverflow.com/questions/319279/how-to-validate-ip-address-in-python
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])$"
ip_local_regx = "^192\.168\.0\.([0-9]|[0-9][0-9]|[0-9][0-9][0-9])$" ip_local_regx = "^192\.168\.0\.([0-9]|[0-9][0-9]|[0-9][0-9][0-9])$"
logging.basicConfig( logging.basicConfig(
filename = log_file, filename=log_file,
format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level = logging.INFO level=logging.INFO
) )
logging.info("iplocate inicio") logging.info("iplocate inicio")
def log_usage(proceso, info): def log_usage(proceso, info):
usage_log = f"Proceso:[{proceso}] - Info: [{info}]" usage_log = f"Proceso:[{proceso}] - Info: [{info}]"
logging.info(usage_log) logging.info(usage_log)
def log_error(proceso, info): def log_error(proceso, info):
error_log = f"Proceso: [{proceso}], Info:[{info}]" error_log = f"Proceso: [{proceso}], Info:[{info}]"
logging.error(error_log) logging.error(error_log)
try: try:
ownip = requests.get('https://ifconfig.me/').text ownip = requests.get('https://ifconfig.me/').text
except Exception as ex: except Exception as ex:
log_error("Request ip pública", ex) log_error("Request ip pública", ex)
def filtro_ip_propia(ip): def filtro_ip_propia(ip):
return True if ip != ownip and not re.search(ip_local_regx, ip) else False return True if ip != ownip and not re.search(ip_local_regx, ip) else False
@ -56,13 +60,13 @@ def print_ipinfo(ip, tkn=True):
try: try:
ip_info = sql_alch.consulta_ip(ip, tkn) ip_info = sql_alch.consulta_ip(ip, tkn)
except Exception as ex: except Exception as ex:
print(f'Exception sql_alch.consulta_ip({ip})\n',ex) print(f'Exception sql_alch.consulta_ip({ip})\n', ex)
ip_info = None ip_info = None
if isinstance(ip_info, dict): if isinstance(ip_info, dict):
print_tabla_ip(ip_info) print_tabla_ip(ip_info)
elif isinstance(ip_info, list): elif isinstance(ip_info, list):
lista_visitas=[] lista_visitas = []
contad=0 contad = 0
for tupla in ip_info: for tupla in ip_info:
visita = [] visita = []
if contad < 1: if contad < 1:
@ -73,32 +77,32 @@ def print_ipinfo(ip, tkn=True):
ip_dict[dato.split("=")[0]] = dato.split("=")[1] ip_dict[dato.split("=")[0]] = dato.split("=")[1]
print_tabla_ip(ip_dict) print_tabla_ip(ip_dict)
else: else:
visita.append(str(val).split(',')[3].split('=')[1]) #.ljust(24)) visita.append(str(val).split(',')[3].split('=')[1]) # .ljust(24))
visita.append(str(val).split(',')[2].split('=')[1]) #.center(11)) visita.append(str(val).split(',')[2].split('=')[1]) # .center(11))
metodo = (str(val).split(',')[4].split('=')[1]) metodo = (str(val).split(',')[4].split('=')[1])
metodo = '---' if metodo == 'None' else metodo #.center(10) metodo = '---' if metodo == 'None' else metodo # .center(10)
visita.append(metodo) visita.append(metodo)
request = ''.join(str(val).split(',')[5].split('=')[1:]) request = ''.join(str(val).split(',')[5].split('=')[1:])
# configurar wrap en tabla # configurar wrap en tabla
#request = request[:86]+'...' if len(request) > 90 else request # request = request[:86]+'...' if len(request) > 90 else request
request = '---' if request == 'None' else request request = '---' if request == 'None' else request
visita.append(request) visita.append(request)
else: else:
for ind, val in enumerate(tupla): for ind, val in enumerate(tupla):
if ind > 0: if ind > 0:
# aqui modificar para representar las nuevas columnas de tabla visita # aqui modificar para representar las nuevas columnas de tabla visita
visita.append(str(val).split(',')[3].split('=')[1]) #.ljust(24) visita.append(str(val).split(',')[3].split('=')[1]) #.ljust(24)
visita.append(str(val).split(',')[2].split('=')[1]) #.center(11) visita.append(str(val).split(',')[2].split('=')[1]) #.center(11)
metodo = (str(val).split(',')[4].split('=')[1]) metodo = (str(val).split(',')[4].split('=')[1])
metodo = '---' if metodo == 'None' else metodo #.center(10) metodo = '---' if metodo == 'None' else metodo #.center(10)
visita.append(metodo) visita.append(metodo)
request = ''.join(str(val).split(',')[5].split('=')[1:]) request = ''.join(str(val).split(',')[5].split('=')[1:])
# configurar wrap en tabla # configurar wrap en tabla
#request = request[:86]+'...' if len(request) > 90 else request # request = request[:86]+'...' if len(request) > 90 else request
request = '---' if request == 'None' else request request = '---' if request == 'None' else request
visita.append(request) visita.append(request)
lista_visitas.append(visita) lista_visitas.append(visita)
contad+=1 contad += 1
print_tabla_visita(lista_visitas) print_tabla_visita(lista_visitas)
else: else:
console.print(f'[red]Error type(ip_info) = [/red][magenta]{type(ip_info)}[/magenta][red]][/red]') console.print(f'[red]Error type(ip_info) = [/red][magenta]{type(ip_info)}[/magenta][red]][/red]')
@ -108,7 +112,7 @@ def print_ipinfo(ip, tkn=True):
def print_tabla_ip(ip_info): def print_tabla_ip(ip_info):
tbl_ip = Table(box = box.ROUNDED, highlight=True, border_style="dim plum1") tbl_ip = Table(box=box.ROUNDED, highlight=True, border_style="dim plum1")
tbl_ip.add_column("IP", justify="left", style="bold #005fff", no_wrap=True) tbl_ip.add_column("IP", justify="left", style="bold #005fff", no_wrap=True)
tbl_ip.add_column(f"{ip_info['ip']}", justify="left", style="#00ff5f") tbl_ip.add_column(f"{ip_info['ip']}", justify="left", style="#00ff5f")
try: try:
@ -154,8 +158,9 @@ def print_tabla_ip(ip_info):
except Exception as ex: except Exception as ex:
print('Exception print(tabla_ip): ', ex) print('Exception print(tabla_ip): ', ex)
def print_tabla_visita(lista_visitas): def print_tabla_visita(lista_visitas):
tbl_v = Table(box = box.ROUNDED, show_lines = False,row_styles=["dim", ""], border_style="dark_orange3") tbl_v = Table(box=box.ROUNDED, show_lines=False, row_styles=["dim", ""], border_style="dark_orange3")
tbl_v.add_column("Fecha visita", justify="center", style="bright_yellow", no_wrap=True) tbl_v.add_column("Fecha visita", justify="center", style="bright_yellow", no_wrap=True)
tbl_v.add_column("Codigo", justify="center", style="bold dodger_blue2") tbl_v.add_column("Codigo", justify="center", style="bold dodger_blue2")
tbl_v.add_column("Metodo", justify="center", style="bright_magenta") tbl_v.add_column("Metodo", justify="center", style="bright_magenta")
@ -168,18 +173,18 @@ def print_tabla_visita(lista_visitas):
def archivo_ips(ips, tkn=True): def archivo_ips(ips, tkn=True):
with open(ips, 'r') as lista: with open(ips, 'r') as lista:
for linea in lista: for linea in lista:
if '\n' in linea: if '\n' in linea:
linea = linea.split('\n')[0] linea = linea.split('\n')[0]
print_ipinfo(linea, tkn) print_ipinfo(linea, tkn)
sys.exit(0) sys.exit(0)
def _sync(): def _sync():
console.print('[bold yellow]Sincronizando logs del servidor(bash script)[/bold yellow]') console.print('[bold yellow]Sincronizando logs del servidor(bash script)[/bold yellow]')
subprocess.check_call(muevelog+"%s" % "--start", shell=True) subprocess.check_call(muevelog+"%s" % "--start", shell=True)
def main(): def main():
if len(sys.argv) > 1: if len(sys.argv) > 1:
try: try:
@ -262,7 +267,7 @@ def main():
else: else:
querys.pt_visita_pais_detalle(pais.upper()) querys.pt_visita_pais_detalle(pais.upper())
case _: case _:
console.print(f'[red] query desconocida [bold]{sys.argv[2]}[/bold][/red]') console.print(f'[red] query desconocida [bold]{sys.argv[2]}[/bold][/red]')
case _: case _:
ip = sys.argv[1] ip = sys.argv[1]
print_ipinfo(ip, False) print_ipinfo(ip, False)
@ -299,6 +304,7 @@ def uso_consultas():
""" """
console.print(ayuda) console.print(ayuda)
def uso(): def uso():
ayuda = f""" ayuda = f"""
[bold blue]ipLocate[/bold blue] [bold blue]ipLocate[/bold blue]
@ -331,5 +337,6 @@ def uso():
""" """
console.print(ayuda) console.print(ayuda)
if __name__ == "__main__": if __name__ == "__main__":
main() main()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 276 KiB

After

Width:  |  Height:  |  Size: 276 KiB

View File

@ -1,8 +1,8 @@
import staticmaps import staticmaps
from iplocate import selfpath from iplocate import selfpath
#context.set_tile_provider(staticmaps.tile_provider_ArcGISWorldImagery) # context.set_tile_provider(staticmaps.tile_provider_ArcGISWorldImagery)
#context.set_tile_provider(staticmaps.tile_provider_CartoDarkNoLabels) # context.set_tile_provider(staticmaps.tile_provider_CartoDarkNoLabels)
cntxt200 = staticmaps.Context() cntxt200 = staticmaps.Context()
cntxt200.set_tile_provider(staticmaps.tile_provider_OSM) cntxt200.set_tile_provider(staticmaps.tile_provider_OSM)
cntxt300 = staticmaps.Context() cntxt300 = staticmaps.Context()
@ -10,15 +10,16 @@ cntxt300.set_tile_provider(staticmaps.tile_provider_OSM)
cntxtAll = staticmaps.Context() cntxtAll = staticmaps.Context()
cntxtAll.set_tile_provider(staticmaps.tile_provider_OSM) cntxtAll.set_tile_provider(staticmaps.tile_provider_OSM)
def map200(geolocs): def map200(geolocs):
for loc in geolocs: for loc in geolocs:
lat = float(loc[0].split(',')[0]) lat = float(loc[0].split(',')[0])
lon = float(loc[0].split(',')[1]) lon = float(loc[0].split(',')[1])
marca200 = staticmaps.create_latlng(lat,lon) marca200 = staticmaps.create_latlng(lat,lon)
cntxt200.add_object(staticmaps.Marker(marca200 ,color=staticmaps.parse_color('#00ff29'), size=7)) cntxt200.add_object(staticmaps.Marker(marca200, color=staticmaps.parse_color('#00ff29'), size=7))
svg_image = cntxt200.render_svg(1920, 1080) svg_image = cntxt200.render_svg(1920, 1080)
with open(f"{selfpath}/maps/map_200.svg", "w", encoding ="utf-8") as f: with open(f"{selfpath}/maps/map_200.svg", "w", encoding="utf-8") as f:
svg_image.write(f, pretty=True) svg_image.write(f, pretty=True)
image = cntxt200.render_cairo(1920, 1080) image = cntxt200.render_cairo(1920, 1080)
@ -30,10 +31,10 @@ def map300(geolocs):
lat = float(loc[0].split(',')[0]) lat = float(loc[0].split(',')[0])
lon = float(loc[0].split(',')[1]) lon = float(loc[0].split(',')[1])
marca300 = staticmaps.create_latlng(lat,lon) marca300 = staticmaps.create_latlng(lat,lon)
cntxt300.add_object(staticmaps.Marker(marca300 ,color=staticmaps.parse_color('#b20101'), size=5)) cntxt300.add_object(staticmaps.Marker(marca300, color=staticmaps.parse_color('#b20101'), size=5))
svg_image = cntxt300.render_svg(1920, 1080) svg_image = cntxt300.render_svg(1920, 1080)
with open(f"{selfpath}/maps/map_300.svg", "w", encoding ="utf-8") as f: with open(f"{selfpath}/maps/map_300.svg", "w", encoding="utf-8") as f:
svg_image.write(f, pretty=True) svg_image.write(f, pretty=True)
image = cntxt300.render_cairo(1920, 1080) image = cntxt300.render_cairo(1920, 1080)
@ -47,7 +48,7 @@ def maps_gen(locs_200, locs_300):
for loc in locs_300: for loc in locs_300:
lat = float(loc[0].split(',')[0]) lat = float(loc[0].split(',')[0])
lon = float(loc[0].split(',')[1]) lon = float(loc[0].split(',')[1])
marca3 = staticmaps.create_latlng(lat,lon) marca3 = staticmaps.create_latlng(lat, lon)
cntxtAll.add_object(staticmaps.Marker(marca3, color=staticmaps.parse_color('#b20101'), size=5)) cntxtAll.add_object(staticmaps.Marker(marca3, color=staticmaps.parse_color('#b20101'), size=5))
for loc in locs_200: for loc in locs_200:
@ -57,7 +58,7 @@ def maps_gen(locs_200, locs_300):
cntxtAll.add_object(staticmaps.Marker(marca3, color=staticmaps.parse_color('#00ff29'), size=6)) cntxtAll.add_object(staticmaps.Marker(marca3, color=staticmaps.parse_color('#00ff29'), size=6))
svg_image = cntxtAll.render_svg(1920, 1080) svg_image = cntxtAll.render_svg(1920, 1080)
with open(f"{selfpath}/maps/map_all.svg", "w", encoding ="utf-8") as f: with open(f"{selfpath}/maps/map_all.svg", "w", encoding="utf-8") as f:
svg_image.write(f, pretty=True) svg_image.write(f, pretty=True)
image = cntxtAll.render_cairo(1920, 1080) image = cntxtAll.render_cairo(1920, 1080)
@ -72,7 +73,7 @@ def maps_thumbs(locs_200, locs_300):
for loc in locs_300: for loc in locs_300:
lat = float(loc[0].split(',')[0]) lat = float(loc[0].split(',')[0])
lon = float(loc[0].split(',')[1]) lon = float(loc[0].split(',')[1])
demo300 = staticmaps.create_latlng(lat,lon) demo300 = staticmaps.create_latlng(lat, lon)
cntxtdemo.add_object(staticmaps.Marker(demo300, color=staticmaps.parse_color('#b20101'), size=4)) cntxtdemo.add_object(staticmaps.Marker(demo300, color=staticmaps.parse_color('#b20101'), size=4))
for loc in locs_200: for loc in locs_200:
@ -82,9 +83,8 @@ def maps_thumbs(locs_200, locs_300):
cntxtdemo.add_object(staticmaps.Marker(demo200, color=staticmaps.parse_color('#00ff29'), size=5)) cntxtdemo.add_object(staticmaps.Marker(demo200, color=staticmaps.parse_color('#00ff29'), size=5))
svg_thumb = cntxtdemo.render_svg(1024, 768) svg_thumb = cntxtdemo.render_svg(1024, 768)
with open(f"{selfpath}/maps/map_thumb.svg", "w", encoding ="utf-8") as f: with open(f"{selfpath}/maps/map_thumb.svg", "w", encoding="utf-8") as f:
svg_thumb.write(f, pretty=True) svg_thumb.write(f, pretty=True)
image = cntxtdemo.render_cairo(1024, 768) image = cntxtdemo.render_cairo(1024, 768)
image.write_to_png(f"{selfpath}/maps/map_thumb.png") image.write_to_png(f"{selfpath}/maps/map_thumb.png")

View File

@ -24,6 +24,7 @@ base_de_datos = f'sqlite:////{selfpath}/ipinfo.db'
console = Console() console = Console()
Base = declarative_base() Base = declarative_base()
# Tabla registro ip info # Tabla registro ip info
class Registro(Base): class Registro(Base):
"""Definición de tabla 'Registro'""" """Definición de tabla 'Registro'"""
@ -50,15 +51,14 @@ class Registro(Base):
"""Convierte fecha 'unix epoch' y devuelve en formato local""" """Convierte fecha 'unix epoch' y devuelve en formato local"""
return time.asctime(time.localtime(int(self.fecha_reg.__repr__()))) return time.asctime(time.localtime(int(self.fecha_reg.__repr__())))
def __repr__(self) -> str: def __repr__(self) -> str:
#print('en repr') # print('en repr')
try: try:
rep = f'ip={self.ip};host={self.hostname};anycast={self.anycast};'+\ rep = f'ip={self.ip};host={self.hostname};anycast={self.anycast};'+\
f'cuidad={self.cuidad};region={self.region};pais={self.pais};'+\ f'cuidad={self.cuidad};region={self.region};pais={self.pais};'+\
f'geoloc={self.geoloc};organizacion={self.organizacion};'+\ f'geoloc={self.geoloc};organizacion={self.organizacion};'+\
f'fecha_reg={self.get_fecha()};tzone={self.tzone};cod_post={self.cod_post}' f'fecha_reg={self.get_fecha()};tzone={self.tzone};cod_post={self.cod_post}'
#print('Repr:', rep) # print('Repr:', rep)
return rep return rep
except Exception as ex: except Exception as ex:
print('Exception :', ex) print('Exception :', ex)
@ -67,7 +67,6 @@ class Registro(Base):
class Visita(Base): class Visita(Base):
"""Definición de tabla 'Visita'""" """Definición de tabla 'Visita'"""
__tablename__ = 'visita' __tablename__ = 'visita'
id = Column(Integer, Sequence('visita_id_seq'), primary_key=True) id = Column(Integer, Sequence('visita_id_seq'), primary_key=True)
ip = Column(String, ForeignKey('registro.ip')) ip = Column(String, ForeignKey('registro.ip'))
@ -76,8 +75,7 @@ class Visita(Base):
metodo = Column(String, default='---') metodo = Column(String, default='---')
consulta = Column(String, default='---') consulta = Column(String, default='---')
registro = Column(Integer, default=0) registro = Column(Integer, default=0)
visita_ip = relationship("Registro", back_populates="visitas") visita_ip = relationship("Registro", back_populates="visitas")
def get_fecha(self): def get_fecha(self):
"""Convierte fecha 'unix epoch' y devuelve en formato local""" """Convierte fecha 'unix epoch' y devuelve en formato local"""
@ -99,7 +97,7 @@ class Visita(Base):
engine = create_engine(base_de_datos) engine = create_engine(base_de_datos)
Base.metadata.create_all(engine) Base.metadata.create_all(engine)
#Base.metadata.create_all(engine.execution_options(synchronize_session="fetch")) # Base.metadata.create_all(engine.execution_options(synchronize_session="fetch"))
Session = sessionmaker(bind=engine) Session = sessionmaker(bind=engine)
session = Session() session = Session()
@ -112,10 +110,10 @@ fecha_error = "2022/05/10 07:11:46"
fecha_access = "10/May/2022:11:42:14 -0400".split(' ')[0] fecha_access = "10/May/2022:11:42:14 -0400".split(' ')[0]
""" """
def fecha_access_to_epoch(fecha): def fecha_access_to_epoch(fecha):
"""Convierte la fecha del formato entregado por access.log """Convierte la fecha del formato entregado por access.log
y reverse_access.log(nginx) al formato unix epoch. y reverse_access.log(nginx) al formato unix epoch.
:fecha: str Fecha :fecha: str Fecha
:returns: int unix epoch fecha (secs) :returns: int unix epoch fecha (secs)
""" """
@ -123,10 +121,10 @@ def fecha_access_to_epoch(fecha):
fecha_unix = int(time.mktime(fecha.timetuple())) fecha_unix = int(time.mktime(fecha.timetuple()))
return fecha_unix return fecha_unix
def fecha_error_to_epoch(fecha): def fecha_error_to_epoch(fecha):
"""Convierte la fecha del formato entregado por error.log """Convierte la fecha del formato entregado por error.log
y reverse_error.log (nginx) al formato unix epoch. y reverse_error.log (nginx) al formato unix epoch.
:fecha_local: str Fecha :fecha_local: str Fecha
:returns: int unix epoch fecha (secs) :returns: int unix epoch fecha (secs)
""" """
@ -134,9 +132,9 @@ def fecha_error_to_epoch(fecha):
fecha_unix = int(time.mktime(fecha.timetuple())) fecha_unix = int(time.mktime(fecha.timetuple()))
return fecha_unix return fecha_unix
def epoch_to_local(fecha): def epoch_to_local(fecha):
"""Convierte fecha unix epoch a localtime """Convierte fecha unix epoch a localtime
:fecha: int Fecha (secs) :fecha: int Fecha (secs)
:returns: str Fecha :returns: str Fecha
""" """
@ -148,7 +146,7 @@ def ip_registrada(ip):
en tabla 'Visita' para ip pasada como argumento. en tabla 'Visita' para ip pasada como argumento.
""" """
try: try:
ip_reg = session.query(Visita).filter(Visita.ip==ip).filter(Visita.registro==1).first() ip_reg = session.query(Visita).filter(Visita.ip == ip).filter(Visita.registro == 1).first()
except Exception as ex: except Exception as ex:
print('Exception', ex) print('Exception', ex)
ip_reg = None ip_reg = None
@ -219,7 +217,7 @@ def carga_access_log(log):
print('Exception: ', ex) print('Exception: ', ex)
try: try:
with Progress() as prog, session: with Progress() as prog, session:
task1=prog.add_task("[yellow bold]Guardando[/yellow bold]", total=len(session.new)) task1 = prog.add_task("[yellow bold]Guardando[/yellow bold]", total=len(session.new))
session.commit() session.commit()
while not prog.finished: while not prog.finished:
prog.update(task1, advance=0.1) prog.update(task1, advance=0.1)
@ -296,9 +294,9 @@ def carga_error_logs(log):
url = url[:252]+'...' url = url[:252]+'...'
except Exception: except Exception:
url = '---' url = '---'
if ip != None: if ip is not None:
if filtro_ip_propia(ip): if filtro_ip_propia(ip):
fecha = int(fecha_error_to_epoch(fecha)) fecha = int(fecha_error_to_epoch(fecha))
codigo = 0 codigo = 0
if ip_registrada(ip): if ip_registrada(ip):
session.add(Visita(ip=ip, session.add(Visita(ip=ip,
@ -324,7 +322,7 @@ def carga_error_logs(log):
pass pass
try: try:
with Progress() as prog, session: with Progress() as prog, session:
task1=prog.add_task("[yellow bold]Guardando[/yellow bold]", total=len(session.new)) task1 = prog.add_task("[yellow bold]Guardando[/yellow bold]", total=len(session.new))
session.commit() session.commit()
while not prog.finished: while not prog.finished:
prog.update(task1, advance=0.1) prog.update(task1, advance=0.1)
@ -368,7 +366,7 @@ def carga_registro_ip(ip_info):
if not ip_registrada(ip_info['ip']): if not ip_registrada(ip_info['ip']):
info_dic = {} info_dic = {}
info_dic['ip'] = ip_info['ip'] info_dic['ip'] = ip_info['ip']
info_dic['hostname'] = ip_info['hostname'] if 'hostname' in ip_info else None info_dic['hostname'] = ip_info['hostname'] if 'hostname' in ip_info else None
info_dic['anycast'] = ip_info['anycast'] if 'anycast' in ip_info else None info_dic['anycast'] = ip_info['anycast'] if 'anycast' in ip_info else None
info_dic['ciudad'] = ip_info['city'] if 'city' in ip_info else None info_dic['ciudad'] = ip_info['city'] if 'city' in ip_info else None
info_dic['region'] = ip_info['region'] if 'region' in ip_info else None info_dic['region'] = ip_info['region'] if 'region' in ip_info else None
@ -378,25 +376,25 @@ def carga_registro_ip(ip_info):
info_dic['tzone'] = ip_info['timezone'] if 'timezone' in ip_info else None info_dic['tzone'] = ip_info['timezone'] if 'timezone' in ip_info else None
info_dic['cod_post'] = ip_info['postal'] if 'postal' in ip_info else None info_dic['cod_post'] = ip_info['postal'] if 'postal' in ip_info else None
try: try:
session.add(Registro( ip = info_dic['ip'], session.add(Registro(ip=info_dic['ip'],
hostname = info_dic['hostname'], hostname=info_dic['hostname'],
anycast = info_dic['anycast'], anycast=info_dic['anycast'],
cuidad = info_dic['ciudad'], cuidad=info_dic['ciudad'],
region = info_dic['region'], region=info_dic['region'],
pais = info_dic['pais'], pais=info_dic['pais'],
geoloc = info_dic['geoloc'], geoloc=info_dic['geoloc'],
organizacion = info_dic['organizacion'], organizacion=info_dic['organizacion'],
fecha_reg = int(time.mktime(time.localtime())), fecha_reg=int(time.mktime(time.localtime())),
tzone = info_dic['tzone'], tzone=info_dic['tzone'],
cod_post = info_dic['cod_post'], cod_post=info_dic['cod_post'],
)) ))
session.commit() session.commit()
except Exception as ex: except Exception as ex:
print('[session.commit(ADD REGISTRO)] Exception: ', ex) print('[session.commit(ADD REGISTRO)] Exception: ', ex)
print('Datos-Dic: ', info_dic) print('Datos-Dic: ', info_dic)
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)
session.commit() session.commit()
@ -420,7 +418,8 @@ def consulta_ip(ip_consulta, tkn=True):
return loads(info_ip) return loads(info_ip)
case None: case None:
resp = consulta_db(ip_consulta) resp = consulta_db(ip_consulta)
return resp return resp
def consulta_db(ip): def consulta_db(ip):
"""Consulta base de datos por la IPs pasada como argumento. """Consulta base de datos por la IPs pasada como argumento.
@ -435,7 +434,6 @@ def consulta_db(ip):
print('Exception consulta_db:\n', ex) print('Exception consulta_db:\n', ex)
def registro_ips(): def registro_ips():
"""Consulta API, obtiene datos de IPs en tabla 'Visita' """Consulta API, obtiene datos de IPs en tabla 'Visita'
cuya valor en columna 'registro' sea '0'. Utiliza clase cuya valor en columna 'registro' sea '0'. Utiliza clase
@ -444,18 +442,18 @@ def registro_ips():
statement = select(Visita).filter_by(registro=0) statement = select(Visita).filter_by(registro=0)
with Progress() as progress: with Progress() as progress:
total = len(session.execute(statement).scalars().all()) total = len(session.execute(statement).scalars().all())
task1= progress.add_task("[bold blue]Cargando [/bold blue]", total=total) task1 = progress.add_task("[bold blue]Cargando [/bold blue]", total=total)
total_ant = total total_ant = total
while not progress.finished: while not progress.finished:
res = session.execute(statement).scalars().first() res = session.execute(statement).scalars().first()
total_act = len(session.execute(statement).scalars().all()) total_act = len(session.execute(statement).scalars().all())
avance = total_ant - total_act avance = total_ant - total_act
#print('total update:',total,'total_act:', total_act,' Diferencia: ', avance ) # print('total update:',total,'total_act:', total_act,' Diferencia: ', avance )
total_ant = total_act total_ant = total_act
if res is None: if res is None:
progress.update (task1, advance=avance) progress.update(task1, advance=avance)
else: else:
ip_actual= res.ip ip_actual = res.ip
ip_info = consulta_ip(ip_actual, True) ip_info = consulta_ip(ip_actual, True)
carga_registro_ip(ip_info) carga_registro_ip(ip_info)
progress.update(task1, advance=avance) progress.update(task1, advance=avance)