type hints
This commit is contained in:
parent
4e6ff22dba
commit
037ce01efd
25
kglobe.py
25
kglobe.py
@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
from kitty import draw_to_terminal, get_position, set_position, hide_cursor, show_cursor
|
import kitty
|
||||||
|
|
||||||
import pyvista as pv
|
import pyvista as pv
|
||||||
import numpy as np
|
import numpy as np
|
||||||
@ -17,7 +17,7 @@ from io import BytesIO
|
|||||||
|
|
||||||
|
|
||||||
# Convert lat/lon to Cartesian coordinates
|
# Convert lat/lon to Cartesian coordinates
|
||||||
def latlon_to_xyz(lat, lon, radius=1.0):
|
def latlon_to_xyz(lat: float, lon: float, radius=1.0):
|
||||||
lat_rad = np.radians(lat)
|
lat_rad = np.radians(lat)
|
||||||
lon_rad = np.radians(lon)
|
lon_rad = np.radians(lon)
|
||||||
x = radius * np.cos(lat_rad) * np.cos(lon_rad)
|
x = radius * np.cos(lat_rad) * np.cos(lon_rad)
|
||||||
@ -53,19 +53,19 @@ def generate_arch(p1, p2, height_factor=0.2, n_points=100):
|
|||||||
return arch_points
|
return arch_points
|
||||||
|
|
||||||
|
|
||||||
def traceroute(target):
|
def traceroute(target: str) -> list[tuple[int, int]]:
|
||||||
# Run traceroute command
|
# Run traceroute command
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
["traceroute", "-n", target, "-q", "1", "-w", "1,3,10"],
|
["traceroute", "-n", target, "-q", "1", "-w", "1,3,10"],
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
text=True,
|
text=True,
|
||||||
)
|
)
|
||||||
hops = re.findall(r"\n\s*\d+\s+([\d.]+)", result.stdout)
|
hops: list[str] = re.findall(r"\n\s*\d+\s+([\d.]+)", result.stdout)
|
||||||
|
|
||||||
coords = []
|
coords: list[tuple[int,int]] = []
|
||||||
for ip in hops:
|
for ip in hops:
|
||||||
try:
|
try:
|
||||||
response = requests.get(f"http://ip-api.com/json/{ip}").json()
|
response: dict = requests.get(f"http://ip-api.com/json/{ip}").json()
|
||||||
if response["status"] == "success":
|
if response["status"] == "success":
|
||||||
coords.append((response["lat"], response["lon"]))
|
coords.append((response["lat"], response["lon"]))
|
||||||
except Exception:
|
except Exception:
|
||||||
@ -110,7 +110,7 @@ def main():
|
|||||||
# Convert to 3D coordinates
|
# Convert to 3D coordinates
|
||||||
points_3d = [latlon_to_xyz(lat, lon) for lat, lon in locations]
|
points_3d = [latlon_to_xyz(lat, lon) for lat, lon in locations]
|
||||||
|
|
||||||
pl = pv.Plotter(off_screen=(not args.external))
|
pl : pv.Plotter = pv.Plotter(off_screen=(not args.external))
|
||||||
pl.add_mesh(globe, color="tan", smooth_shading=True, texture=tex, show_edges=False)
|
pl.add_mesh(globe, color="tan", smooth_shading=True, texture=tex, show_edges=False)
|
||||||
|
|
||||||
for pt in points_3d:
|
for pt in points_3d:
|
||||||
@ -121,23 +121,22 @@ def main():
|
|||||||
line = pv.lines_from_points(arch, close=False)
|
line = pv.lines_from_points(arch, close=False)
|
||||||
pl.add_mesh(line, color="red", line_width=2)
|
pl.add_mesh(line, color="red", line_width=2)
|
||||||
|
|
||||||
hide_cursor()
|
kitty.hide_cursor()
|
||||||
y, x = get_position()
|
y, x = kitty.get_position()
|
||||||
print("\n" * 25, end="")
|
print("\n" * 25, end="")
|
||||||
|
|
||||||
frames = []
|
|
||||||
try:
|
try:
|
||||||
if not args.external:
|
if not args.external:
|
||||||
while True:
|
while True:
|
||||||
pl.camera.Azimuth(1)
|
pl.camera.Azimuth(1)
|
||||||
buf: BytesIO = BytesIO()
|
buf: BytesIO = BytesIO()
|
||||||
pl.screenshot(buf, transparent_background=True, window_size=(512, 512))
|
pl.screenshot(buf, transparent_background=True, window_size=(512, 512))
|
||||||
set_position(y - 25, x)
|
kitty.set_position(y - 25, x)
|
||||||
draw_to_terminal(buf)
|
kitty.draw_to_terminal(buf)
|
||||||
else:
|
else:
|
||||||
pl.show()
|
pl.show()
|
||||||
finally:
|
finally:
|
||||||
show_cursor()
|
kitty.show_cursor()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
Reference in New Issue
Block a user