Skip to content

Instantly share code, notes, and snippets.

@vongohren
Created May 7, 2020 12:27
Show Gist options
  • Save vongohren/c2f0b247755bf684619289b5481087a5 to your computer and use it in GitHub Desktop.
Save vongohren/c2f0b247755bf684619289b5481087a5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Copyright 2012-2017 Gerwin Sturm
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import time
from jsonslicer import JsonSlicer
from argparse import ArgumentParser, ArgumentTypeError
from datetime import datetime
def valid_date(s):
try:
return datetime.strptime(s, "%Y-%m-%d")
except ValueError:
msg = "Not a valid date: '{0}'.".format(s)
raise ArgumentTypeError(msg)
def dateCheck(timestampms, startdate, enddate):
dt = datetime.utcfromtimestamp(int(timestampms) / 1000)
if startdate and startdate > dt : return False
if enddate and enddate < dt : return False
return True
def main(args):
arg_parser = ArgumentParser()
arg_parser.add_argument("input", help="Input File (JSON)")
arg_parser.add_argument("-o", "--output", help="Output File (will be overwritten!)")
arg_parser.add_argument("-f", "--format", choices=["kml", "json", "csv", "js", "gpx", "gpxtracks"], default="kml", help="Format of the output")
arg_parser.add_argument("-v", "--variable", default="locationJsonData", help="Variable name to be used for js output")
arg_parser.add_argument('-s', "--startdate", help="The Start Date - format YYYY-MM-DD (0h00)", type=valid_date)
arg_parser.add_argument('-e', "--enddate", help="The End Date - format YYYY-MM-DD (0h00)", type=valid_date)
arg_parser.add_argument('-c', "--chronological", help="Sort items in chronological order", action="store_true")
args = arg_parser.parse_args(args)
if not args.output: #if the output file is not specified, set to input filename with a diffrent extension
args.output = '.'.join(args.input.split('.')[:-1]) + '.' + args.format
if args.input == args.output:
arg_parser.error("Input and output have to be different files")
return
try:
f_out = open(args.output, "w")
except:
print("Error creating output file for writing")
print("Unexpected error:", sys.exc_info()[0])
raise
tic = time.perf_counter()
with open(args.input) as data:
f_out.write("{\"locations\":[")
first = True
for times in JsonSlicer(data, ('locations', None)):
if(dateCheck(times["timestampMs"], args.startdate, args.enddate)):
if not first:
f_out.write(",{")
if(first):
f_out.write("{")
first = False
f_out.write("\"timestampMs\":%s," % times['timestampMs'])
f_out.write("\"latitudeE7\":%s," % times['latitudeE7'])
f_out.write("\"longitudeE7\":%s" % times['longitudeE7'])
f_out.write("}")
f_out.write("]}")
toc = time.perf_counter()
print(f"Parsing the file in {toc - tic:0.4f} seconds")
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
path = os.path.join(takeoutDir,targetFilePath)
print('Target file path: {}'.format(path))
get_file_info(os.path.join(takeoutDir,targetFilePath))
now = datetime.now()
end = now.strftime("%Y-%m-%d")
startDelta = now - timedelta(days=700)
start = startDelta.strftime("%Y-%m-%d")
location_history_json_converter.main([
path,
'-o',
temp_parsed_filename,
'-f',
'json',
'-s',
start,
'-e',
end
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment