@@ -517,6 +517,34 @@ def downloadGeoLiteDb(self, db_path):
517517 - 100
518518 ])
519519
520+ def getLoc (self , geodb , ip ):
521+ global loc_cache
522+
523+ if ip in loc_cache :
524+ return loc_cache [ip ]
525+ else :
526+ try :
527+ loc_data = geodb .get (ip )
528+ except :
529+ loc_data = None
530+
531+ if not loc_data or "location" not in loc_data :
532+ loc_cache [ip ] = None
533+ return None
534+
535+ loc = {
536+ "lat" : loc_data ["location" ]["latitude" ],
537+ "lon" : loc_data ["location" ]["longitude" ],
538+ }
539+ if "city" in loc_data :
540+ loc ["city" ] = loc_data ["city" ]["names" ]["en" ]
541+
542+ if "country" in loc_data :
543+ loc ["country" ] = loc_data ["country" ]["names" ]["en" ]
544+
545+ loc_cache [ip ] = loc
546+ return loc
547+
520548 def getPeerLocations (self , peers ):
521549 import maxminddb
522550 db_path = config .data_dir + '/GeoLite2-City.mmdb'
@@ -535,28 +563,8 @@ def getPeerLocations(self, peers):
535563 ping = round (peer .connection .last_ping_delay * 1000 )
536564 else :
537565 ping = None
566+ loc = self .getLoc (geodb , peer .ip )
538567
539- # Query and cache location
540- if peer .ip in loc_cache :
541- loc = loc_cache [peer .ip ]
542- else :
543- try :
544- loc_data = geodb .get (peer .ip )
545- except :
546- loc_data = None
547- if not loc_data or "location" not in loc_data :
548- loc_cache [peer .ip ] = None
549- continue
550-
551- loc = {
552- "lat" : loc_data ["location" ]["latitude" ],
553- "lon" : loc_data ["location" ]["longitude" ],
554- }
555- if "city" in loc_data :
556- loc ["city" ] = loc_data ["city" ]["names" ]["en" ]
557- if "country" in loc_data :
558- loc ["country" ] = loc_data ["country" ]["names" ]["en" ]
559- loc_cache [peer .ip ] = loc
560568 if not loc :
561569 continue
562570 # Create position array
@@ -576,19 +584,10 @@ def getPeerLocations(self, peers):
576584 peer_locations .append (peer_location )
577585
578586 # Append myself
579- try :
580- loc_data = geodb .get (config .ip_external )
581- except :
582- loc_data = None
583- if loc_data and loc_data .get ("location" ):
584- peer_location = {
585- "lat" : loc_data ["location" ]["latitude" ],
586- "lon" : loc_data ["location" ]["longitude" ],
587- "country" : loc_data ["country" ]["names" ]["en" ],
588- "city" : loc_data ["city" ]["names" ]["en" ],
589- "ping" : 0
590- }
591- peer_locations .append (peer_location )
587+ my_loc = self .getLoc (geodb , config .ip_external )
588+ if my_loc :
589+ my_loc ["ping" ] = 0
590+ peer_locations .append (my_loc )
592591
593592 return peer_locations
594593
0 commit comments