28 lines
1.1 KiB
Python
28 lines
1.1 KiB
Python
import json
|
|
import datetime
|
|
from models import Location
|
|
|
|
class GoogleTakeoutImporter:
|
|
def import_locations(self, json_filepath: str) -> list[Location]:
|
|
locations = []
|
|
try:
|
|
with open(json_filepath, 'r', encoding='utf-8') as f:
|
|
data = json.load(f)
|
|
for loc_data in data.get('locations', []):
|
|
timestamp_ms = int(loc_data['timestampMs'])
|
|
timestamp = datetime.datetime.fromtimestamp(timestamp_ms / 1000.0)
|
|
latitude = loc_data['latitudeE7'] / 1e7
|
|
longitude = loc_data['longitudeE7'] / 1e7
|
|
locations.append({
|
|
'latitude': latitude,
|
|
'longitude': longitude,
|
|
'timestamp': timestamp
|
|
})
|
|
except FileNotFoundError:
|
|
print(f"Error: File not found: {json_filepath}")
|
|
except json.JSONDecodeError:
|
|
print("Error: Invalid JSON format.")
|
|
except Exception as e:
|
|
print(f"An unexpected error occurred during import: {e}")
|
|
return locations
|