DeaD_EyE

Standortverlauf

Aug 18th, 2020
1,941
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import datetime
  2. import json
  3.  
  4.  
  5. def read_json(file):
  6.     with open(file) as fd:
  7.         return json.load(fd)["locations"]
  8.  
  9.  
  10. def convert_timestamps(locations):
  11.     for loc in locations:
  12.         convert_timestamp(loc)
  13.  
  14.  
  15. def convert_timestamp(location):
  16.     from_ts = datetime.datetime.fromtimestamp
  17.     location["timestamp"] = from_ts(int(location["timestampMs"]) / 1000).astimezone(datetime.timezone.utc)
  18.     del location["timestampMs"]
  19.  
  20.  
  21. def convert_locations(locations):
  22.     for loc in locations:
  23.         convert_location(loc)
  24.  
  25.  
  26. def convert_location(location):
  27.     lat, long = int(location['latitudeE7']), int(location['longitudeE7'])
  28.     # 32 bit overflow bugfix
  29.     if lat > 900000000:
  30.         lat -= 4294967296
  31.     if long > 1800000000:
  32.         long -= 4294967296
  33.     lat /= 1e7
  34.     long /= 1e7
  35.     del location['latitudeE7'], location['longitudeE7']
  36.     location['lat'] = lat
  37.     location['long'] = long
  38.    
  39.  
  40. def by_datetime(locations, start=None, end=None):
  41.     if start:
  42.         start = start.astimezone(datetime.timezone.utc)
  43.     if end:
  44.         end = end.astimezone(datetime.timezone.utc)
  45.     if start and end:
  46.         condition = lambda ts: start <= ts <= end
  47.     elif start and end is None:
  48.         condition = lambda ts: start <= ts
  49.     elif start is None and end:
  50.         condition = lambda tx: ts <= end
  51.     else:
  52.         raise TypeError("start and/or end must be given")
  53.     for loc in locations:
  54.         if condition(loc["timestamp"]):
  55.             yield loc
  56.  
  57.  
  58. def area(locations):
  59.     iterator = iter(locations)
  60.     first = next(iterator)
  61.     lat, long = first["lat"], first["long"]
  62.     min_lat = lat
  63.     max_lat = lat
  64.     min_long = long
  65.     max_long = long
  66.     for loc in iterator:
  67.         lat, long = loc["lat"], loc["long"]
  68.         min_lat = min(min_lat, lat)
  69.         max_lat = max(max_lat, lat)
  70.         min_long = min(min_long, long)
  71.         max_long = max(max_long, long)
  72.     return {
  73.         "min_lat": min_lat,
  74.         "max_lat": max_lat,
  75.         "min_long": min_long,
  76.         "max_long": max_long,
  77.     }
  78.  
  79.  
  80. locations = read_json("Standortverlauf.json")
  81. convert_timestamps(locations)
  82. convert_locations(locations)
  83.  
  84. week = datetime.timedelta(days=7)
  85. today = datetime.datetime.now()
  86.  
  87. last_7_days = list(by_datetime(locations, today - week))
  88. print(f"Last 7 days: {len(last_7_days)} datapoints")
  89.  
RAW Paste Data