Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Device filtering #43

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions location_history_json_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import sys
import json
import math
import re
from argparse import ArgumentParser, ArgumentTypeError
from datetime import datetime
from datetime import timedelta
Expand All @@ -41,6 +42,12 @@
shapely_available = True


IGNORED_PLATFORMS = {
"ANDROID": [
re.compile("^android/google/sdk_.*"),
]
}

def _valid_date(s):
try:
return datetime.strptime(s, "%Y-%m-%d")
Expand Down Expand Up @@ -80,6 +87,15 @@ def _read_activity(arr):
return ret


def _valid_platform(item):
if not "platformType" in item or not "platform" in item:
return True
for ignored_platform_regex in IGNORED_PLATFORMS.get(item["platformType"], []):
if ignored_platform_regex.match(item["platform"]):
return False
return True


def _distance(lat1, lon1, lat2, lon2):
"""Returns the distance between to two coordinates in km using the Haversine formula"""

Expand Down Expand Up @@ -333,7 +349,7 @@ def _write_footer(output, format):
def convert(locations, output, format="kml",
js_variable="locationJsonData", separator=",",
start_date=None, end_date=None, accuracy=None, polygon=None,
chronological=False):
chronological=False, filtered_devices=None):
"""Converts the provided locations to the specified format

Parameters
Expand Down Expand Up @@ -371,13 +387,23 @@ def convert(locations, output, format="kml",
chronological: bool
Whether to sort all timestamps in chronological order (required for gpxtracks)
This might be uncessary since recent Takeout data seems properly sorted already.

filtered_devices: list
A list of device Tags to filter out
"""

if chronological:
locations = sorted(locations, key=lambda item: item["timestampMs"])

_write_header(output, format, js_variable, separator)

auto_filtered_devices = filtered_devices == 'auto'
if auto_filtered_devices:
filtered_devices = set()
for item in locations:
if not _valid_platform(item):
filtered_devices.add(item['deviceTag'])

first = True
last_loc = None
added = 0
Expand All @@ -401,6 +427,13 @@ def convert(locations, output, format="kml",
break
continue

if filtered_devices and "deviceTag" in item:
if item["deviceTag"] in filtered_devices:
continue

if not auto_filtered_devices and not _valid_platform(item):
continue

if polygon and not _check_point(polygon, item["latitudeE7"], item["longitudeE7"]):
continue

Expand Down Expand Up @@ -474,6 +507,12 @@ def main():
type=_valid_polygon
)

arg_parser.add_argument(
"-d", "--filtered-devices",
help="Comma separated list of device TAGs to filter out, use 'auto' to guess them from platforms",
type=lambda s: s if s == 'auto' else [int(item) for item in s.split(',')]
)

args = arg_parser.parse_args()

if args.input == args.output:
Expand Down Expand Up @@ -575,7 +614,8 @@ def main():
end_date=args.enddate,
accuracy=args.accuracy,
polygon=polygon,
chronological=args.chronological
chronological=args.chronological,
filtered_devices=args.filtered_devices
)

f_out.close()
Expand Down