prints distances wrongly
parent
b46dc83f41
commit
af5eef91bc
55
analyze.py
55
analyze.py
|
@ -1,11 +1,7 @@
|
|||
import sys
|
||||
import requests
|
||||
|
||||
def sorted_dict(d: dict, reverse=True) -> dict:
|
||||
return {
|
||||
key: d[key]
|
||||
for key in sorted(d, key=d.get, reverse=reverse)
|
||||
}
|
||||
from math import radians, cos, sin, acos, asin, sqrt
|
||||
from itertools import combinations
|
||||
|
||||
def main(filename: str):
|
||||
validnames = {
|
||||
|
@ -54,16 +50,10 @@ def main(filename: str):
|
|||
print(f'{hitfiles = }')
|
||||
requesters = sorted_dict(requesters)
|
||||
print(f'{requesters = }')
|
||||
print(f'{locations = }')
|
||||
|
||||
def get_ip_latlon(ip: str) -> (int, int):
|
||||
# make a reqest to ip-api.com to associate an ip to a
|
||||
# latitude and longitude
|
||||
addr = f'http://ip-api.com/json/{ip}'
|
||||
response = requests.get(addr)
|
||||
resulting_dict = eval(response.content)
|
||||
if resulting_dict['status'] != 'fail':
|
||||
return resulting_dict['lat'], resulting_dict['lon']
|
||||
for p1, p2 in combinations(locations.values(), 2):
|
||||
d = latlon_distance(p1, p2)
|
||||
if d != 0:
|
||||
print(d)
|
||||
|
||||
def filterwords(filename: str, validnames: set):
|
||||
with open(filename, 'r') as f:
|
||||
|
@ -79,5 +69,38 @@ def filterwords(filename: str, validnames: set):
|
|||
date, time, timezone = timethings.split(' ')
|
||||
yield ip, date, time, timezone, status, method, file
|
||||
|
||||
def get_ip_latlon(ip: str) -> (int, int):
|
||||
# make a reqest to ip-api.com to associate an ip to a
|
||||
# latitude and longitude
|
||||
addr = f'http://ip-api.com/json/{ip}'
|
||||
response = requests.get(addr)
|
||||
resulting_dict = eval(response.content)
|
||||
if resulting_dict['status'] != 'fail':
|
||||
return resulting_dict['lat'], resulting_dict['lon']
|
||||
|
||||
def sorted_dict(d: dict, reverse=True) -> dict:
|
||||
return {
|
||||
key: d[key]
|
||||
for key in sorted(d, key=d.get, reverse=reverse)
|
||||
}
|
||||
|
||||
def latlon_distance(p1, p2) -> float:
|
||||
# black magic do not touch. use the haversine formula to find the distance
|
||||
lat1, lon1 = p1
|
||||
lat2, lon2 = p2
|
||||
lon1 = radians(lon1)
|
||||
lat1 = radians(lat1)
|
||||
lon2 = radians(lon2)
|
||||
lat2 = radians(lat2)
|
||||
|
||||
dlon = lon2 - lon1
|
||||
dlat = lat2 - lat1
|
||||
a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
|
||||
|
||||
c = 2 * asin(sqrt(a))
|
||||
earth_radius_km = 6371
|
||||
|
||||
return c * earth_radius_km
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(sys.argv[1])
|
Loading…
Reference in New Issue